Python 为什么 PyMongo 3 会给出 ServerSelectionTimeoutError?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31030307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is PyMongo 3 giving ServerSelectionTimeoutError?
提问by drfence
I'm using:
我正在使用:
- Python 3.4.2
- PyMongo 3.0.2
- mongolab running mongod 2.6.9
- uWSGI 2.0.10
- CherryPy 3.7.0
- nginx 1.6.2
- 蟒蛇 3.4.2
- PyMongo 3.0.2
- mongolab 运行 mongod 2.6.9
- uWSGI 2.0.10
- CherryPy 3.7.0
- nginx 1.6.2
uWSGI start params:
uWSGI 启动参数:
--socket 127.0.0.1:8081 --daemonize --enable-threads --threads 2 --processes 2
I setup my MongoClient ONE time:
我设置了我的 MongoClient 一次:
self.mongo_client = MongoClient('mongodb://user:[email protected]:port/mydb')
self.db = self.mongo_client['mydb']
I try and save a JSON dict to MongoDB:
我尝试将 JSON dict 保存到 MongoDB:
result = self.db.jobs.insert_one(job_dict)
It works via a unit test that executes the same code path to mongodb. However when I execute via CherryPy and uWSGI using an HTTP POST, I get this:
它通过执行与 mongodb 相同的代码路径的单元测试工作。但是,当我使用 HTTP POST 通过 CherryPy 和 uWSGI 执行时,我得到以下信息:
pymongo.errors.ServerSelectionTimeoutError: No servers found yet
Why am I seeing this behavior when run via CherryPy and uWSGI? Is this perhaps the new thread model in PyMongo 3?
为什么我在通过 CherryPy 和 uWSGI 运行时会看到这种行为?这可能是 PyMongo 3 中的新线程模型吗?
Update:
更新:
If I run without uWSGI and nginx by using the CherryPy built-in server, the insert_one()
works.
如果我使用 CherryPy 内置服务器在没有 uWSGI 和 nginx 的情况下运行,则insert_one()
工作正常。
Update 1/25 4:53pm EST:
美国东部时间 1/25 下午 4:53 更新:
After adding some debug in PyMongo, it appears that topology._update_servers()
knows that the server_type = 2 for server 'myserver-a.mongolab.com'. However server_description.known_servers()
has the server_type = 0 for server 'myserver.mongolab.com'
在 PyMongo 中添加一些调试后,似乎topology._update_servers()
知道服务器“myserver-a.mongolab.com”的 server_type = 2。但是server_description.known_servers()
,服务器“myserver.mongolab.com”的 server_type = 0
This leads to the following stack trace:
这导致以下堆栈跟踪:
result = self.db.jobs.insert_one(job_dict)
File "/usr/local/lib/python3.4/site-packages/pymongo/collection.py", line 466, in insert_one
with self._socket_for_writes() as sock_info:
File "/usr/local/lib/python3.4/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.4/site-packages/pymongo/mongo_client.py", line 663, in _get_socket
server = self._get_topology().select_server(selector)
File "/usr/local/lib/python3.4/site-packages/pymongo/topology.py", line 121, in select_server
address))
File "/usr/local/lib/python3.4/site-packages/pymongo/topology.py", line 97, in select_servers
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: No servers found yet
采纳答案by Bernie Hackett
We're investigating this problem, tracked in PYTHON-961. You may be able to work around the issue by passing connect=Falsewhen creating instances of MongoClient. That defers background connection until the first database operation is attempted, avoiding what I suspect is a race condition between spin up of MongoClient's monitor thread and multiprocess forking.
我们正在调查这个问题,在PYTHON-961 中进行了跟踪。您可以通过在创建 MongoClient 实例时传递connect=False来解决此问题。这将后台连接推迟到尝试第一个数据库操作之前,避免了我怀疑 MongoClient 的监视器线程的启动和多进程分叉之间的竞争条件。
回答by Greg
I fixed it for myself by downgrading from pymongo 3.0 to 2.8. No idea what's going on.
我通过从 pymongo 3.0 降级到 2.8 为自己修复了它。不知道发生了什么。
flask/bin/pip uninstall pymongo
flask/bin/pip install pymongo==2.8
回答by Joe Cheng
I've come accross the same problem and finally I found that the client IP is blocked by the firewall of the mongo server.
我遇到了同样的问题,最后我发现客户端 IP 被 mongo 服务器的防火墙阻止了。
回答by zephor
I encountered this too.
我也遇到了这个。
This could be due to pymongo3 isn't fork safe.
这可能是由于pymongo3 is not fork safe。
I fix this by adding --lazy-apps
param to uwsgi, this can avoid the "fork safe" problem.
我通过向--lazy-apps
uwsgi添加参数来解决这个问题,这可以避免“分叉安全”问题。
seeing uwsgi doc preforking-vs-lazy-apps-vs-lazy.
看到uwsgi文档preforking-vs-lazy-apps-vs-lazy。
Notice, no sure for this two having positive connection.
请注意,不确定这两者是否具有正相关关系。
回答by Raj
I had the same problem with Pymongo 3.5 Turns out replacing localhost with 127.0.0.1 or corresponding ip address of your mongodb instance solves the problem.
我在 Pymongo 3.5 上遇到了同样的问题结果将 localhost 替换为 127.0.0.1 或您的 mongodb 实例的相应 IP 地址解决了问题。
回答by Vaishnavi Bala
First set up the MongoDB environment.
Run this on CMD - "C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe"
- Open another CMD and run this - "C:\Program Files\MongoDB\Server\3.6\bin\mongo.exe"
首先设置MongoDB环境。
在 CMD 上运行这个 - “C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe”
- 打开另一个 CMD 并运行这个 - “C:\Program Files\MongoDB\Server\3.6\bin\mongo.exe”
And then you can use pymongo[anaconda prompt]
然后你可以使用pymongo[anaconda prompt]
import pymongo
from pymongo import MongoClient
client = MongoClient()
db = client.test_db
collection = db['test_coll']
Refer - https://docs.mongodb.com/tutorials/install-mongodb-on-windows/
参考 - https://docs.mongodb.com/tutorials/install-mongodb-on-windows/
回答by cruise_lab
I am not sure if you are using the MongoDB paired with AWS Cloud service. But if you are, I found that you have to specify which IP Address you want MongoDB to have access to.
我不确定您是否正在使用与 AWS 云服务配对的 MongoDB。但是,如果您是,我发现您必须指定您希望 MongoDB 访问哪个 IP 地址。
So what you need to do is add the IP Address of your host server to allow entry.
因此,您需要做的是添加主机服务器的 IP 地址以允许进入。
In MongoAtlas, this can be done at this page
I know there was already a solution to the same issue, but I didn't find a solution that helped my situation, so wanted to post this, so others could benefit if they ever face the same problem that I do.
我知道已经有针对同一问题的解决方案,但我没有找到对我的情况有帮助的解决方案,因此想发布此信息,以便其他人在遇到与我相同的问题时受益。
回答by Thxer
If it can help, I solved by replace :
如果有帮助,我通过替换解决了:
from flask.ext.mongoengine import MongoEngine
by :
经过 :
from flask_mongoengine import MongoEngine
回答by Patrick Chen
maybe you can try to add your server ip address into the mongod.conf file. if you use linux(ubuntu) os,you can try my solution:
也许您可以尝试将您的服务器 IP 地址添加到 mongod.conf 文件中。如果您使用 linux(ubuntu) 操作系统,您可以尝试我的解决方案:
modify mongod.conf file:
vi /etc/mongod.conf
and you can add mongodb server ip address behind 127.0.0.1,and save:
net: port:27017 bindIp:127.0.0.1,mongodb server ip
in the teminal:
sudo service mongod restart
修改 mongod.conf 文件:
vi /etc/mongod.conf
您可以在 127.0.0.1 后面添加 mongodb 服务器 ip 地址,并保存:
net: port:27017 bindIp:127.0.0.1,mongodb server ip
在终端:
sudo service mongod restart
Now,you can try to connect mongodb by using pymongo MongoClient.
现在,您可以尝试使用 pymongo MongoClient 连接 mongodb。