mongo-connector icon indicating copy to clipboard operation
mongo-connector copied to clipboard

[Pymongo] pymongo.errors.ServerSelectionTimeoutError: connection closed

Open OrlandiniSilva opened this issue 5 years ago • 13 comments

Hello everybody! This is my first post, hope we can find the solution together!

Here in the company I work in we have this mongodb from another team of the same company. I've tried to connect to their db with the login and password they gave to me. Here's a very simple code, that reflect the problem I'm having. When trying to connect I get this error. My final objective will be to extract an entire db in csv format, for our own purposes. It need to be done via python.

Any idea of what's happening?

Thank you for your help.

My Code:

from pymongo import MongoClient
from pprint import pprint

mongodb_uri = "mongodb://<my_login>:<my_password>@<mongodb_path>:27017"
client = MongoClient(mongodb_uri)
db = client.ctox
serverStatusResult = db.command("serverStatus")
pprint(serverStatusResult)

The error:

File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\database.py", line 655, in command read_preference) as (sock_info, slave_ok): File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\contextlib.py", line 112, in enter return next(self.gen) File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1135, in _socket_for_reads server = topology.select_server(read_preference) File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\topology.py", line 226, in select_server address)) File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\topology.py", line 184, in select_servers selector, server_timeout, address) File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\topology.py", line 200, in _select_servers_loop self._error_message(selector)) pymongo.errors.ServerSelectionTimeoutError: connection closed

OrlandiniSilva avatar Jul 18 '19 11:07 OrlandiniSilva

I'm having the same problem. Any solutions?

yan-michael avatar Dec 29 '19 04:12 yan-michael

I'm having the same problem. Any solutions?

https://stackoverflow.com/questions/55005134/app-engine-pymongo-errors-serverselectiontimeouterror-connection-closed-connec

I've faced the same problem when I tried to access my Mongo cluster from another machine. So I followed the steps above and fixed the problem. The solution is adding 0.0.0.0/0 to the IP Whitelist. It's situated here: *Your Mongo cluster* -> Security -> Network Access -> IP Whitelist -> Add IP Address Not sure if it's secure though, but at least fixes problem for kind of little pet-projects :)

dementednoor avatar Jan 08 '20 10:01 dementednoor

I don't want to have to configured this through a GUI. Is it possible to update this in the mongod.conf file? or another place?

I want to be able to configure a Docker container to spin up and able to connect to the db using the forwarded ports

SnowCone161803 avatar Feb 16 '20 17:02 SnowCone161803

I had the same problem but found another way to address this issue. You can just pass network parameter while running docker image and this way docker points to correct localhost. docker run --network="host" ....

Source for this solution

ravneetg avatar Mar 03 '20 04:03 ravneetg

You should add your IP address to MongoDB Network Access. (You can set Network Access on MongoDB GUI.)

Lytua avatar Mar 06 '20 05:03 Lytua

@dementednoor I Am Still Facing The Same Issue After Changing this

Your Mongo cluster -> Security -> Network Access -> IP Whitelist -> Add IP Address

i Made it to 0.0.0.0/0 . But Still I Am Facing The Same issue

