skip to Main Content

I want to use MongoDB with Python, but every time I try to access/connect to my database or do some action, it doesn’t work, and an error is displayed in the terminal.

My code is as follows:

import pymongo

myclient = pymongo.MongoClient('mongodb+srv://******:<**************>@cluster0.dr6egzq.mongodb.net/')

mydb = myclient['mydatabase']
mycol = mydb["customers"]

mydict = { "name": "John", "address": "Highway 37" }

x = mycol.insert_one(mydict)

print(x)

And the error I’m getting is:

Traceback (most recent call last):
    File "C:UsersblackprojectpythonprojectvenvLibsite-packagestestingMongo.py", line 11, in <module>
    x = mycol.insert_one(mydict)
    File "C:UsersblackprojectpythonprojectvenvLibsite-packagespymongocollection.py", line 628, in insert_one
    self._insert_one(
    File "C:UsersblackprojectpythonprojectvenvLibsite-packagespymongocollection.py", line 569, in _insert_one      
    self.__database.client._retryable_write(acknowledged, _insert_command, session)
    File "C:UsersblackprojectpythonprojectvenvLibsite-packagespymongomongo_client.py", line 1475, in _retryable_write
    with self._tmp_session(session) as s:
    File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0libcontextlib.py", line 135, in __enter__
    return next(self.gen)
    File "C:UsersblackprojectpythonprojectvenvLibsite-packagespymongomongo_client.py", line 1757, in _tmp_session  
    s = self._ensure_session(session)
    File "C:UsersblackprojectpythonprojectvenvLibsite-packagespymongomongo_client.py", line 1740, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
    File "C:UsersblackprojectpythonprojectvenvLibsite-packagespymongomongo_client.py", line 1685, in __start_session
    self._topology._check_implicit_session_support()
    File "C:UsersblackprojectpythonprojectvenvLibsite-packagespymongotopology.py", line 538, in _check_implicit_session_support
    self._check_session_support()
    File "C:UsersblackprojectpythonprojectvenvLibsite-packagespymongotopology.py", line 554, in _check_session_support
    self._select_servers_loop(
    File "C:UsersblackprojectpythonprojectvenvLibsite-packagespymongotopology.py", line 238, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: 
ac-yeuyapn-shard-00-01.dr6egzq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1007),
ac-yeuyapn-shard-00-02.dr6egzq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1007),
ac-yeuyapn-shard-00-00.dr6egzq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1007), 
Timeout: 30s, 
Topology Description: <TopologyDescription id: 6467ddd302fb8613579250bb, 
topology_type: ReplicaSetNoPrimary, 
servers: [
<ServerDescription ('ac-yeuyapn-shard-00-00.dr6egzq.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-yeuyapn-shard-00-00.dr6egzq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1007)')>, 
<ServerDescription ('ac-yeuyapn-shard-00-01.dr6egzq.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-yeuyapn-shard-00-01.dr6egzq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1007)')>, 
<ServerDescription ('ac-yeuyapn-shard-00-02.dr6egzq.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-yeuyapn-shard-00-02.dr6egzq.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1007)')>
]> 

2

Answers


  1. Chosen as BEST ANSWER

    I think it work I needed ISRG Root X1,ISRG Root X2 and Let's Encrypt R3 and needed to remove the '< >' around the password in the connection string

    https://letsencrypt.org/certificates/

    That's the .der for the 1st category to download

    It was explained in https://www.youtube.com/watch?v=dXvK-HZMG0A


  2. Try certifi package is a Python library that provides a set of root certificates for verifying the authenticity of SSL/TLS connection.

    import certifi

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search