Cannot connect to MongoDB errno:61

2019-03-12 07:06发布

问题:

I installed MongoDB using macports and tried running the mongo command in order to start the mongo shell. I received the following errors:

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

I then ran mongod and received the following error:

$ [initandlisten] MongoDB starting : pid=11984 port=27017 dbpath=/data/db 64-bit host=Nikitas-MacBook-Air.local
$ [initandlisten] 
$ [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
$ [initandlisten] db version v2.6.0
$ [initandlisten] git version: nogitversion
$ [initandlisten] build info: Darwin tennine-slave.macports.org 13.1.0 Darwin Kernel Version 
13.1.0: Thu Jan 16 19:40:37 PST 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_55
$ [initandlisten] allocator: tcmalloc
$ [initandlisten] options: {}
$ [initandlisten] exception in initAndListen: 10296 
*********************************************************************
 ERROR: dbpath (/data/db) does not exist.
 Create this directory or give existing directory in --dbpath.
 See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
, terminating
$ [initandlisten] dbexit: 
$ [initandlisten] shutdown: going to close listening sockets...
$ [initandlisten] shutdown: going to flush diaglog...
$ [initandlisten] shutdown: going to close sockets...
$ [initandlisten] shutdown: waiting for fs preallocator...
$ [initandlisten] shutdown: lock for final commit...
$ [initandlisten] shutdown: final commit...
$ [initandlisten] shutdown: closing all files...
$ [initandlisten] closeAllFiles() finished
$ [initandlisten] dbexit: really exiting now

Where do I create the /data/db directory, at root? Would the second error be the reason for the errno:61 when I run mongo?

回答1:

Yes, you would create /data/db at root. The MongoDB Documentation suggests the following command for creation on OS X mkdir -p /data/db.

Regarding your second question, also yes. Since the Mongo Daemon failed to start and isn't listening you get the connection error.

MongoDB defaults to /data/db for the data directory. You can override the default using the --dbpath option on the mongod command (also on the doc link above).



回答2:

I encountered similar problem. I have detailed the error trace and solution on this blog This is step by step process I did to solve this issue:

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.



回答3:

** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

you can add one line into your /etc/bashrc, the warning will disappear.

ulimit -S -n 1024

use chown -R {user} /data/db to change the owner of /data/db from root to yourself. Then you can start mongo shell just by mongo, no need to use sudo mongo.