ServerSelectionTimeoutError [SSL: CERTIFICATE_VERIFY_FAILED] Trying to understand the origin of the problem

Hello, I am working on a python project using MongoDB for several months now, I had no connect issue until today where I encounter a CERTIFICATE_VERIFY_FAILED problem that looks like this:

pymongo.errors.ServerSelectionTimeoutError: xxxx-shard-00-02.xxxxxx.mongodb.net:00000: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

I browse through the community forums and lucky found the problem:

My original code:

connection = "mongodb+srv://"+ username + ":" + password + "@xxxx.xxxxx.mongodb.net/Configurations?retryWrites=true&w=majority"

client = pymongo.MongoClient(connection)

My new fixed code:

client = pymongo.MongoClient(connection, ssl_ca_certs=certifi.where())

So it had something to do with the cacert.pem.

But I find this problem odd, why today? it has been working for months with no issues, and furthermore, which cacert.pem is the default “client = pymongo.MongoClient(connection)” using? Where is the problem originated from and how do I fix it at its core?

I would very much appreciate it if someone explains this to me.

Thank you.

4 Likes

By default pymongo relies on the operating system’s root certificates.

But I find this problem odd, why today?

It could be that Atlas itself updated its certificates or it could be that something on your OS changed. “certificate verify failed” often occurs because OpenSSL does not have access to the system’s root certificates or the certificates are out of date. For how to troubleshoot see TLS/SSL and PyMongo - PyMongo 4.6.2 documentation

Also please note that “ssl_ca_certs” is deprecated and you should use “tlsCAFile” instead:

client = pymongo.MongoClient(connection, tlsCAFile=certifi.where())
7 Likes

thanks for sharing, currently still working in 2023!

1 Like

thank you it finally worked after adding the tlsCAFile

1 Like

thanks it works with python 3.12
date 4/18/2024