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://”

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()

/usr/bin/python3.10 /home/rodrigo/opt/gui/
Traceback (most recent call last):
File “/usr/local/lib/python3.10/dist-packages/pymongo/”, line 89, in _resolve_uri
results = _resolve(
File “/usr/local/lib/python3.10/dist-packages/pymongo/”, line 43, in _resolve
return resolver.resolve(*args, **kwargs)
File “/usr/local/lib/python3.10/dist-packages/dns/”, line 1368, in resolve
return get_default_resolver().resolve(
File “/usr/local/lib/python3.10/dist-packages/dns/”, line 1190, in resolve
(request, answer) = resolution.next_request()
File “/usr/local/lib/python3.10/dist-packages/dns/”, 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:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/rodrigo/opt/gui/”, line 19, in
dbname = get_database()
File “/home/rodrigo/opt/gui/”, line 10, in get_database
client = MongoClient(CONNECTION_STRING)
File “/usr/local/lib/python3.10/dist-packages/pymongo/”, line 736, in init
res = uri_parser.parse_uri(
File “/usr/local/lib/python3.10/dist-packages/pymongo/”, line 542, in parse_uri
nodes = dns_resolver.get_hosts()
File “/usr/local/lib/python3.10/dist-packages/pymongo/”, line 121, in get_hosts
_, nodes = self._get_srv_response_and_hosts(True)
File “/usr/local/lib/python3.10/dist-packages/pymongo/”, line 101, in _get_srv_response_and_hosts
results = self._resolve_uri(encapsulate_errors)
File “/usr/local/lib/python3.10/dist-packages/pymongo/”, line 97, in _resolve_uri
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: The DNS query name does not exist:

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://")
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

1 Like

Verify you cluster URI since the following is incomplete

You are missing a few letters and/or numbers between clusterk and 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://")
db = client.get_database('myFirstDatabase')