我有一个巨大的烧瓶应用程序,我使用来自flask_pymongo的flask_pymongo类进行MongoDB操作。我的问题是在开发环境。
我的config.py中有这样一个config.py:
MONGO_URI = "mongodb+srv://username:password@cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority"我的应用程序中的用法如下:
# This is how I have initialized it in '__init__.py'
from flask_pymongo import PyMongo
mongo = PyMongo(app)
# This is how I access collections in the specified DB
document = mongo.db[collection_name].find() # Throws the below error直到几天前,我从零开始重新安装Windows和Python时,这一切都很好。现在,相同的代码引发以下错误:
pymongo.errors.ServerSelectionTimeoutError: cluster-name-shard-00-01.pihvl.gcp.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate但是使用MongoClient而不是PyMongo效果很好,如下所示:
# Using MongoClient and setting 'ssl_cer_reqs'
from flask_pymongo import MongoClient
import ssl
mongo = MongoClient(app.config['MONGO_URI'], ssl_cert_reqs=ssl.CERT_NONE)
# This works just fine
document = mongo[db_name][collection_name].find()问题
当我重新安装所有东西时,PyMongo发生了什么变化?我不想仅仅因为有100多个端点而使用MongoClient,现在这是一个噩梦,而且我更喜欢PyMongo,因为我不必在每个查询中指定db_name,它是从MONGO_URI获得的。
我缺少了什么,如何使它与PyMongo一起工作?是否有方法在使用ssl_cert_reqs时设置PyMongo
发布于 2021-08-23 22:35:23
的重要性:只有当您在开发环境中面临这个问题时才这样做,
我所要做的就是将&ssl=true&ssl_cert_reqs=CERT_NONE添加到我的Development Configuration中的MONGO_URI中,这样现在我的uri将从:
mongodb+srv://username:password@cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority
至:
mongodb+srv://username:password@cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE
发布于 2021-08-20 20:35:29
证书链中的某些内容将发生更改。pymongo有关于如何解决此问题的特定文档:
https://pymongo.readthedocs.io/en/stable/examples/tls.html#troubleshooting-tls-errors
发布于 2021-11-11 15:55:02
pip安装证书
import pymongo
import certifi
myclient = pymongo.MongoClient(CONNECTION_STRING, tlsCAFile=certifi.where())
...https://stackoverflow.com/questions/68865953
复制相似问题