ymongo.errors.ServerSelectionTimeoutError: SSL handshake failed

I’m connected to a VPS (Ubuntu 23.10, Python 3.11.6) over SSH trying to run a python script that connects to my database but after a while of waiting i get an exception pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed

The script works fine on my PC (Arch Linux, Python 3.11.8)

I tried all the workarounds i could find such as:

  • adding ssl_cert_reqs=ssl.CERT_NONE to the client parameters
  • adding tlsCAFile=certifi.where() to the client parameters
  • adding tlsAllowInvalidCertificates=True to the client parameters

Hey @Asparagus9159,

Thanks for reaching out to the MongoDB Community forums :sparkles:

Please provide the full traceback and error message for the exceptions you are experiencing. You can redact sensitive info like host names or user names.

Please also provide the output of the pip list so we can determine if you are using PyOpenSSL or not.

Can you also share what server product (atlas dedicated, atlas free tier) and version (MongoDB 4.0, 5.0, etc) you are using? In addition to it, if you are using MongoDB Atlas, have you whitelisted your IP address of VPS to the cluster?

Looking forward to your response.

Best regards,
Kushagra

Hello

`pip list:
Package Version


aiohttp 3.9.4
aiosignal 1.3.1
attrs 23.2.0
certifi 2024.2.2
cffi 1.16.0
charset-normalizer 3.3.2
discord 2.3.2
discord-protos 0.0.2
discord.py 2.3.2
dnspython 1.16.0
frozenlist 1.4.1
idna 3.7
multidict 6.0.5
pip 23.2
protobuf 5.26.1
pycparser 2.22
pymongo 3.11.0
PyNaCl 1.5.0
requests 2.31.0
selfcord.py 2.1.0a4406+g6e12bbbb
setuptools 68.1.2
tzlocal 5.2
urllib3 2.2.1
yarl 1.9.4
`

Here’s the full traceback:
/home/#####/#####/venv/lib/python3.11/site-packages/pymongo/common.py:781: UserWarning: The value of retrywrites must be 'true' or 'false' warnings.warn(str(exc)) Traceback (most recent call last): File "/home/#####/#####/bot.py", line 53, in <module> cached_collections = client['<collection>'].list_collection_names() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/#####/#####/venv/lib/python3.11/site-packages/pymongo/database.py", line 863, in list_collection_names for result in self.list_collections(session=session, **kwargs)] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/#####/#####/venv/lib/python3.11/site-packages/pymongo/database.py", line 825, in list_collections return self.__client._retryable_read( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/#####/#####/venv/lib/python3.11/site-packages/pymongo/mongo_client.py", line 1460, in _retryable_read server = self._select_server( ^^^^^^^^^^^^^^^^^^^^ File "/home/#####/#####/venv/lib/python3.11/site-packages/pymongo/mongo_client.py", line 1278, in _select_server server = topology.select_server(server_selector) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/#####/#####/venv/lib/python3.11/site-packages/pymongo/topology.py", line 241, in select_server return random.choice(self.select_servers(selector, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/#####/#####/venv/lib/python3.11/site-packages/pymongo/topology.py", line 199, in select_servers server_descriptions = self._select_servers_loop( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/#####/#####/venv/lib/python3.11/site-packages/pymongo/topology.py", line 215, in _select_servers_loop raise ServerSelectionTimeoutError( pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: ###############-00-00.#######.mongodb.net:27017: [Errno 104] Connection reset by peer,SSL handshake failed: ###############-00-02.#######.mongodb.net:27017: [Errno 104] Connection reset by peer,SSL handshake failed: ###############-00-01.#######.mongodb.net:27017: [Errno 104] Connection reset by peer, Timeout: 30s, Topology Description: <TopologyDescription id: 661d6b90088c126cd0556bd1, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('###############-00-00.#######.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: ###############-00-00.#######.mongodb.net:27017: [Errno 104] Connection reset by peer')>, <ServerDescription ('###############-00-01.#######.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: ###############-00-01.#######.mongodb.net:27017: [Errno 104] Connection reset by peer')>, <ServerDescription ('###############-00-02.#######.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: ###############-00-02.#######.mongodb.net:27017: [Errno 104] Connection reset by peer')>]>
I’m using the free tier, Atlas says I’m using version 7.0.8

Yes i have whitelisted the IP

Thanks!