I am building an application using MongoDB Atlas as the database and Django REST Framework on the backend.
I'm trying to initiate the connection between the two, however, I keep getting this error back:
Error connecting to MongoDB: 'ag-lfgx9hg-shard-00-02.glqs5bt.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992),'ag-lfgx9hg-shard-00-02.glqs5bt.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992),'ag-lfgx9hg-shard-00-02.glqs5bt.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992), Timeout:
30s, Topology Description: <TopologyDescription id: 63d06b0f8f66929906b7c17a, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription (''ag-lfgx9hg-shard-00-02.glqs5bt.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect(''ag-lfgx9hg-shard-00-02.glqs5bt.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)')>, <ServerDescription ('ag-lfgx9hg-shard-00-02.glqs5bt.mongodb.net, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(''ag-lfgx9hg-shard-00-02.glqs5bt.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)')>, <ServerDescription (''ag-lfgx9hg-shard-00-02.glqs5bt.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ag-lfgx9hg-shard-00-02.glqs5bt.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)')>]>
I don't understand how this issue is arising as MongoDB Atlas is supposed to deal with issuing the SSL certificate and I've watched multiple videos that appear to be doing the same thing I am. Here is my code:
import pymongo
conn_str = "mongodb srv://<username>:<password>@cluster0.mongodb.net/<database>?retryWrites=true&w=majority"
# set a 5-second connection timeout
client = pymongo.MongoClient(conn_str)
try:
print(client.server_info())
except Exception:
print("Unable to connect to the server.")
I have replaced my actual connection string with a generic one for security so assume that this is correct
Any help would be greatly appreciated
CodePudding user response:
To resolve this issue I followed the steps below as outlined in this article: https://www.linkedin.com/pulse/ssl-certificateverifyfailed-while-python-tried-retrieve-sanjeev-kumar
Step 1. Download Pem file from this link https://letsencrypt.org/certs/lets-encrypt-r3.pem
Step 2. Rename the file from .pem to .cer
Step 3. And install the cert file to your machine where python program executes.
Step 4. Restart the machine if required and re-run your program.
it will resolved SSL Issues.
Note - a comment on the article said that it did not work for Mac, I cannot confirm this as I use Windows but should be kept in mind for Mac users
CodePudding user response:
Try adding one of these Params to the conn string:
ssl=false
tlsAllowInvalidCertificates=true