开发者

Pymongo keeps refusing the connection at 27017

I am trying to run a simple connection开发者_Go百科 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?


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


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.


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


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


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.


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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