PyMongo: error connecting to Sandbox Atlas database from M001 course

Good day everyone,

I am working on my first Mongo online course, M001: MongoDB Basics

The course assumes creating Atlas Free Tier database called Sandbox, and populate it with about 300Mb of sample data. Even though I did all exercises related to CLI or Compass, my future work is going to be mostly on developing client applications based on PyMongo, so I tried also connect to Sandbox using the Python script.

First I checked admin credentials and opened cluster for all IPs, adding to whitelist.

My client instance specs: Windows 7, Python 3.8.4, PyMongo 3.11.0

As PyMongo documentation says, if I use Free Tier, I have to check my client app on SNI support, OpenSSL version and TLS version.

SNI check

python -c "import ssl; print(getattr(ssl, 'HAS_SNI', False))"

OpenSSL version

openssl version
OpenSSL 1.1.0h  27 Mar 2018

TLS version

python -c "import requests; print(requests.get('', verify=False).json()['tls_version'])" InsecureRequestWarning: Unverified HTTPS request is being made to host ''. Adding certificate verification is strongly advised. See:
warnings.warn(TLS 1.3)

There’s a code snippet I’m trying to execute

    ATLAS_CONNECT = r"mongodb+srv://" \

    client = pymongo.MongoClient(ATLAS_CONNECT)
    db = client.get_database("sample_airbnb")
    collection = db.get_collection("listingsAndReviews")

Executing results the following exception

File "", line 35, in main
File "...pymongo\", line 1785, in count_documents
return self.__database.client._retryable_read(
File "...pymongo\", line 1460, in _retryable_read
server = self._select_server(
File "...pymongo\", line 1278, in _select_server
server = topology.select_server(server_selector)
File "...pymongo\", line 241, in select_server
return random.choice(self.select_servers(selector,
File "...pymongo\", line 199, in select_servers
server_descriptions = self._select_servers_loop(
File "...pymongo\", line 215, in _select_servers_loop
raise ServerSelectionTimeoutError(

pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 5faf922aa1fe2651f43d4e91, topology_type: ReplicaSetNoPrimary,
servers: [<ServerDescription ('', 27017)    server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>,
<ServerDescription ('', 27017) server_type:     Unknown, rtt: None, error=AutoReconnect('connection closed')>,
<ServerDescription ('', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>]>

I was able to reproduce the exception on other system (macOS), so I don’t think it’s some local misconfiguration.

Is that MongoDB URI really correct?

I solved it.
This was probably my stupidest error. notation for allowing all IPs is incorrect.
Correct form is
Would’ve been nice to have a clue like “access denied”.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.