解決pymongo連接數(shù)據(jù)庫報錯certificate verify failed:certificate has expired
問題
使用 pymongo.MongoClient 建立連接時報錯
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired
之前遇到過一次這個錯誤,解決掉之后今天又遇到一次,記錄一下。
具體的報錯內(nèi)容:
pymongo.errors.ServerSelectionTimeoutError:
xxxxxx.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129),
xxxxxx.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129),
xxxxxx.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129),
Timeout: 30s,
Topology Description: <TopologyDescription id: 631d41e5181176036e30eacc, topology_type: ReplicaSetNoPrimary, servers: [
<ServerDescription ('xxxxxx.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('xxxxxx.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)')>,
<ServerDescription ('xxxxxx.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('xxxxxx.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)')>,
<ServerDescription ('xxxxxx.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('xxxxxx.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)')>
]>
解決
1.安裝 certifi:
pip install certifi
2.連接時加入 tlsCAFile 參數(shù):
import certifi
client = pymongo.MongoClient(
f"mongodb+srv://{db_username}:{db_password}@{db_host}/?retryWrites=true&w=majority",
server_api=ServerApi('1'),
tlsCAFile=certifi.where()
)總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
從Node.js+TypeScript無縫切換到Python的完整過程實錄
隨著技術(shù)的發(fā)展,越來越多的項目需要同時使用多種編程語言進行開發(fā),Python和Node.js都是非常流行的編程語言,分別有著各自的優(yōu)勢和適用場景,這篇文章主要介紹了從Node.js+TypeScript無縫切換到Python的完整過程,需要的朋友可以參考下2026-06-06
Python使用pyinstaller打包含有g(shù)ettext?locales語言環(huán)境的項目(推薦)
最近在用 pyhton 做一個圖片處理的小工具,順便接觸了gettext,用來實現(xiàn)本地化化中英文轉(zhuǎn)換,本文通過一個項目給大家詳細介紹下,感興趣的朋友跟隨小編一起看看吧2022-01-01
Python使用Tenacity一行代碼實現(xiàn)自動重試詳解
tenacity 是一個專為 Python 設(shè)計的通用重試庫,它的核心理念就是用簡單、清晰的方式,為任何可能失敗的操作添加重試能力,下面我們就來看看如何一行代碼實現(xiàn)自動重試吧2025-08-08
跟老齊學(xué)Python之網(wǎng)站的結(jié)構(gòu)
本教程的最終目的就是教會大家如何使用Python制作網(wǎng)站,非常的詳盡,需要的朋友可以參考下2014-10-10

