Cannot connect to mongodb errno:61 Connection refu

2019-01-16 00:28发布

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

11条回答
倾城 Initia
2楼-- · 2019-01-16 01:03

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 ):

brew update
brew install mongodb

Step 2 - Run Mongo Daemon:

mkdir -p /data/db
sudo mongod

Step 3 - Run Mongo Shell Interface:

mongo

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.

查看更多
爷、活的狠高调
3楼-- · 2019-01-16 01:06

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:

ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents

Then to load mongodb now:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

Or, if you don't want/need launchctl, you can just run:

mongod --config /usr/local/etc/mongod.conf

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.

查看更多
三岁会撩人
4楼-- · 2019-01-16 01:07

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 with foo.0, foo.1,... from my data folder /usr/local/var/mongodb/. Then I restarted the mongo server with brew 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
5楼-- · 2019-01-16 01:13

I got this error after I upgraded to mongo 3.6 with homebrew.

The log /usr/local/var/log/mongodb/mongo.log contained the message shutting 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:

  • Delete your database folder (/usr/local/var/mongodb)
  • Or follow the upgrade procedure

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 * and brew services restart commands made swapping between versions relatively painless.

查看更多
一夜七次
6楼-- · 2019-01-16 01:14

for me on osx, I had to kill old running instance, then restarting worked.

$>ps -aef | grep mongo
502  8047     1   0 11:52AM ??         0:00.23 /usr/local/opt/mongodb/bin/mongod --config /usr/local/etc/mongod.conf 

$>sudo kill 8047

$>sudo mongod
查看更多
Evening l夕情丶
7楼-- · 2019-01-16 01:15

My guess is that you do not have a running database while trying to access testing through the "mongo" command.

First run this command in terminal:

mongod 

Then open another terminal window and run:

mongo

Everything should be working now.

查看更多
登录 后发表回答