skip to Main Content

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


  1. You need to provide the Server CA certificate in the connection string

    uri = "mongodb+srv://admin:<password>@cluster0.hkzvmvh.mongodb.net/?retryWrites=true&w=majority&tlsCAFile=isrgrootx1.pem"
    

    You can download the files from https://letsencrypt.org/certificates/

    I think it is this one: https://letsencrypt.org/certs/isrgrootx1.pem

    Login or Signup to reply.
  2. I recommend replacing <password> with your password.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search