MongoDB could not connect local server

I had mongdb installed and evrything was working fine. I wanted to delete the mongodb lock. By mistake the directory /var/lib/mongodb/ got deleted.

So, I uninstalled mongo db and installed again. But now i always keep getting this error:

Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect fail

The error remains same even if I try to login from terminal or rockmongo or my application. I am using ubuntu12.04 and have latest version of mongo.


You get the connect failure because the server is not running. Run this:

sudo service mongodb restart

If that does not succeed then you will need to examine the log files for the server to see what is stopping it from starting. You can find the log in /var/log/mongodb/mongod.log and if it is not an obvious cause, then please add the relevant snippet to your question and I'd be happy to take a look.

If I were to guess based on your description, I would suspect you did not set the correct permissions on /var/lib/mongodb - when you recreated it. It should look like this:

drwxr-xr-x 4 mongodb mongodb 4096 Nov 26 18:52 mongodb

If it has incorrect permissions, then you can fix it with the following command:

sudo chown -R mongodb:mongodb /var/lib/mongodb