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 to run the mongodb daemon and therefore when the daemon trys to run later as the mongodb user, it won't have permissions to start. In that case you'll get this error: Unable to create / open lock file for lockfilepath: /var/lib/mongodb/mongod.lock errno:13 Permission denied, terminating.
  • On Ubuntu, you must specify the configuration file /etc/mongodb.conf using the -f flag. Otherwise it will look for the data files in the wrong 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.5
connecting to: test
Fri Jun  1 11:20:33 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed

hisham-agil:~ hisham$ mongod
mongod --help for help and startup options
Fri Jun  1 11:24:47 [initandlisten] MongoDB starting : pid=53452 port=27017 dbpath=/data/db/ 64-bit host=hisham-agil.local
Fri Jun  1 11:24:47 [initandlisten] db version v2.0.5, pdfile version 4.5
Fri Jun  1 11:24:47 [initandlisten] git version: nogitversion
Fri 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_49
Fri Jun  1 11:24:47 [initandlisten] options: {}
Fri Jun  1 11:24:47 [initandlisten] exception in initAndListen: 10296 dbpath (/data/db/) does not exist, terminating
Fri 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() finished
Fri 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


This is actually not an error... What happens here is that Mongo relies on a daemon in order to run the local database server, so in order to "fire up" the mongo server in your shell, you have to start the mongo service first.

For Fedora Linux (wich is the Distro I use) You have to run these commands:

1 sudo service mongod start
2 mongo

And there you have it! the server is going to run. Now, If you want Mongo service to Start when the system boots then you have to run:

sudo chkconfig --levels 235 mongod on

And that's all! If you do that, now in the shell you just have to type mongo in order to start the server but that's pretty much it, the problem is you have to start the SERVICE first and then the SERVER :)

P.S. The commands I posted might work on other linux distros as well, not just in fedora... In case not maybe you have to tweak some words depending on the distro you're using ;)