Hello. I’m connecting for the first time with Python on Ubuntu 22.04 to the Atlas mongodb. However, I am getting the following error regarding DNS.
I saw some similar threads, but I couldn’t solve my problem. Could you help me please?
from pymongo import MongoClient
import dns.resolver
def get_database():
Provide the mongodb atlas url to connect python to mongodb using pymongo
CONNECTION_STRING = “mongodb+srv://projegui:@clusterak.mongodb.net/myFirstDatabase”
Create a connection using MongoClient. You can import MongoClient or use pymongo.MongoClient
client = MongoClient(CONNECTION_STRING)
Create the database for our example (we will use the same database throughout the tutorial
return client[‘myFirstDatabase’]
This is added so that many files can reuse the function get_database()
if name == “main”:
Get the database
dbname = get_database()
ERROR:
/usr/bin/python3.10 /home/rodrigo/opt/gui/pymongo_get_database.py
Traceback (most recent call last):
File “/usr/local/lib/python3.10/dist-packages/pymongo/srv_resolver.py”, line 89, in _resolve_uri
results = _resolve(
File “/usr/local/lib/python3.10/dist-packages/pymongo/srv_resolver.py”, line 43, in _resolve
return resolver.resolve(*args, **kwargs)
File “/usr/local/lib/python3.10/dist-packages/dns/resolver.py”, line 1368, in resolve
return get_default_resolver().resolve(
File “/usr/local/lib/python3.10/dist-packages/dns/resolver.py”, line 1190, in resolve
(request, answer) = resolution.next_request()
File “/usr/local/lib/python3.10/dist-packages/dns/resolver.py”, line 691, in next_request
raise NXDOMAIN(qnames=self.qnames_to_try, responses=self.nxdomain_responses)
dns.resolver.NXDOMAIN: The DNS query name does not exist: _mongodb._tcp.clusterak.mongodb.net.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/home/rodrigo/opt/gui/pymongo_get_database.py”, line 19, in
dbname = get_database()
File “/home/rodrigo/opt/gui/pymongo_get_database.py”, line 10, in get_database
client = MongoClient(CONNECTION_STRING)
File “/usr/local/lib/python3.10/dist-packages/pymongo/mongo_client.py”, line 736, in init
res = uri_parser.parse_uri(
File “/usr/local/lib/python3.10/dist-packages/pymongo/uri_parser.py”, line 542, in parse_uri
nodes = dns_resolver.get_hosts()
File “/usr/local/lib/python3.10/dist-packages/pymongo/srv_resolver.py”, line 121, in get_hosts
_, nodes = self._get_srv_response_and_hosts(True)
File “/usr/local/lib/python3.10/dist-packages/pymongo/srv_resolver.py”, line 101, in _get_srv_response_and_hosts
results = self._resolve_uri(encapsulate_errors)
File “/usr/local/lib/python3.10/dist-packages/pymongo/srv_resolver.py”, line 97, in _resolve_uri
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: The DNS query name does not exist: _mongodb._tcp.clusterak.mongodb.net.
Process finished with exit code 1