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.