Reputation: 1580
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?
Upvotes: 61
Views: 124458
Reputation: 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
Upvotes: -1
Reputation: 2230
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.
Upvotes: 2
Reputation: 615
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:
docker ps
in consoleNAMES
MongoClient('mongodb://CONTAINER_NAME')
PROFIT.
Upvotes: 21
Reputation: 3104
First make sure you have installed mongodb using sudo apt install mongodb
Upvotes: 1
Reputation: 639
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
Upvotes: 0
Reputation: 39
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
Upvotes: 1
Reputation: 892
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
Upvotes: 57
Reputation: 1136
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']
Upvotes: 4
Reputation: 1576
For anyone who's having this problem on a remote server rather than the localhost, try enabling external interfaces:
Upvotes: 11
Reputation: 2454
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
Upvotes: 14
Reputation: 206
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.)
Upvotes: 3
Reputation: 11912
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.
Upvotes: 7