skip to Main Content

I’m using the pymongo library to connect to MongoDB Atlas with the following Python code snippet:

I have tried changing my network environment and confirmed my Atlas cluster is operational with no ongoing maintenance that could affect connectivity. Below is the Python code snippet I’m using to establish the connection:

from pymongo import MongoClient, server_api
import certifi

# MongoDB Atlas connection URI
uri = 'mongodb+srv://<username>:<password>@<cluster-address>/?retryWrites=true&w=majority&appName=TaipeiYouBikeHourlyData'

# Use the certifi library to get the path of the CA certificate
ca = certifi.where()

try:
    # Create a MongoClient instance and specify the CA certificate path
    client = MongoClient(uri, server_api=server_api.ServerApi('1'), tlsCAFile=ca)
    
    # Send a ping command to test the connection
    response = client.admin.command('ping')
    print("Successfully connected to MongoDB, response:", response)

except Exception as e:
    print("Error connecting to MongoDB:", e)

I’ve already tried switching networks, confirming my MongoDB Atlas cluster’s status, and ensuring my Python environment uses the latest versions of necessary libraries. However, the issue persists.

Question:

Has anyone encountered similar SSL handshake failures with MongoDB Atlas, and how were you able to resolve it? Any suggestions on troubleshooting steps or configurations I might be missing?

2

Answers


  1. Chosen as BEST ANSWER

    I guess the issues I'm facing are tied to the WiFi network I'm using. The code runs flawlessly at school, but I encounter errors when I try to run it using my home WiFi. Below is the code snippet in question:

    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    client = MongoClient(uri, tls=True, tlsAllowInvalidCertificates=True, tlsCAFile=certifi.where())
    
    try:
        client.admin.command('ping')
        print("Successfully connected to MongoDB!")
    except Exception as e:
        print(f"Connection to MongoDB failed: {e}")
    
    databases = client.list_database_names()
    print("Databases:", databases)
    

    Despite attempting to establish a connection with these settings, I am unable to successfully ping MongoDB from my home network.

    The problem may be related to my home WiFi configuration or network restrictions that differ from those at my school, affecting the SSL handshake or the connection to MongoDB.


  2. Verify it is a tlsCAFile issue or if it is network related. TLS/SSL runs over a transport protocol, such as TCP. Disable the TLS and verify if the connection is established:

    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    client = MongoClient(uri, server_api=server_api, tlsContext=sslContext)
    

    If there is no connection failure now, then your issue appears to incorrect credentials. If credentials are correct and you have the latest version of certifi, then I would check your firewall. MongoDB Atlas runs on AWS and therefore sits inside a VPC. The VPC can have public or private subnets. Even if it is on a public subnet, is the security group open? can you ping 27017?

    There are various causes to this error but do process of elimination.

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