57

I am trying to run a simple connection to pymongo but it keeps returning that the connection was refused

Here is what I tried:

>>>from pymongo import Connection
>>>connection = Connection('localhost',27017)

here is what I get

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pymongo-2.0.1_-py2.7-linux i686.egg/pymongo/connection.py", line 348, in __init__
 self.__find_node()
File "/usr/local/lib/python2.7/dist-packages/pymongo-2.0.1_-py2.7-linux- i686.egg/pymongo/connection.py", line 627, in __find_node
  raise AutoReconnect(', '.join(errors))
pymongo.errors.AutoReconnect: could not connect to localhost:27017: [Errno 111]     Connection refused

How do I fix this?

Ulmer
  • 1,530
  • 2
  • 17
  • 24

12 Answers12

56

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
alper
  • 2,919
  • 9
  • 53
  • 102
Guille Polito
  • 882
  • 7
  • 3
16

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.

muinh
  • 535
  • 6
  • 14
13

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

Kedar.Aitawdekar
  • 2,364
  • 1
  • 23
  • 25
10

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
Qaz
  • 1,556
  • 2
  • 20
  • 34
7

It looks like you might not be running the MongoDB server. One thing that frequently trips me up is that if the server was shut down uncleanly, it will refuse to start up again until you remove the mongod.lock file from the data directory.

Rick Copeland
  • 11,672
  • 5
  • 39
  • 38
4

Try following commands :

sudo service mongod start
sudo service mongod status

db.py

import pymongo
from pymongo import MongoClient
#mongo client is connected
client = MongoClient()
db     = client['db']
Nija I Pillai
  • 1,046
  • 11
  • 13
3

Rather than deleting mongod.lock, I'd recommend running 'mongod --repair'. (I figure it's better to go in through the front door whenever possible. And there may be other things that this catches as well, AFAIK.)

ryepdx
  • 206
  • 1
  • 6
  • 1
    Fair enough. Though I don't recall size being a factor here. My answer's certainly valid for people working with smaller datasets. – ryepdx Nov 07 '13 at 17:56
2

None of the above answers worked for me, as I am using docker-compose so this worked for me:

docker run --rm --volumes-from my-mongo-server mongo unlink "/data/db/mongod.lock"
docker run --rm --volumes-from my-mongo-server mongo --repair

Replace my-mongo-server with your container name/id.

carkod
  • 1,844
  • 19
  • 32
1

If you're trying to connect from a server (other than your localhost), try checking if your mongo installation is complete : you should have: * /var/lib/mongodb file * /var/log/mongodb file * mongodb.service as service ( check by starting the service sudo service mongodb start )

If any of those fails, try reinstalling mongo ( Failed to start mongod.service: Unit mongod.service not found )

This solved my problem. Cheers

Neshy
  • 41
  • 5
1

First make sure you have installed mongodb using sudo apt install mongodb

Biplob Das
  • 2,818
  • 21
  • 13
0

For the newer versions(4.x) of MongoDb, you can try:

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

sudo systemctl daemon-reload

sudo systemctl start mongod

Abhinav Anand
  • 573
  • 3
  • 5
  • 19
-1

Thanks, but for me i just had to stop mongod and then restart it and it worked fine without having to remove anything. PS : pymongo 2.7.2