MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

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 0.0.0.0/32 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))"
True

OpenSSL version

openssl version
OpenSSL 1.1.0h  27 Mar 2018

TLS version

python -c "import requests; print(requests.get('https://www.howsmyssl.com/a/check', verify=False).json()['tls_version'])"
connectionpool.py:979: InsecureRequestWarning: Unverified HTTPS request is being made to host 'www.howsmyssl.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
warnings.warn(TLS 1.3)

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

    ATLAS_CONNECT = r"mongodb+srv://m001student:PASSWORD@sandbox.v16f0.mongodb.net/" \
    r"sample_airbnb?retryWrites=true&w=majority"

    client = pymongo.MongoClient(ATLAS_CONNECT)
    db = client.get_database("sample_airbnb")
    collection = db.get_collection("listingsAndReviews")
    collection.count_documents({})
    print(collection)

Executing results the following exception

File "test_mongo_connect.py", line 35, in main
collection.count_documents({})
File "...pymongo\collection.py", line 1785, in count_documents
return self.__database.client._retryable_read(
File "...pymongo\mongo_client.py", line 1460, in _retryable_read
server = self._select_server(
File "...pymongo\mongo_client.py", line 1278, in _select_server
server = topology.select_server(server_selector)
File "...pymongo\topology.py", line 241, in select_server
return random.choice(self.select_servers(selector,
File "...pymongo\topology.py", line 199, in select_servers
server_descriptions = self._select_servers_loop(
File "...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: 5faf922aa1fe2651f43d4e91, topology_type: ReplicaSetNoPrimary,
servers: [<ServerDescription ('sandbox-shard-00-00.v16f0.mongodb.net', 27017)    server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>,
<ServerDescription ('sandbox-shard-00-01.v16f0.mongodb.net', 27017) server_type:     Unknown, rtt: None, error=AutoReconnect('connection closed')>,
<ServerDescription ('sandbox-shard-00-02.v16f0.mongodb.net', 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.
0.0.0.0/32 notation for allowing all IPs is incorrect.
Correct form is 0.0.0.0/0
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.