从Python连接到MongoDB Atlas时SSL握手失败

问题描述 投票:0回答:1

我正在使用 pymongo 库通过以下 Python 代码片段连接到 MongoDB Atlas:

我尝试更改我的网络环境,并确认我的 Atlas 集群可以运行,并且没有可能影响连接的持续维护。下面是我用来建立连接的 Python 代码片段:


从 pymongo 导入 MongoClient,server_api 进口证明

MongoDB Atlas 连接 URI

uri = 'mongodb+srv://:@/?retryWrites=true&w=majority&appName=TaipeiYouBikeHourlyData'

使用certifi库获取CA证书的路径

ca = certifi.where()

尝试: # 创建MongoClient实例并指定CA证书路径 客户端 = 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)

例外情况为 e: print("连接 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?
python mongodb handshake
1个回答
0
投票

验证这是一个

tlsCAFile
问题或者是否与网络相关。 TLS/SSL 在传输协议(例如 TCP)上运行。禁用 TLS 并验证连接是否已建立:

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

如果现在没有连接失败,那么您的问题似乎是凭据不正确。如果凭据正确并且您拥有最新版本的 certifi,那么我会检查您的防火墙。 MongoDB Atlas 在 AWS 上运行,因此位于 VPC 内。 VPC 可以具有公共或私有子网。即使是在公有子网上,安全组是否开放?你能ping通27017吗?

导致此错误的原因有多种,但请进行排除过程。

© www.soinside.com 2019 - 2024. All rights reserved.