ServerSelectionTimeoutError: <CLUSTER_NAME>.mongodb.net:27017: timed out

Hi!
Im trying to connect to cloud.mongodb.com database with this code:

import pymongo
client = pymongo.MongoClient("mongodb+srv://<USERNAME>:<USER_PASSWORD>@andreytestmdb.oztib01.mongodb.net/")
client.list_database_names()

but i have ServerSelectionTimeoutError: ac-1epbh6r-shard-00-00.oztib01.mongodb.net:27017: timed out

I double checked my user password - correct
I double checked ip list (i have 0.0.0.0/0 (includes your current IP address) and my real IP here)
And i still cant conntect to database (error on client.list_database_names() )

python version 3.10.2
pymongo version 4.1.1

Im in tilt now, i need help with it

I have the same error if i try to connect using certs:

from pymongo import MongoClient

uri = "mongodb+srv://andreytestmdb.oztib01.mongodb.net/?authSource=%24external&authMechanism=MONGODB-X509&retryWrites=true&w=majority"
client = MongoClient(uri,
                     tls=True,
                     tlsCertificateKeyFile='<path_to_certificate>')

db = client['testDB']
collection = db['testCol']
doc_count = collection.count_documents({})
print(doc_count)

Hi Andrey_Zubov,

Just to clarify, is the connection being attempted from the same machine that has it’s IP on the whitelist? I.e. Not a client within VM (with different network configuration(s)) running on a machine.

If so, please try performing the initial basic network connectivity tests and provide the output for the cluster you are having trouble connecting to:

  1. ping one of the hosts (prefereably the PRIMARY):
/// example
ping cluster0-shard-00-00.ze4xc.mongodb.net
  1. telnet to one of the hosts on port 27017:
/// example
telnet cluster0-shard-00-00.ze4cx.mongodb.net 27017

Note: You can find the hostname in the metrics page of your cluster

Additionally, I would recommend to review the Troubleshoot Connection Issues documentation and verify some configurations, such as adding the client’s IP (or IP ranges) to the Network Access List. You may also find the following blog post regarding tips for atlas connectivity useful.

I would also ask if you can try connecting using mongosh or MongoDB Compass to see if the same error occurs. Please let me know the results of this.

Please note, use of the +srv connection string modifier generally automatically sets the tls (or the equivalent ssl ) option to true for the connection. You can override this behavior by explicitly setting the tls (or the equivalent ssl ) option to false with tls=false (or ssl=false ) in the query string. More info on this here.

Regards,
Jason Tran