Pymongo keeps refusing the connection at 27017

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

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

Method 4

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.

Method 5

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.

Method 6

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']

Method 7

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.)

Method 8

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

Method 9

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.

Method 10

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

Method 11

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

Method 12

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x