繁体   English   中英

无法连接到 mongodb,ssl 错误,超时错误

[英]Unable to connect to mongodb, ssl error, timeout errors

我正在尝试使用 pymongo 连接到我的云 mongodb 数据库。 当我尝试运行基本插入时,我遇到了 ssl 证书错误的问题。

我试过设置 ssl=False 但这只会导致 ServerSelectionTimeoutError。 我试过在其他人的计算机上运行代码,它运行得非常好并且插入正确。

from pymongo import MongoClient

client = MongoClient("mongodb+srv://thejordanchoi:sasha@testcluster-0rbdo.mongodb.net/test?retryWrites=true&w=majority", ssl=False)

db = client['testdatabase']

collection = db['testcollection']

testdata = {
    'name' : 'ethan',
    'age' : 20
}

test_data_id = collection.insert_one(testdata).inserted_id
print(test_data_id)

print('eof')

我在 mac 上建立连接时遇到同样的错误。 按照下面提到的步骤来解决这个问题:

  1. 执行下面提到的命令。 它基本上删除现有文件或链接并创建符号链接到 certifi 证书包

    /Applications/Python\\ 3.7/Install\\ Certificates.command

  2. 将您的 IP 地址添加到地图集中的 IP 白名单。

1) 如果您收到pymongo.errors.ServerSelectionTimeoutError: connection closed,ip not in whitelist,ip not in whitelist错误,则将您的 IP 地址添加到地图集中的 IP 白名单。

在此处输入图片说明

2) 不要使用ssl=False参数。 这导致了pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed错误。

删除ssl=False参数后,如果出现错误pymongo.errors.OperationFailure: bad auth Authentication failed. 然后按照第 2 步操作。

2) 确保用户thejordanchoitestdatabase具有写入权限。

通过上述两个更改,我能够使用您的代码片段成功编写。

MongoDB [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获得本地颁发者证书 (_ssl.c:1123)')

这个错误是专门针对 Mac 用户的,因为 mongoDB 的客户端 ID 在 Mac 和 Windows 上是不同的。

clientMAC = MongoClient('mongodb+srv://**namehere**:**namehere**@cluster0.0osjv.mongodb.net/<dbname>?ssl=true&ssl_cert_reqs=CERT_NONE')
db = clientMAC.get_database('**databasename**')

在 Mac 上使用上述客户端 ID,仅编辑粗体字(namehere 和 databasename),不应显示 ssl 错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM