Why is PyMongo 3 giving ServerSelectionTimeoutError?

社会主义新天地 提交于 2019-11-27 03:59:06

问题


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

uWSGI start params:

--socket 127.0.0.1:8081 --daemonize --enable-threads --threads 2 --processes 2

I setup my MongoClient ONE time:

self.mongo_client = MongoClient('mongodb://user:pw@host.mongolab.com:port/mydb')
self.db = self.mongo_client['mydb']

I try and save a JSON dict to 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:

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?

Update:

If I run without uWSGI and nginx by using the CherryPy built-in server, the insert_one() works.

Update 1/25 4:53pm EST:

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'

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

回答1:


We're investigating this problem, tracked in PYTHON-961. You may be able to work around the issue by passing connect=False when 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.




回答2:


I fixed it for myself by downgrading from pymongo 3.0 to 2.8. No idea what's going on.

   flask/bin/pip uninstall pymongo
   flask/bin/pip install pymongo==2.8



回答3:


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.




回答4:


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.

So what you need to do is add the IP Address of your host server to allow entry.

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.




回答5:


As mentioned here: https://stackoverflow.com/a/54314615/8953378

I added ?ssl=true&ssl_cert_reqs=CERT_NONE to my connection string, and it fixed the issue.

so instead of:

connection_string = "mongodb+srv://<USER>:<PASSWORD>@<CLUSTER>/<COLLECTION>"

I wrote:

connection_string = "mongodb+srv://<USER>:<PASSWORD>@<CLUSTER>/<COLLECTION>?ssl=true&ssl_cert_reqs=CERT_NONE"

(Note that if you have other parameters in your connection string, you need to change the ? to & )




回答6:


I solved this by installing dnspython (pip install dnspython). The issue is that: "The "dnspython" module must be installed to use mongodb+srv:// URIs"




回答7:


I've come accross the same problem and finally I found that the client IP is blocked by the firewall of the mongo server.




回答8:


I encountered this too.

This could be due to pymongo3 isn't fork safe.

I fix this by adding --lazy-apps param to uwsgi, this can avoid the "fork safe" problem.

seeing uwsgi doc preforking-vs-lazy-apps-vs-lazy.

Notice, no sure for this two having positive connection.




回答9:


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:

  1. 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
    
  2. in the teminal:

    sudo service mongod restart

Now,you can try to connect mongodb by using pymongo MongoClient.




回答10:


That error has occurred because there is no MongoDB server running in the background. To run the MongoDB server open cmd or anaconda prompt and type this:-

"C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe"

then run

import pymongo
myclient = pymongo.MongoClient()    
mydb = myclient["mydatabase"]
myclient.list_database_names()



回答11:


I'm using pymongo 3.2 and I run into the same error, however it was a missconfiguration in my case. After enabling authorization, I forgot to update the port in the url which ended up in a connection timout. Probably it is worth to mention that ?authSource might be required as it is typically different than the database storing the application data.




回答12:


I commented out bindIP variable in mongod.conf instead of allowing all connections (for which you have to enter 0.0.0.0). Of course, beware of the consequence.




回答13:


This has been fixed in PyMongo with this pull_request.




回答14:


  • 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"

And then you can use 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/




回答15:


If it can help, I solved by replace :

from flask.ext.mongoengine import MongoEngine

by :

from flask_mongoengine import MongoEngine


来源:https://stackoverflow.com/questions/31030307/why-is-pymongo-3-giving-serverselectiontimeouterror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!