If someone can provide some insights here I would GREATLY appreciate it.
I am new to MongoDB, and (relatively) new to the command line.
I had a express/node.js app running on MongoDB locally successfully, but upon restarting my computer, I attempted to restart the Mongo server and it began giving errors and wouldn't start. Since then, I have re-installed Mongo several times only to find the same error occurring. this is what I am receiving:
privee:mongodb-osx-x86_64-2.4.6 jonlinton$ ./bin/mongo MongoDB shell version: 2.4.6 connecting to: test Mon Aug 26 14:48:47.168 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145 exception: connect failed
Am I missing a step? Should I be creating a config file? Some basic guidance would be really helpful. Thanks in advance for any insights.
If you have installed mongodb through
homebrew then you can simply start mongodb through (
mongodb-community if installted
brew services start mongodb
brew services start mongodb-community
Then access the shell by
You can shut down your db by
brew services stop mongodb
You can restart your db by
brew services restart mongodb
For more options
brew info mongodb
Updated answer (9/2/2019):
Homebrew has removed mongodb formula from its core repository, see this pull request.
The new way to install mongodb using Homebrew is as follows:
brew tap mongodb/brew brew install mongodb-community
After installation you can start the mongodb service by following the caveats:
~> brew info mongodb-community mongodb/brew/mongodb-community: stable 4.2.0 High-performance, schema-free, document-oriented database https://www.mongodb.com/ Not installed From: https://github.com/mongodb/homebrew-brew/blob/master/Formula/mongodb-community.rb ==> Caveats To have launchd start mongodb/brew/mongodb-community now and restart at login: brew services start mongodb/brew/mongodb-community Or, if you don't want/need a background service you can just run: mongod --config /usr/local/etc/mongod.conf
Deprecated answer (8/27/2019): I assume you are using Homebrew. You can see the additional information that you need using
brew info $FORMULA
~> brew info mongo 255 mongodb: stable 2.4.6, devel 2.5.1 http://www.mongodb.org/ /usr/local/Cellar/mongodb/2.4.5-x86_64 (20 files, 287M) * Built from source From: https://github.com/mxcl/homebrew/commits/master/Library/Formula/mongodb.rb ==> Caveats To reload mongodb after an upgrade: launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Caveats is what you need to follow after installation.
Assuming you have created the data/db directory under bin after install.
- Start a terminal for your mongo server
- Go to
Run the command
Start a terminal for your mongo shell
- Go to
Run the command (make sure you put the name of the database)
Problem here is you are trying to open a mongo shell without starting a mongo db which is listening to port 127.0.0.1:27017(deafault for mongo db) thats what the error is all about:
Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145 exception: connect failed
The easiest solution is to open the terminal and type
$ mongod --dbpath ~/data/db
Note: dbpath here is "Users/user" where data/db directories are created
i.e., you need to create directory data and sub directory db in your user folder. For e.g say `
After mongo db is up. Open another terminal in new window and type
it will open mongo shell with your mongo db connection opened in another terminal.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Update and verify you are good with
brew update brew doctor
Install mongodb with
brew install mongodb
Create folder for mongo data files:
mkdir -p /data/db
sudo chown -R `id -un` /data/db
Open another terminal window & run and keep running a mongo server/daemon
Return to previous terminal and run a mongodb shell to access data
To quit each of these later:
For those that could be facing the same problem and the solutions suggested above aren't working, for example in my case, I had installed mongodb-community, so you might wanna run the command below to restart your mongo server.
For those that installed mongodb-community using brew
brew services start mongodb-community
additionally you may want mongo to run on another port, then paste this command on terminal,
mongod --dbpath /data/db/ --port 27018
where 27018 is the port we want mongo to run on
- mongod exists in your bin i.e
/usr/local/bin/for mac ( which would be if you installed with brew), otherwise you'd need to navigate to the path where mongo is installed
- the folder
mongo => mongo-db console mongodb => mongo-db server
If you're on Mac and looking for a easier way to start/stop your mongo-db server, then MongoDB Preference Pane is something that you should look into. With it, you start/stop your mongo-db instance via UI. Hope it helps!
Make sure you are logged in as root user in your terminal.
Steps to start mongodb server in your mac
- Open Terminal
- Run the command
- Enter your administrator password
- run the command
- MongoDb Server starts
Hope it helps you. Thanks
Nothing less likely to be outdated that the official docs: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
Download MongoDB and install it on your local machine. Link https://www.mongodb.com/try/download/enterprise
Extract the file and put it on the desktop. Create another folder where you want to store the data. I have created mongodb-data folder. Then run the below command.
Before the hyphen is the executable path of your mongoDB and after hyphen is your data store.