Home > Enterprise >  How do you use pymongo to connect to mongodb atlas
How do you use pymongo to connect to mongodb atlas

Time:12-08

I'm trying to connect to MongoDB Atlas but am having some difficulty, below is my code so far (I've removed my actual password from the MongoDB string hence why it just says password):

import pymongo

client = pymongo.MongoClient("mongodb srv://ben:[email protected]/international_football?retryWrites=true&w=majority")

print(client.list_database_names())

db = client.international_football

print(db.list_collection_names())

I've never connected to MongoDB using Python before so I've bene using some YouTube tutorials to help me but I've had no luck so far.

I am getting the following error:

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)')>]>

Any help would be greatly appreciated

CodePudding user response:

Did you try use another python version? Do you using the newest version of pymongo? Have you added your IP in Atlas? Have you try make another Database?

Else, Try this: pymongo [SSL: CERTIFICATE_VERIFY_FAILED]: certificate has expired on Mongo Atlas

CodePudding user response:

Try changing to tls certificate, this should solve your problem. I add the code below, the only thing I have changed are the last two attributes.

client = pymongo.MongoClient("mongodb srv://ben:[email protected]/international_football?retryWrites=true&w=majority", tls=True, tlsAllowInvalidCertificates=True)

  • Related