我最近将Bottle + uWSGI + Nginx应用程序升级到MongoDB 3.0.2。在PyMongo 2.8上运行正常,但是今天我通过运行以下命令升级到PyMongo 3.0:
pip install --upgrade pymongo
我没有对代码进行任何其他更改,但是现在我不断遇到以下错误。
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
我用来连接数据库的功能如下:
def connect_db(db_name):
global db
host = "localhost"
port = 27017
connection = pymongo.MongoClient(host=host, port=port)
db = connection[db_name]
我已经重新启动了所有服务器。静态页面可以正常工作,但是任何尝试访问数据库的页面都将挂起并引发上述错误。但是,如果我转到
mongo
shell或Python shell并查询MongoDB服务器,则可以正常工作。>>> 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>
似乎只有我的应用程序找不到MongoDB服务器。请注意,我正在使用虚拟环境,以防以任何方式影响这种情况。另外,如果我降级到PyMongo 2.8,一切正常。
最佳答案
这似乎与this question和此bug中的问题相同。但是我尝试在MongoClient()创建中将connect = False设置为建议,但无济于事。目前,似乎唯一可行的解决方案是降级到2.8(pip install pymongo==2.8
)
关于python - 升级到PyMongo 3.0导致ServerSelectionTimeoutError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29663931/