본문 바로가기

CS/DB

[MongoDB]pymongo.errors.ServerSelectionTimeoutError

문제

pymongo.errors.ServerSelectionTimeoutError: cluster0-shard-00-00.4bw3y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get issuer certificate (_ssl.c:997),cluster0-shard-00-01.4bw3y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get issuer certificate (_ssl.c:997),cluster0-shard-00-02.4bw3y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get issuer certificate (_ssl.c:997), Timeout: 30s, Topology Description: <TopologyDescription id: 62997a0dfe47f2fad0f27ecb, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('cluster0-shard-00-00.4bw3y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-00.4bw3y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get issuer certificate (_ssl.c:997)')>, <ServerDescription ('cluster0-shard-00-01.4bw3y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-01.4bw3y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get issuer certificate (_ssl.c:997)')>, <ServerDescription ('cluster0-shard-00-02.4bw3y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-02.4bw3y.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get issuer certificate (_ssl.c:997)')>]>

  • mongoDB atlas를 테스트하던 중 에러 발생
    • python code로 atlas에 접근하려 했으나 해당 에러 발생으로 접근이 불가했다.

해결

  • 라이브러리 설치
pip install certifi
  • certifi 임포트
  • MongoClient() 인자로 tlsCAFile=certifi.where() 할당
from pymongo import MongoClient
import certifi
client = MongoClient('mongodb+srv://admin:admin@cluster0.4bw3y.mongodb.net/?retryWrites=true&w=majority', tlsCAFile=certifi.where())

출처

Keep getting ServerSelectionTimeoutError