如何使用PyMongo连接MongoDB Atlas?证书过期报错求助
连接MongoDB Atlas时SSL证书过期问题的解决方案
问题详情
尝试用Python连接MongoDB Atlas失败,使用的代码如下(真实密码已替换为password):
import pymongo client = pymongo.MongoClient("mongodb+srv://ben:password@footballstatistics2022.fgh93gq.mongodb.net/international_football?retryWrites=true&w=majority") print(client.list_database_names()) db = client.international_football print(db.list_collection_names())
运行后触发的错误:
Traceback (most recent call last): File "C:\Users\bench\Documents\national-football-predictor\database\database_access.py", line 10, in <module> print(client.list_database_names()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\bench\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymongo\mongo_client.py", line 1867, in list_database_names return [doc["name"] for doc in self.list_databases(session, nameOnly=True, comment=comment)] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\bench\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymongo\mongo_client.py", line 1840, in list_databases res = admin._retryable_read_command(cmd, session=session) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\bench\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymongo\database.py", line 849, in _retryable_read_command return self.__client._retryable_read(_cmd, read_preference, session) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\bench\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymongo\_csot.py", line 105, in csot_wrapper return func(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\bench\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymongo\mongo_client.py", line 1441, in _retryable_read server = self._select_server(read_pref, session, address=address) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\bench\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymongo\mongo_client.py", line 1257, in _select_server server = topology.select_server(server_selector) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\bench\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymongo\topology.py", line 272, in select_server server = self._select_server(selector, server_selection_timeout, address) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\bench\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymongo\topology.py", line 261, in _select_server servers = self.select_servers(selector, server_selection_timeout, address) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\bench\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymongo\topology.py", line 223, in select_servers server_descriptions = self._select_servers_loop(selector, server_timeout, address) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\bench\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymongo\topology.py", line 238, in _select_servers_loop raise ServerSelectionTimeoutError( pymongo.errors.ServerSelectionTimeoutError: ac-copgz5h-shard-00-00.fgh93gq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992),ac-copgz5h-shard-00-01.fgh93gq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992),ac-copgz5h-shard-00-02.fgh93gq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992), Timeout: 30s, Topology Description: <TopologyDescription id: 6390deeb7f6561d07d0fe2d1, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('ac-copgz5h-shard-00-00.fgh93gq.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-copgz5h-shard-00-00.fgh93gq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)')>, <ServerDescription ('ac-copgz5h-shard-00-01.fgh93gq.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-copgz5h-shard-00-01.fgh93gq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)')>, <ServerDescription ('ac-copgz5h-shard-00-02.fgh93gq.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-copgz5h-shard-00-02.fgh93gq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)')>]>
解决步骤
错误核心是SSL证书过期导致验证失败,按以下顺序尝试解决:
更新依赖库
旧版本pymongo可能内置过期根证书,执行命令更新:pip install --upgrade pymongo dnspythondnspython是SRV连接字符串必需的依赖,需同步更新。
手动指定根证书(更新后仍失效时)
下载MongoDB官方根证书,连接时指定证书路径:import pymongo client = pymongo.MongoClient( "mongodb+srv://ben:password@footballstatistics2022.fgh93gq.mongodb.net/international_football?retryWrites=true&w=majority", tlsCAFile="/本地路径/globalroot.pem" ) print(client.list_database_names()) db = client.international_football print(db.list_collection_names())替换
/本地路径/globalroot.pem为证书实际存储路径。校准系统时间
系统时间与真实时间偏差过大也会触发证书验证失败,同步Windows系统时间到互联网服务器。检查Atlas网络权限
顺带确认Atlas控制台的网络访问设置,确保当前IP已加入允许列表,测试环境可临时设置为允许所有IP。
内容的提问来源于stack exchange,提问作者ben_11
相关产品推荐
相关产品推荐

