0
点赞
收藏
分享

微信扫一扫

关于在win下通过pymongo连接到mongodb报错[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate

米小格儿 2022-01-12 阅读 117

最近做项目要用到mongodb

在用python连接的时候出现以下问题:

pymongo.errors.ServerSelectionTimeoutError: cluster0-shard-00-00.llrsd.mongodb.net:
27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate 
has expired (_ssl.c:1131)

google了很久得到原因:

这里说是mongo atlas使用的加密方法过期了

解决方案如下:

1. 下载这个文件

2. 将文件后缀的.pem换成.cer

3. 双击改好后缀的文件会弹出一个窗口,点击下载证书

4. 直接按照默认的安装,然后再重试

5. 原文说装完后要重启电脑,但是我这里不用重启也可以

如果上述方法不行,原文给了另一个解决方案 (这个方案我也没试过):

1. 进入网页

2. 下载 ISRG Root X1, ISRG Root X2 ( Root Certificates), Let’s Encrypt R3 ( Intermediate Certificates)

3. 重启电脑后重试

最后附上我用的code:

from pymongo import MongoClient
import pymongo

def get_database():
    # Provide the mongodb atlas url to connect python to mongodb using pymongo
    CONNECTION_STRING = "mongodb+srv://<user>:<password>@<cluster>.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"

    # Create a connection using MongoClient. You can import MongoClient or use pymongo.MongoClient
    client = MongoClient(CONNECTION_STRING,ssl=True)

    # Create the database for our example (we will use the same database throughout the tutorial
    return client.AVATA_room
    
# This is added so that many files can reuse the function get_database()
if __name__ == "__main__":    
    
    # Get the database
    dbname = get_database()

CONNECTION_STRING那行字符串进入mongodb就能获取:

1. 点cluster旁边的connect

2. 点了之后会跳出一个页面,点connect your application

3. 在Add your connection string into your application code下面复制字符串

原网页链接供参考

举报

相关推荐

0 条评论