The DNS query name does not exist

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

Hi @Rodrigo_A_Kartcheski and welcome to the MongoDB community forum!!

I tried the following code in my local environment with Atlas URI and a localhost URI

from pymongo import MongoClient

client = MongoClient("mongodb+srv://cluster0.sqm88.mongodb.net/test")
db = client.get_database('mydatabase')

and it works perfectly fine.
To further understand the error messages , could you help me with a few details:

  1. The complete code snippet and what you are trying to achieve?
  2. The Pymongo version you are using.
  3. Can you confirm, if you are able to connect to the Atlas using the mongosh or compass . If not what error are you observing.

Let us know if you have further queries .

Best Regards
Aasawari

1 Like

Verify you cluster URI since the following is incomplete

You are missing a few letters and/or numbers between clusterk and mongodb.net like sqm88 in

But yours will be different from sqm88.

Hello, Aasawari. I’m trying to access mongodb Atlas to create some databases and collections. Basic Python code operations

My pymongo version: pymongo==4.3.3

By mongodb Atlas I was able to connect perfectly.

I tried the following exact string through Pycharm. I received the message “Process terminated with exit code 0”

from pymongo import MongoClient

client = MongoClient("mongodb+srv://projegui:a1b2c3@clusterak.mp9ylv1.mongodb.net/test")
db = client.get_database('myFirstDatabase')