I'm using Python 3.9.5 and PyMongo 3.11.4. The version of my MongoDB database is 4.4.6. I'm using Windows 8.1
I'm learning MongoDB and I have a cluster set up in Atlas that I connect to. Whenever I try to insert a document into a collection, a ServerSelectionTimeoutError
is raised, and inside its parentheses there are several [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
.
Troubleshooting TLS Errors in the PyMongo docs weren't too much help as they only provided tips for Linux and macOS users.
It's worth mentioning that if I set tlsAllowInvalidCertificates=True
when initializing my MongoClient
, everything works fine. That sounds insecure, and while I am working on a small project, I would still like to develop good habits and not override any security measures in place, so I'm hoping there is an alternative to that.
From all the searching I've done, I'm guessing that I'm missing certain certificates, or that Python can't find them. I've looked into the certifi
package, but this part of the docs makes it seem that should only be necessary if I'm using Python 2.x, which I'm not.
So yeah, I'm kind of stuck right now.
1. Fix Certificate Verify Failed: Unable To Get Local Issuer Certificate Error Steps. Open mac os finder, then click Applications —> Python 3.7 folder to expand it. Your python may have a different version. You can find the Install Certificates.command program in the Python 3.7 folder.
Fix Certificate Verify Failed: Unable To Get Local Issuer Certificate Error Steps. Open mac os finder, then click Applications —> Python 3.7 folder to expand it. Your python may have a different version. You can find the Install Certificates.command program in the Python 3.7 folder. Double click the Install Certificates.command file to run it.
How to fix certificate_verify_failed? SSL certificate_verify_failed errors typically occur as a result of outdated Python default certificates or invalid root certificates. We will cover how to fix this issue in 4 ways in this article.
1. Fix Certificate Verify Failed: Unable To Get Local Issuer Certificate Error Steps. Open mac os finder, then click Applications —> Python 3.7 folder to expand it.
Well, I eventually decided to install certifi
and it worked.
client = MongoClient(CONNECTION_STRING, tlsCAFile=certifi.where())
Wish the docs were a bit clearer on this, but maybe I just didn't look hard enough.
In Flask server I solved by using:
import certifi
app = Flask(__name__)
app.config['MONGO_URI'] =
'mongodb+srv://NAME:<PWD><DBNAME>.9xxxx.mongodb.net/<db>? retryWrites=true&w=majority'
mongo = PyMongo(app,tlsCAFile=certifi.where())
collection_name = mongo.db.collection_name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With