【问题标题】:Unable to connect to mongodb, ssl error, timeout errors无法连接mongodb,ssl错误,超时错误
【发布时间】:2019-11-01 16:11:10
【问题描述】:

我正在尝试使用 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')

【问题讨论】:

    标签: python python-3.x mongodb ssl certificate


    【解决方案1】:

    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) 确保用户 thejordanchoi 具有对 testdatabase 的写入权限。

    通过以上两个改动,我可以用你的sn-p代码写成功了。

    【讨论】:

    • 1) 我从来没有真正得到 ip not in whitelist 错误
    • 然后从第 2 步开始尝试
    • 2i) 取出 ssl=False 我得到这个: pymongo.errors.ServerSelectionTimeoutError: testcluster-shard-00-01-0rbdo.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书(_ssl.c:1056),testcluster-shard-00-00-0rbdo.mongodb.net:27017:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败:无法获取本地颁发者证书(_ssl.c :1056),testcluster-shard-00-02-0rbdo.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1056)
    • 2ii) 用户 thejordanchoi 有权访问测试数据库。我认为我的本地机器有问题,因为它在其他机器上运行良好。也许某种SSL问题?我对ssl不太了解,所以不知道从哪里开始调试
    • 我还尝试检查我的 ssl 证书并得到: MacBook-Pro:test thejordanchoi$ openssl verify cert.pem Error opening certificate file cert.pem 4696172140:error:02FFF002:system library:func(4095) :没有这样的文件或目录:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.240.1/libressl-2.6/crypto/bio/bss_file.c:255:fopen('cert.pem ', 'r') 4696172140:error:20FFF002:BIO 例程:CRYPTO_internal:system lib:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.240.1/libressl-2.6/crypto/bio /bss_file.c:257: 无法加载证书
    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:

      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 错误

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-25
        • 2020-11-27
        • 2017-06-29
        • 2010-11-26
        • 1970-01-01
        • 1970-01-01
        • 2017-04-11
        • 2016-09-16
        相关资源
        最近更新 更多