Why is PyMongo 3 giving ServerSelectionTimeoutError?
Asked Answered
A

30

77

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:[email protected]: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
Anthropocentric answered 24/6, 2015 at 15:11 Comment(9)
I'm hitting this too on a similar setup. Did you ever find an answer?Disepalous
Nothing yet ... haven't gotten back to debugging.Anthropocentric
Want me to start a bounty? Would you be available to answer follow up questions? I'm completely stuck on this myself.Disepalous
Is this useful? blog.fejes.ca/?p=2496 I'm not sure how it would apply to uwsgi?Disepalous
Bounty would be great. I tried posting in the mongodb google group but my question never got posted.Anthropocentric
I see your comment has now been posted on the Google Group, @drfence, and PyMongo's maintainer Bernie Hackett is trying to diagnose over there: groups.google.com/forum/#!topic/mongodb-user/7qX03P3_zKMHluchy
In general if you're seeing what looks like a PyMongo bug, the most direct way to communicate with me, Bernie, Anna, and Luke is to file a ticket in the PYTHON project at jira.mongodb.org. We try to respond immediately.Hluchy
Thanks Jesse, I guess we couldn't tell if it was a usage issue or an actual bug.Disepalous
I had the same problem but the issue was the connection at the ORM level with mongoengineErinerina
U
77

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.

Unhopedfor answered 2/7, 2015 at 21:31 Comment(5)
This maybe a dumb question but how exactly is this done?Donica
This solution fails if we are using authentication in mongodb.Is there be any other solution for thisImbricate
This didn't work for me - pymongo 3.7.2 with python 3.6.7 under Ubuntu 18.04 -- can connect and exec a fetch, but get the ServerSelectionTimeoutError exception when I attempt a simple insert. Debugger shows that the same SSL certfile reference throughout all three stages in the connection resource.Bossy
@SARose, mongo = MongoClient(MONGO_CONNECTION_STRING, connect=False)Android
@MichealShallop were you ever able to fix this? I have the same issueTeredo
A
30

As mentioned here: https://mcmap.net/q/266455/-serverselectiontimeouterror-pymongo

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 & )

Adrieneadrienne answered 10/10, 2019 at 5:42 Comment(3)
Solved the issue for me with pymongo==3.11.3Glandulous
Solved the issue for me with Flask-Pymongo 2.3.0Dachia
Fixed my issue as well. pymongo==3.12.3Icbm
B
17

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 enter image description here

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.

Bushbuck answered 30/5, 2018 at 1:59 Comment(1)
Not exactly with AWS but I had the issue with IP address. Thanks!Nehru
D
15

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
Disepalous answered 1/7, 2015 at 14:47 Comment(3)
pip install pymongo==2.8 --upgradePoult
as of 22NOV16, with Debian Jessie (ie pymongo 3.3.x and lastest mongodb-org ie something like 3.2) got same error, even with connect=False flag. downgrading to pymongo 2.8 solved the problem.Brindled
I used pymogo==3.7.2 and I uninstall it and installed pymongo==2.8. But it didn't work for me.Productive
E
8

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.

Elbertelberta answered 17/9, 2017 at 15:15 Comment(1)
fixed for me as well (pymongo==3.6, motor)Rogers
M
5

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

Maritime answered 2/6, 2019 at 19:42 Comment(0)
B
4

Go to your Atlas Console > Network Access, then add your client IP address, Ex. 0.0.0.0/00 (Note: All client ips can access your database)

Network access page

Borroff answered 14/10, 2021 at 6:43 Comment(0)
H
3

In my case

  • I was using Mongo Atlas
  • I got another IP adress after a router reboot

hence I had to add that IP to the whitelist on Mongo Atlas settings via

MongoAtlas website -> Network Access -> IP Whitelist -> Add IP Address -> Add Current IP Address

then wait for IP Address's status to change to Active and then try to run the app again

if you are using a repl.it server to host, just add the host ip you used to configure your server, for me it was 0.0.0.0, which is the most common

Heidi answered 4/8, 2020 at 2:34 Comment(0)
T
2

pymongo 3 will not tell you your connection failed when you instantiate your client. You may not be connected.

https://api.mongodb.com/python/3.5.1/api/pymongo/mongo_client.html

"it no longer raises ConnectionFailure if they are unavailable .. You can check if the server is available like this:"

from pymongo.errors import ConnectionFailure
client = MongoClient()
try:
    # The ismaster command is cheap and does not require auth.
    client.admin.command('ismaster')
except ConnectionFailure:
    print("Server not available")
Tracy answered 17/3, 2020 at 15:39 Comment(0)
R
2

I was facing the same exception today. In my case, the proxy settings were probably blocking the connection since I could establish a successful connection to the mongodb by changing my wifi. Even if this question is marked as solved already, it can hopefully narrow down the problem for some others.

Ridgeway answered 10/6, 2020 at 15:4 Comment(0)
P
1

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

Polypody answered 27/7, 2016 at 7:11 Comment(0)
A
1

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.

Assist answered 28/12, 2016 at 10:2 Comment(0)
P
1

I simply added my current IP address in the network access tab, as it got changed automatically. Deleted the earlier one, there was a slight change in IP address.