Traceback (most recent call last): File "C:\Users\Belgin\Desktop\Python Coding\MongoDB.py", line 8, in print(records.insert_one(personDocument)) File "C:\Users\Belgin\AppData\Roaming\Python\Python38\site-packages\pymongo\collection.py", line 698, in insert_one self._insert(document, File "C:\Users\Belgin\AppData\Roaming\Python\Python38\site-packages\pymongo\collection.py", line 613, in _insert return self._insert_one( File "C:\Users\Belgin\AppData\Roaming\Python\Python38\site-packages\pymongo\collection.py", line 602, in _insert_one self.__database.client._retryable_write( File "C:\Users\Belgin\AppData\Roaming\Python\Python38\site-packages\pymongo\mongo_client.py", line 1497, in _retryable_write with self._tmp_session(session) as s: File "C:\Program Files (x86)\Python38-32\lib\contextlib.py", line 113, in enter return next(self.gen) File "C:\Users\Belgin\AppData\Roaming\Python\Python38\site-packages\pymongo\mongo_client.py", line 1829, in _tmp_session s = self._ensure_session(session) File "C:\Users\Belgin\AppData\Roaming\Python\Python38\site-packages\pymongo\mongo_client.py", line 1816, in _ensure_session return self.__start_session(True, causal_consistency=False) File "C:\Users\Belgin\AppData\Roaming\Python\Python38\site-packages\pymongo\mongo_client.py", line 1766, in __start_session server_session = self._get_server_session() File "C:\Users\Belgin\AppData\Roaming\Python\Python38\site-packages\pymongo\mongo_client.py", line 1802, in _get_server_session return self._topology.get_server_session() File "C:\Users\Belgin\AppData\Roaming\Python\Python38\site-packages\pymongo\topology.py", line 490, in get_server_session self._select_servers_loop( File "C:\Users\Belgin\AppData\Roaming\Python\Python38\site-packages\pymongo\topology.py", line 215, in _select_servers_loop raise ServerSelectionTimeoutError( pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 5f840eb64b798e2b07d97950, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('cluster-shard-00-00.0g9bt.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('cluster-shard-00-01.0g9bt.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('cluster-shard-00-02.0g9bt.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>]>

ibelgin avatar Oct 12 '20 08:10 ibelgin

Like me, lots of people are facing this problem. After having several exploration in internet and lytua's comment above, I find out the solution.

If you are facing issue "pymongo.errors.ServerSelectionTimeoutError: connection closed" while connecting Atlas using pymongo

Do the following :

  1. Check your internet connection and VPN net access restriction if any

  2. Change the network setting with DNS IP to 8.8.8.8 (google dns)

  3. Add IP address ( 0.0.0.0/0) to MongoDB Network Access via 'Network Access' link on MongoDB GUI as below: Your Mongo cluster -> Security -> Network Access -> IP Whitelist -> Add IP Address

You are done! you then successfully run following code, without the above error:

from pymongo import MongoClient
from pprint import pprint

mongodb_uri = "mongodb://<my_login>:<my_password>@<mongodb_path>:27017"
client = MongoClient(mongodb_uri)
db = client.ctox
serverStatusResult = db.command("serverStatus")
pprint(serverStatusResult)

skskcco2o17 avatar Nov 05 '20 07:11 skskcco2o17

Hi ,

Could you please explain the 2nd point.

  • Change the network setting with DNS IP to 8.8.8.8 (google dns)

Thanks.

mohapatramanas avatar Apr 25 '21 08:04 mohapatramanas

Hi guys, I am having the same problem as you.

My app I created with python was working perfectly when added the MongoDB database, but when I deployed to heroku, my app was not working anymore. It raises the error showed above.

I also read that allowing IP address ( 0.0.0.0/0) to MongoDB Network Access it is not very secure... Could you explain a little bit more in depth ? and also @mohapatramanas question related to DNS (point number 2)?

Thank you so much guys, you are awesome!

aitor848 avatar Apr 30 '21 13:04 aitor848

https://pymongo.readthedocs.io/en/stable/examples/tls.html

Hope it helps! It worked for me by passing "tls=true" in the connection URI.

ghost avatar May 22 '21 06:05 ghost

well, I am very late but I hope this will work for others who get stuck at timeout error even after doing 0.0.0.0/0 thing, It worked for me by adding 'ssl_cert_reqs' : 'CERT_NONE' ( I am using djongo ), you can add in connection string this as a parameter--> ssl=true&ssl_cert_reqs=CERT_NONE this is what I referred https://stackoverflow.com/a/58316119/16307140

MihirHundiwala avatar Nov 04 '21 18:11 MihirHundiwala

Hello everybody! This is my first post, hope we can find the solution together!

Here in the company I work in we have this mongodb from another team of the same company. I've tried to connect to their db with the login and password they gave to me. Here's a very simple code, that reflect the problem I'm having. When trying to connect I get this error. My final objective will be to extract an entire db in csv format, for our own purposes. It need to be done via python.

Any idea of what's happening?

Thank you for your help.

My Code:

from pymongo import MongoClient
from pprint import pprint

mongodb_uri = "mongodb://<my_login>:<my_password>@<mongodb_path>:27017"
client = MongoClient(mongodb_uri)
db = client.ctox
serverStatusResult = db.command("serverStatus")
pprint(serverStatusResult)

The error:

File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\database.py", line 655, in command read_preference) as (sock_info, slave_ok): File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\contextlib.py", line 112, in enter return next(self.gen) File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1135, in _socket_for_reads server = topology.select_server(read_preference) File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\topology.py", line 226, in select_server address)) File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\topology.py", line 184, in select_servers selector, server_timeout, address) File "C:\Users\matheus.silva\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\topology.py", line 200, in _select_servers_loop self._error_message(selector)) pymongo.errors.ServerSelectionTimeoutError: connection closed

Did you add uri in mongodb_uri = "mongodb://<my_login>:<my_password>@<mongodb_path>:27017" add it first I got the same when uri was missing.

AnujKondhalkar avatar Jul 30 '22 15:07 AnujKondhalkar

In My case, I was running it in the GitHub action, and This was happening because the environment variable for the connection string was null.

kalilinux-png avatar Sep 25 '23 05:09 kalilinux-png