I’m trying to connect to my shared MongoDB cluster, but I keep receiving the following error: SSL handshake failed: ac-ascvkib-shard-00-02.hkzvmvh.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host.
The exact code I’m using is below:
from pymongo.mongo_client import MongoClient
# I replaced <password> with my own password
uri = "mongodb+srv://admin:<password>@cluster0.hkzvmvh.mongodb.net/?retryWrites=true&w=majority"
# Create a new client and connect to the server
client = MongoClient(uri)
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
I’m using Python 3.11 and I’m using pymongo==3.11. I’m on a windows machine, and it seems that the error message gets printed multiple times.
2
Answers
You need to provide the Server CA certificate in the connection string
You can download the files from https://letsencrypt.org/certificates/
I think it is this one: https://letsencrypt.org/certs/isrgrootx1.pem
I recommend replacing
<password>
with your password.