Can't connect to Atlas on MacOS

Hello
I have an issue when trying connect to Atlas cluster on MacOS. On linux all work fine. Here is how i installed pymongo:

pip3 install pymongo

after I got the error, i additionally executed:

pip3 install dnspython
pip3 install pymongo[tls]

Here is error:

s = MongoClient(“mongodb+srv://xxx:yyy@cluster0.myfdu.mongodb.net”)
print(s[‘data’][‘test’].find_one())
Traceback (most recent call last):
File “”, line 1, in
File “venv/lib/python3.8/site-packages/pymongo/collection.py”, line 1319, in find_one
for result in cursor.limit(-1):
File “venv/lib/python3.8/site-packages/pymongo/cursor.py”, line 1207, in next
if len(self.__data) or self._refresh():
File “venv/lib/python3.8/site-packages/pymongo/cursor.py”, line 1100, in _refresh
self.__session = self.__collection.database.client._ensure_session()
File “venv/lib/python3.8/site-packages/pymongo/mongo_client.py”, line 1816, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File “venv/lib/python3.8/site-packages/pymongo/mongo_client.py”, line 1766, in __start_session
server_session = self._get_server_session()
File “venv/lib/python3.8/site-packages/pymongo/mongo_client.py”, line 1802, in _get_server_session
return self._topology.get_server_session()
File “venv/lib/python3.8/site-packages/pymongo/topology.py”, line 490, in get_server_session
self._select_servers_loop(
File “venv/lib/python3.8/site-packages/pymongo/topology.py”, line 215, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: cluster0-shard-00-00.myfdu.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123),cluster0-shard-00-02.myfdu.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123),cluster0-shard-00-01.myfdu.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123), Timeout: 30s, Topology Description: <TopologyDescription id: 6012d175aa7e4741cc6ef1a0, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription (‘cluster0-shard-00-00.myfdu.mongodb.net’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(‘cluster0-shard-00-00.myfdu.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)’)>, <ServerDescription (‘cluster0-shard-00-01.myfdu.mongodb.net’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(‘cluster0-shard-00-01.myfdu.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)’)>, <ServerDescription (‘cluster0-shard-00-02.myfdu.mongodb.net’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(‘cluster0-shard-00-02.myfdu.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)’)>]>

Query work fine if add to connection tlsAllowInvalidCertificates=True. But that is bad solution.
Is it any issues with pymongo and MacOS, or may by i missed some dependency?

Thank you in advance

Run the following script which comes with Python when installed from python.org:

$ cd '/Applications/Python 3.8'
$ ./Install\ Certificates.command
 -- pip install --upgrade certifi
Requirement already up-to-date: certifi in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (2020.12.5)
WARNING: You are using pip version 20.2.3; however, version 21.0 is available.
You should consider upgrading via the '/Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8 -m pip install --upgrade pip' command.
 -- removing any existing file or link
 -- creating symlink to certifi certificate bundle
 -- setting permissions
 -- update complete

If that does not resolve the issue then try installing certifi:

pip3 install certifi
2 Likes

I still have the issue after updating certifi. Current version is 2020.12.05.
Is it anything else that i can try to do?

Try this:

s = MongoClient(“mongodb+srv://xxx:yyy@cluster0.myfdu.mongodb.net”, tlsCAFile=certifi.where())

https://pymongo.readthedocs.io/en/stable/examples/tls.html#specifying-a-ca-file

1 Like

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