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
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:
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.
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: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.