Pymongo keeps refusing the connection at 27017

Removing mongod.lock inside /var/lib/mongodb

sudo rm /var/lib/mongodb/mongod.lock

And then restarting the service should do it. For example, in my Ubuntu installation, restarting the server is something like this:

sudo service mongodb start

If you found this page because you use Docker and you face the connection problem, try to use in your client initialization the docker container name of the mongodb instead of the localhost:27017 or 0.0.0.0:27017

Steps to fix:

  1. write docker ps in console
  2. find the name of container (it's in the last column of the command output called NAMES
  3. MongoClient('mongodb://CONTAINER_NAME')

PROFIT.


Just try following commands in given order :

sudo rm /var/lib/mongodb/mongod.lock

sudo mongod --repair

sudo service mongodb start

sudo service mongodb status

That's it now you could see following as output of last command:

mongodb start/running, process 2796


For anyone who's having this problem on a remote server rather than the localhost, try enabling external interfaces:

  • Go to the configuration file (ex. /etc/mongodb.conf)
  • Find bind_ip=127.0.0.1
  • Comment out that line with a # at the front
  • Restart mongod

Tags:

Python

Pymongo