Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyMongo [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate

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.

like image 294
MaxwellN Avatar asked Jun 24 '21 23:06

MaxwellN


People also ask

How to fix certificate verify failed error in Python?

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.

How to fix unable to get local issuer certificate error?

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.

What is certificate_verify_failed and how to fix 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.

How to fix certificate verify failed on Mac OS X?

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.


2 Answers

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.

like image 177
MaxwellN Avatar answered Oct 16 '22 08:10

MaxwellN


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
 
like image 20
chamod rathnayake Avatar answered Oct 16 '22 09:10

chamod rathnayake