Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pymongo keeps refusing the connection at 27017

Tags:

python

pymongo

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?

like image 971
Ulmer Avatar asked Oct 12 '11 17:10

Ulmer


People also ask

Can't connect to found database localhost 27017?

0.1 : 27017 ” is a general error message indicating that your client/driver cannot connect to a server on the specified hostname/IP and port. In this specific example, 127.0. 0.1 is the hostname or IP and 27017 is the port. Your connection from client to server is blocked by firewall or network configuration.

Can't connect to server connection refused MongoDB?

Normally this caused because you didn't start mongod process before you try starting mongo shell. This case worked out for me, i had to do ~>sudo mongod --dbpath /dbpathlocation and then opened the new terminal to run mongo...

Do I need to close PyMongo connection?

There's no need to close a Connection instance, it will clean up after itself when Python garbage collects it. You should use MongoClient instead of Connection ; Connection is deprecated. To take advantage of connection pooling, you could create one MongoClient that lasts for the entire life of your process.

Why I cant connect to MongoDB?

These are some of the solutions: Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.


1 Answers

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 
like image 123
Guille Polito Avatar answered Oct 04 '22 19:10

Guille Polito