简体   繁体   English

升级到PyMongo 3.0导致ServerSelectionTimeoutError

[英]Upgrade to PyMongo 3.0 Resulting in ServerSelectionTimeoutError

I recently upgraded a Bottle + uWSGI + Nginx application to MongoDB 3.0.2. 我最近将Bottle + uWSGI + Nginx应用程序升级到MongoDB 3.0.2。 It was working fine with PyMongo 2.8, but today I upgraded to PyMongo 3.0 by running following command: 它与PyMongo 2.8一起工作正常,但今天我通过运行以下命令升级到PyMongo 3.0:

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. 但是,如果我转到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>

It seems like only my application cannot find the MongoDB server. 似乎只有我的应用程序找不到MongoDB服务器。 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. 此外,如果我降级回PyMongo 2.8,一切正常。

The fix for me was surprising; 对我的解决方案令人惊讶; I entered this at the Bash prompt : 我在Bash提示下输入了这个

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. 结果是Apache权限问题。


Note: I'm running PyMongo 3.0, Python 2.6, and Mongo 2.4 on CentOS 6.6. 注意:我在CentOS 6.6上运行PyMongo 3.0,Python 2.6和Mongo 2.4。 I got a marginally different error, but it was in the same line of PyMongo. 我得到了一个略有不同的错误,但它与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

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. 但是我尝试在MongoClient()创建中设置connect = False,如此建议无济于事。 Currently the only solution that seems to work across the board is to downgrade to 2.8 ( pip install pymongo==2.8 ) 目前唯一可以全面运作的解决方案是降级到2.8( pip install pymongo==2.8

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). 避免触发错误的解决方法很有效(在创建MongoClient实例时传递connect = False )。 There will be a clearer warning in pymongo version 3.0.4. 在pymongo 3.0.4版本中会有更清晰的警告。 and hopefully a fix in the next versions. 并希望在下一个版本中修复。

Could this be related to this critical PyMomongo 3.0 bug when using mongos? 这可能与使用mongos时这个关键的PyMomongo 3.0错误有关吗? If so, they pushed a fix on master branch (see this commit .) 如果是这样,他们推动了对master分支的修复(参见此提交 。)

EDITED: 编辑:

Did you check your mongodb log file? 你检查过你的mongodb日志文件了吗? I think that I had the same problem than you. 我认为我遇到的问题与你相同。

I found the following problem in /var/log/mongodb: 我在/ 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? 在下面的帖子中是这个错误的解决方案: 为什么得到错误mongod死了但是subsys被锁定并且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. 也许你有同样的错误,你需要使用smallfiles才能工作。

Best regards. 最好的祝福。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM