Upgrade to PyMongo 3.0 Resulting in ServerSelectio

2020-07-06 06:00发布

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.

5条回答
淡お忘
2楼-- · 2020-07-06 06:18

EDITED:

Did you check your mongodb log file? I think that I had the same problem than you.

I found the following problem in /var/log/mongodb:

[initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
It seems I have no enough free disk in my VM so my mongodb it wasn't running on my machine.

In the following post is the solution for this error: Why getting error mongod dead but subsys locked and Insufficient free space for journal files on Linux?

Now it seems that my problem was solved. Maybe you have the same error and you need to use smallfiles in order to work.

Best regards.

查看更多
Melony?
3楼-- · 2020-07-06 06:20

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)

查看更多
Explosion°爆炸
4楼-- · 2020-07-06 06:21

Could this be related to this critical PyMomongo 3.0 bug when using mongos? If so, they pushed a fix on master branch (see this commit.)

查看更多
疯言疯语
5楼-- · 2020-07-06 06:23

The fix for me was surprising; I entered this at the Bash prompt:

sudo /usr/sbin/setsebool -P httpd_can_network_connect 1
sudo service httpd restart

Credit to this answer. It turned out to be an Apache permissions issue.


Note: I'm running PyMongo 3.0, Python 2.6, and Mongo 2.4 on CentOS 6.6. I got a marginally different error, but it was in the same line of PyMongo. This was the end of my stack trace:

File "/usr/lib64/python2.6/site-packages/pymongo/topology.py", line 93, in select_servers
    self._error_message(selector))
ServerSelectionTimeoutError: localhost:27017: [Errno 13] Permission denied
查看更多
闹够了就滚
6楼-- · 2020-07-06 06:33

I think this is a duplicate of this question.

The workaround to avoid triggering the bug works well (pass connect=False when creating instances of MongoClient). There will be a clearer warning in pymongo version 3.0.4. and hopefully a fix in the next versions.

查看更多
登录 后发表回答