MongoDB : Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused MongoDB : Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused mongodb mongodb

MongoDB : Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused


Are you running all of these commands in the same terminal as separate actions as your timestamps suggest? (if not, I would expect to see overlap between the failures and logs in order to diagnose properly).

If so, then what you are doing is as follows:

  1. Starting a shell, attempting to connect to a non-running database (connection refused)
  2. Starting the database, seeing 0 connections in the logs
  3. Stopping the database (Ctrl-C or similar), starting a shell, fails to connect again because database has been shut down
  4. Starting the database, seeing 0 connections in logs
  5. repeat

The database needs to be running concurrently with the shell so you can connect to it. Hence you should start the mongod process in one terminal, leave it running, and then open a new terminal (or tab) and run the mongo command to connect to the still running database. The other option is to run mongod as a daemon, which will allow you to run the mongo command in the same terminal once it returns.

Note: you should never have a folder with 777 permissions, it is not required and is a significant security risk.


Just try this command.

sudo chown mongodb /tmp/mongodb-27017.sock

And

sudo service mongod restart


Fixed!

The reason was the dbpath variable in /etc/mongodb.conf. Previously, I was using mongodb 1.8, where the default value for dbpath was /data/db. The upstart job mongodb(which comes with mongodb-10gen package) invokes the mongod with --config /etc/mongodb.conf option.

As a solution, I only had to change the owner of the /data/db directory recursively.

Like this: Go to "/data" location in the terminal and then type

sudo chown -R <username>:foldername

For example:

sudo chown -R raja db/