Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrade to PyMongo 3.0 Resulting in ServerSelectionTimeoutError

I recently upgraded a Bottle + uWSGI + Nginx application to MongoDB 3.0.2. It was working fine with PyMongo 2.8, but today I upgraded to PyMongo 3.0 by running following command:

pip install --upgrade pymongo

I haven't done any other changes to the code, but now I keep getting the following error.

File "/pymongo/cursor.py", line 968, in __next__ if len(self.__data) or self._refresh():
File "/pymongo/cursor.py", line 905, in _refresh self.__read_preference))
File "/pymongo/cursor.py", line 812, in __send_message **kwargs)
File "/pymongo/mongo_client.py", line 716, in _send_message_with_response server = topology.select_server(selector)
File "/pymongo/topology.py", line 113, in select_server server_selection_timeout))
File "/pymongo/topology.py", line 93, in select_servers self._error_message(selector))
ServerSelectionTimeoutError: No servers found yet

The function I use to connect to the database is the following:

def connect_db(db_name):
    global db
    host = "localhost"
    port = 27017
    connection = pymongo.MongoClient(host=host, port=port)
    db = connection[db_name]

I have restarted all the servers. The static pages work fine, but any page that tries to reach the database hangs and throws the error above. However, if I go to a mongo shell or a Python shell and query the MongoDB server, it works fine.

>>> import pymongo
>>> host = "localhost"
>>> port = 27017
>>> connection = pymongo.MongoClient(host=host, port=port)
>>> db = connection[test]
>>> db.test.insert_one({"test": True});
<pymongo.results.InsertOneResult object at 0x7fc43b8efc80>

It seems like only my application cannot find the MongoDB server. Note that I am using a Virtual Environment in case that would affect the situation in any way. Also, if I downgrade back to PyMongo 2.8, everything works fine.

like image 807
Juan Carlos Farah Avatar asked Apr 16 '15 01:04

Juan Carlos Farah


People also ask

What is ServerSelectionTimeoutError?

[Solved] pymongo.errors.ServerSelectionTimeoutErrorThis error is thrown when you attempt to do an operation on MongoDB, but no MongoDB server is available. If there is no suitable server for an operation PyMongo waits for a specific amount of time defined by the constant serverSelectionTimeoutMS .

How do I check my Pymongo version?

The result of that command is: pymongo==3.0, none of them above! @Egzona So, that's the version of pymongo you've installed. You can force to install it by pip install pymongo==2.7 , sudo maybe required.

What is Pymongo and what can you do with Pymongo?

In general, PyMongo provides a rich set of tools that you can use to communicate with a MongoDB server. It provides functionality to query, retrieve results, write and delete data, and run database commands.


1 Answers

It seems to be the same issue as in this question, and this bug. However I tried setting connect=False in the MongoClient() creation as suggested there to no avail. Currently the only solution that seems to work across the board is to downgrade to 2.8 (pip install pymongo==2.8)

like image 125
tdc Avatar answered Sep 28 '22 04:09

tdc