couldn't connect to server 127.0.0.1 shell/mongo.js couldn't connect to server 127.0.0.1 shell/mongo.js mongodb mongodb

couldn't connect to server 127.0.0.1 shell/mongo.js


  • Manually remove the lockfile: sudo rm /var/lib/mongodb/mongod.lock
  • Run the repair script: sudo -u mongodb mongod -f /etc/mongodb.conf --repair

Please note the following:

  • You must run this command as the mongodb user. If you run it as root,then root will own files in /var/lib/mongodb/ that are necessary torun the mongodb daemon and therefore when the daemon trys to runlater as the mongodb user, it won't have permissions to start. Inthat case you'll get this error: Unable to create / open lock filefor lockfilepath: /var/lib/mongodb/mongod.lock errno:13 Permissiondenied, terminating.
  • On Ubuntu, you must specify the configuration file /etc/mongodb.confusing the -f flag. Otherwise it will look for the data files in thewrong place and you will see the following error: dbpath (/data/db/)does not exist, terminating.


sudo rm /var/lib/mongodb/mongod.lock   sudo -u mongodb mongod -f /etc/mongodb.conf --repair sudo service mongodb start

Here is all, sometimes, it takes a little while to start mongo after performing these operations.


Trying running $mongod

If you get en error such as

MongoDB shell version: 2.0.5connecting to: testFri Jun  1 11:20:33 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84exception: connect failedhisham-agil:~ hisham$ mongodmongod --help for help and startup optionsFri Jun  1 11:24:47 [initandlisten] MongoDB starting : pid=53452 port=27017 dbpath=/data/db/ 64-bit host=hisham-agil.localFri Jun  1 11:24:47 [initandlisten] db version v2.0.5, pdfile version 4.5Fri Jun  1 11:24:47 [initandlisten] git version: nogitversionFri Jun  1 11:24:47 [initandlisten] build info: Darwin gamma.local 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:48:32 PST 2012; root:xnu-1699.24.23~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49Fri Jun  1 11:24:47 [initandlisten] options: {}Fri Jun  1 11:24:47 [initandlisten] exception in initAndListen: 10296 dbpath (/data/db/) does not exist, terminatingFri Jun  1 11:24:47 dbexit: Fri Jun  1 11:24:47 [initandlisten] shutdown: going to close listening sockets...Fri Jun  1 11:24:47 [initandlisten] shutdown: going to flush diaglog...Fri Jun  1 11:24:47 [initandlisten] shutdown: going to close sockets...Fri Jun  1 11:24:47 [initandlisten] shutdown: waiting for fs preallocator...Fri Jun  1 11:24:47 [initandlisten] shutdown: lock for final commit...Fri Jun  1 11:24:47 [initandlisten] shutdown: final commit...Fri Jun  1 11:24:47 [initandlisten] shutdown: closing all files...Fri Jun  1 11:24:47 [initandlisten] closeAllFiles() finishedFri Jun  1 11:24:47 dbexit: really exiting now

Then you've run into a basic startup error that is pretty common.

By default mongod will try to use /data/db for its database files, which in this case, does not exist.

You can't start

mongo 

until you handle

mongod.

Try creating those directories and make sure they are writable by the same user that is running the mongod process.

**See similar question-- Getting an error, "Error: couldn't connect to server 127.0.0.1 shell/mongo.js" & when trying to run mongodb on mac osx lion