解決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)文章
Python調(diào)用adb命令實現(xiàn)對多臺設(shè)備同時進行reboot的方法
今天小編就為大家分享一篇Python調(diào)用adb命令實現(xiàn)對多臺設(shè)備同時進行reboot的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10使用Python實現(xiàn)簡單的學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細介紹了Python實現(xiàn)學(xué)生成績管理系統(tǒng),使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01python利用不到一百行代碼實現(xiàn)一個小siri
這篇文章主要介紹了關(guān)于python利用不到一百行代碼實現(xiàn)了一個小siri的相關(guān)資料,文中介紹的很詳細,對大家具有一定的參考借鑒價值,需要的朋友們下面來一起看看吧。2017-03-03Python Web框架Flask信號機制(signals)介紹
這篇文章主要介紹了Python Web框架Flask信號機制(signals)介紹,本文介紹Flask的信號機制,講述信號的用途,并給出創(chuàng)建信號、訂閱信號、發(fā)送信號的方法,需要的朋友可以參考下2015-01-01