Pliers answered 4/12, 2020 at 12:11 Comment(0)
J
1

I was also facing the same issue. Then, I added

import certifi
Client = MongoClient("mongodb+srv://<username>:<password>@cluster0.ax9ugoz.mongodb.net/?retryWrites=true&w=majority", tlsCAFile=certifi.where())

and it solved my issue.

Certifi provides a collection of Root Certificates for validating the trustworthiness of SSL certificates while verifying the identity of TLS hosts.

Jacobsohn answered 31/10, 2022 at 2:48 Comment(0)
L
1

Go to mongodb Atlas/ Network Access and make sure to add your server ip or just click allow access from any where

Leaving answered 4/10, 2023 at 21:58 Comment(0)
O
0

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.

Owe answered 24/7, 2018 at 3:42 Comment(0)
I
0

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()
Ironlike answered 22/11, 2018 at 11:39 Comment(0)
P
0

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.

Polyclinic answered 6/2, 2019 at 13:11 Comment(0)
R
0

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.

Rowles answered 9/11, 2019 at 16:49 Comment(0)
P
0

The developers are investigating this problem, tracked in PYTHON-961. You may be able to work around the issue by running mongod.exe manually and monitoring it. This issue arises when the console freezes and you can hit the enter if the mongod console is got stuck. This is the simplest solution for now until the developers fix this bug.

Pyaemia answered 4/7, 2020 at 9:7 Comment(0)
B
0

I ran into the same issue during development. It was due to the fact that mongodb wasn't running on my local machine (sudo systemctl restart mongod to get mongodb running on Ubuntu).

Bluff answered 14/7, 2020 at 12:39 Comment(0)
M
0

I faced the same error on windows and I just started the MongoDB service

  • open services ctrl+R then type services.msc then Enter
Mawkish answered 6/1, 2021 at 21:37 Comment(0)
N
0

For my case I only set my ip allow list 0.0.0.0 allow anywhere but you can set your ip using "what is my ip" and copy paste it to network access > add ip

Nimocks answered 4/7, 2021 at 3:18 Comment(0)
H
0

I have been struggling with same problem. Read and either insert did not work at all failed with ServerSelectionTimeoutError.

I have been using pymongo==3.11.4 on Ubuntu 18.04 LTS. Tried use connect=False, pass extra ?ssl=true&ssl_cert_reqs=CERT_NONE options to my connection string and other suggestions listed above. In my case they didn't work.

Finally simple tried to upgrade to pymongo==3.12.1 and connection started to work without passing connect=false, and other extra arguments suggested.

    login = '<USERNAME>'
    password = '<PASSWORD>'
    host = '*.mongodb.net'
    db = '<DB>'
    uri = f'mongodb+srv://{login}:{password}@{host}/{db}?retryWrites=true&w=majority'
    client = MongoClient(uri, authsource='admin')#, connect=False)
    collection = client.db.get_collection('collection_name')
    # t = collection.find_one({'hello': '1'})
    t = collection.insert_one({'hello': '2'})
    print(t)
Hollingsworth answered 11/11, 2021 at 5:3 Comment(0)
T
0

I had this issue today. I managed to deal with it by:

installing dnspython library > going to MongoDB webpage > signing in > security > network access > add IP address > adding the IP address from where my request comes from.

Hope this could help someone.

Terribly answered 3/12, 2021 at 18:57 Comment(0)
B
0

Make sure you entered the user password, not the MongoDB account password. I encountered similar issue. In my case, I mistakenly entered the MongoDB account password instead of the user password.

Buxtehude answered 12/12, 2021 at 7:16 Comment(1)
Such cases related to the configuration of MongoDB. Better don't use system users for non system applications. Cause of the problem of the current question is not an authentication subsystem. For a first look problem is inside of a connectivity(configuration of a router, firewall, VPN or within provider issues), also possibly in a security layout of the database hosting provider. Maybe the hoster uses a non standard TCP port.Sour
D
0

I had the same issue..the code that was working perfectly fine 2 minutes before gave this error. I was looking for solutions over google for about 30 minutes and it automatically got fixed. The problem could be my home internet connection. Just a guess but if you haven't made any changes to the code or any other config file best to wait for sometime and retry.

Decompose answered 28/1, 2022 at 10:40 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewIllboding
R
-1

This has been fixed in PyMongo with this pull_request.

Rosie answered 16/3, 2016 at 16:26 Comment(2)
I just upgraded from pymongo 2.6.2 to 3.2.2 and am getting this error: ServerSelectionTimeoutError: No servers found yet. Running MongoDB 3.2.5, Python 2.7, Bottle 0.12.9 on Ubuntu 16.04. Is the fix in a version other than 3.2.2?Aby
This is not a fix, this PR simply enables the workaround mentioned in the accepted answer: https://mcmap.net/q/264869/-why-is-pymongo-3-giving-serverselectiontimeouterrorBrazen
L
-1

This problem solved when I just toggled the MongoDB in the services to running which was stopped previously.mongodb service portion

Lauds answered 12/1, 2021 at 7:9 Comment(0)
S
-2
  • 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/

Scrobiculate answered 29/5, 2018 at 5:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.