I recently installed mongodb-2.6.0 with Homebrew.
After successfully installed, I tried to connect using the mongo
command. I am receiving the following errors which do not allow me to connect:
Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
I encountered the exact same issue and here is a clear step by step process to avoid this error.
Step 1 - Installation ( Don't follow this step if you have already installed MongoDB ):
Step 2 - Run Mongo Daemon:
Step 3 - Run Mongo Shell Interface:
In this sequence, I was able to run the
mongo
command without any error. I have also detailed the error trace and its solution on this blog.To solve your issue you need to follow the instructions which are then given to you by brew after you use "brew install mongodb".
To have launchd start mongodb at login:
Then to load mongodb now:
Or, if you don't want/need launchctl, you can just run:
You may be able to just run that last command, but it didn't work for me and I needed to run the second command. To save me in the future. I just ran the first command too. Hope that helps!
EDIT Hrishi's method of using
brew services mongodb start
worked good for me. I think they should include this in the mongo docs.I had the same error but a different root cause. Thought I'd post the solution here in case anyone else runs into the problem. I got this error after my Mac improperly shut down while I was running
mongorestore -d foo dump/foo/
.tl;dr: I fixed the problem by removing the damaged
foo.ns
file along withfoo.0
,foo.1
,... from my data folder/usr/local/var/mongodb/
. Then I restarted the mongo server withbrew services restart mongodb
and I was back to normal.Details: I kept getting the error even after trying to start or restart the mongodb service via brew or launchctl. Eventually I ran
mongod --dbpath /usr/local/var/mongodb
and saw that the service was not actually starting, and the start sequence included the following error:[initandlisten] bad .ns file: /usr/local/var/mongodb/foo.ns [initandlisten] User Assertion: 10079:bad .ns file length, cannot open database
I got rid of the bad.ns
file and the rest of the data files, and the next time I started the service I was good to go.I got this error after I upgraded to mongo 3.6 with homebrew.
The log
/usr/local/var/log/mongodb/mongo.log
contained the messageshutting down with code:62
. This error code means that the existing database is too old to run with the current version of mongo.I found 2 solutions in another SO question:
/usr/local/var/mongodb
)I chose to upgrade. In my case, that meant I had to downgrade to 3.4, run a command in the mongo console, then upgrade again. Mongo requires you to upgrade one major version at a time, so depending on how far back you were, there could be additional steps. The docs will guide you.
The
brew switch
* andbrew services restart
commands made swapping between versions relatively painless.for me on osx, I had to kill old running instance, then restarting worked.
mongo