mongo-connector
mongo-connector copied to clipboard
[Pymongo] pymongo.errors.ServerSelectionTimeoutError: connection closed
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
I'm having the same problem. Any solutions?
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 :)
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
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" ....
You should add your IP address to MongoDB Network Access. (You can set Network Access on MongoDB GUI.)
@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')>]>
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 :
-
Check your internet connection and VPN net access restriction if any
-
Change the network setting with DNS IP to 8.8.8.8 (google dns)
-
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)
Hi ,
Could you please explain the 2nd point.
- Change the network setting with DNS IP to 8.8.8.8 (google dns)
Thanks.
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!
https://pymongo.readthedocs.io/en/stable/examples/tls.html
Hope it helps! It worked for me by passing "tls=true" in the connection URI.
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
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.
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.