pymongo [SSL:证书验证失败]:Mongo Atlas上的证书已过期

balp4ylt  于 2023-01-09  发布在  Go
关注(0)|答案(4)|浏览(218)

我在Django应用中使用MongoDB(Mongo Atlas)。昨天之前一切都很好。但是今天当我运行服务器时,它在控制台上显示了以下错误

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "c:\users\admin\appdata\local\programs\python\python39\lib\threading.py", line 973, in _bootstrap_inner
    self.run()
  File "c:\users\admin\appdata\local\programs\python\python39\lib\threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
    self.check_migrations()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\django\core\management\base.py", line 486, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
    self.build_graph()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\django\db\migrations\loader.py", line 220, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
    tables = self.connection.introspection.table_names(cursor)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\django\db\backends\base\introspection.py", line 52, in table_names
    return get_names(cursor)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\django\db\backends\base\introspection.py", line 47, in get_names
    return sorted(ti.name for ti in self.get_table_list(cursor)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\djongo\introspection.py", line 47, in get_table_list
    for c in cursor.db_conn.list_collection_names()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\pymongo\database.py", line 880, in list_collection_names
    for result in self.list_collections(session=session, **kwargs)]
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\pymongo\database.py", line 842, in list_collections
    return self.__client._retryable_read(
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\pymongo\mongo_client.py", line 1514, in _retryable_read
    server = self._select_server(
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\pymongo\mongo_client.py", line 1346, in _select_server
    server = topology.select_server(server_selector)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\pymongo\topology.py", line 244, in select_server
    return random.choice(self.select_servers(selector,
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\pymongo\topology.py", line 202, in select_servers
    server_descriptions = self._select_servers_loop(
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\pymongo\topology.py", line 218, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: cluster0-shard-00-02.mny7y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129),cluster0-shard-00-01.mny7y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129),cluster0-shard-00-00.mny7y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129), Timeout: 30s, Topology Description: <TopologyDescription id: 6155f0c9148b07ff5851a1b3, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('cluster0-shard-00-00.mny7y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-00.mny7y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)')>, <ServerDescription ('cluster0-shard-00-01.mny7y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-01.mny7y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)')>, <ServerDescription ('cluster0-shard-00-02.mny7y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-02.mny7y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)')>]>

我使用djongo作为数据库引擎

DATABASES = {
    'default': {
            'ENGINE': 'djongo',
            'NAME': 'DbName',
            'ENFORCE_SCHEMA': False,
            'CLIENT': {
                'host': 'mongodb+srv://username:password@cluster0.mny7y.mongodb.net/DbName?retryWrites=true&w=majority'
            }  
    }
}

应用程序中正在使用以下依赖项

dj-database-url==0.5.0
Django==3.2.5
djangorestframework==3.12.4
django-cors-headers==3.7.0
gunicorn==20.1.0
psycopg2==2.9.1
pytz==2021.1
whitenoise==5.3.0
djongo==1.3.6
dnspython==2.1.0

要解决此错误,应该执行什么操作?

rqcrx0a6

rqcrx0a61#

这是因为Let 's Encrypt使用的根CA(Mongo Atals使用Let' s Encrypt)已于2020年9月30日到期,即“IdentTrust DST根CA X3”。
修复方法是在Windows证书存储区中手动安装“ISRG Root X1”和“ISRG Root X2”根证书,以及“Let 's Encrypt R3”中间证书-指向其官方站点的链接-https://letsencrypt.org/certificates/
从评论中复制:从第一个类别下载.der字段,下载,双击并按照向导进行安装。

lg40wkob

lg40wkob2#

我在我的windows机器上解决了这个问题。它与让我们加密的DST根CA X3证书到期有关。
1.下载https://letsencrypt.org/certs/lets-encrypt-r3.pem
1.重命名文件. pem到. cer
1.双击并安装
1.重新启动电脑
不过,如果不成功
1.转到https://letsencrypt.org/certificates/
1.下载ISRG根X1、ISRG根X2(根证书)、让我们加密R3(中间证书)
1.重新启动电脑

flmtquvp

flmtquvp3#

我在mongoDB社区找到了这个解决方案。下载https://letsencrypt.org/certs/lets-encrypt-r3.pem
1.将.pem文件重命名为.cer
1.双击并安装
1.尝试运行应用程序
您的SSL问题应已解决。
来源:https://www.mongodb.com/community/forums/t/keep-getting-serverselectiontimeouterror/126190/13

xfb7svmp

xfb7svmp4#

MongoDB社区的另一个解决方案。超级简单,对我很有效。您可以在这里阅读完整的解决方案-

https://www.mongodb.com/community/forums/t/keep-getting-serverselectiontimeouterror/126190/

以下是我的成功经验(Windows 11)-

检查虚拟环境中是否安装了dnspython、pymongo和certifici,或者通过以下方式安装它们:

pip install dnspython pymongo certifi

您是否可以使用终端/命令行并运行Python,在Python环境中是否可以输入并运行以下命令(请根据Atlas集群中的用户情况更改密码):

from pymongo import MongoClient
import certifi
s = MongoClient("mongodb+srv://m220student:m220password@cluster0.maiqr.mongodb.net", tlsCAFile=certifi.where())

相关问题