Getting DNS operation timed out error

this is my code

` "import pymongo
from pymongo import MongoClient
MONGO_URI = ‘mongodb+srv://saadatbbaig:*********@cluster0.rcawijs.mongodb.net/test’

client = MongoClient(MONGO_URI)

try to list the available databases on your cluster

print(client.list_database_names())" `

i have hided the password in uri string for security reason.

SO Here is problem when i run this code it give me this error , i have tried change dns , check out firewell bloackage but nothing worked , here is the error:

"File "C:\Users\Al Ghani Computer\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\srv_resolver.py", line 82, in get_options raise ConfigurationError(str(exc)) pymongo.errors.ConfigurationError: The resolution lifetime expired after 21.221 seconds: Server 192.168.43.235 UDP port 53 answered The DNS operation timed out.; Server 192.168.43.235 UDP port 53 answered The DNS operation timed out.; Server 192.168.43.235 UDP port 53 answered The DNS operation timed out.; Server 192.168.43.235 UDP port 53 answered The DNS operation timed out.; Server 192.168.43.235 UDP port 53 answered The DNS operation timed out.; Server 192.168.43.235 UDP port 53 answered The DNS operation timed out.; Server 192.168.43.235 UDP port 53 answered The DNS operation timed out."

Kindly help me out , i need this to be resolved today,Thank you

Are you able to see the ip from “ping cluster0.rcawijs.mongodb.net” ?

This seems to be your local DNS resolver address and looks like it is not able to get IP address of that server FQDN.

cluster0.rcawijs.mongodb.net is not a public name so some local configuration are needed to access the endpoint. (at least i can’t resolve the name on my mac)

can you please elaborate more , and how i am going to git rid of this?

The cluster is public and correct. An error often made with clusters is to think that they have an IP address. They do not. They have 2 types of DNS records TXT and SRV. The TXT provides connection strings options and the SRV point to the initial hosts to contact for the cluster. These hosts will resolve to IP addresses.

See the DNS records for the said cluster:

;QUESTION
cluster0.rcawijs.mongodb.net. IN ANY
;ANSWER
cluster0.rcawijs.mongodb.net. 60 IN TXT "authSource=admin&replicaSet=atlas-uftgjk-shard-0"
cluster0.rcawijs.mongodb.net. 60 IN SRV 0 0 27017 ac-uk0pdt4-shard-00-00.rcawijs.mongodb.net.
cluster0.rcawijs.mongodb.net. 60 IN SRV 0 0 27017 ac-uk0pdt4-shard-00-01.rcawijs.mongodb.net.
cluster0.rcawijs.mongodb.net. 60 IN SRV 0 0 27017 ac-uk0pdt4-shard-00-02.rcawijs.mongodb.net.

Try again with Google’s 8.8.8.8 or 8.8.4.4.

You might also have the wrong python module for SRV.

If it stills fail with the SRV, try to use the old style using the host provided in the DNS response above.

i did this but didn’t worked

File "C:\Users\Al Ghani Computer\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\srv_resolver.py", line 82, in get_options raise ConfigurationError(str(exc)) pymongo.errors.ConfigurationError: The resolution lifetime expired after 21.606 seconds: Server 8.8.8.8 UDP port 53 answered The DNS operation timed out.; Server 8.8.4.4 UDP port 53 answered The DNS operation timed out.; Server 8.8.8.8 UDP port 53 answered The DNS operation timed out.; Server 8.8.4.4 UDP port 53 answered The DNS operation timed out.; Server 8.8.8.8 UDP port 53 answered The DNS operation timed out.; Server 8.8.4.4 UDP port 53 answered The DNS operation timed out.; Server 8.8.8.8 UDP port 53 answered The DNS operation timed out.; Server 8.8.4.4 UDP port 53 answered The DNS operation timed out.; Server 8.8.8.8 UDP port 53 answered The DNS operation timed out.; Server 8.8.4.4 UDP port 53 answered The DNS operation timed out.
after changing dns got this error

Right, i am able to get those records from online tools. It doesn’t have a A record, so ping doesn’t show anything.

@saadat_baig

Did you check this?

yes i did increased the timeout but still getting same error

When

and

does not work. Make sure you do not

If it still fails.

If it still fails. Change network.

Hello everyone! I know this is a bit old topic but I have some questions for you.
I’m using motor to connect to my db from python, using mongo+srv.
Often all is good and I can connect to my DB, but sometimes it throws the Server 8.8.8.8 UPD port 53 answered The DNS operation timed out. (this usually happens 2/3 times a day).
As you can notice I’m using the Google DNS, I also put my connectTimeoutMS to a big value, sometimes this is useful, and I can connect after a bunch of seconds (sometimes mins), but sometimes also having a big connection timeout isn’t enough.
I’m wondering: for now it’s all good, I’m developing, so no problems, but what will happen when I will put my code into production?

Do you know why? Is there ant way to get rid of this annoying DNS problem once for all?

Thank you in advance

1 Like

We don’t know. You control the prod environment (e.g. network, ), not us.

Just as the msg says, the dns query times out. Why? no idea. maybe some packets are lost. Network is not reliable by nature. that’s why we usually consider P95 case instead of P100, because anything can happen in extreme cases.

Yeah, but this is not an extreme case. I used MongoDB in the past when the srv protocol wasn’t implemented, same network, same DNS and I never had these types of problems.
It seems very strange that sometimes it works and sometimes not.
I’m also using FireBase for other projects and I didn’t notice nothing similar.

If you need some more logs to analyze the problem I can give it to you.
In the meanwhile I will try to host my dev environment on a dedicated server to see if I have the same issues.

We are recently seeing the same issue when testing locally.

Hello there! Has anyone in this thread found anyhting that may explain the reason for this error? We are observing this dns.resolver issue on our current GKE cluster.

  • We are using pymongo 3.13.0 and dnspython 2.4.2

When the driver connects to “mongodb+srv://” connection strings it must perform SRV and TXT lookups via DNS. If those lookups consistently time out that indicates a DNS misconfiguration in the network; perhaps there’s some throttling going on or these DNS queries are being dropped/blocked.

One option that’s always available to workaround these issues is to switch to using the old-style “mongodb://” connection string. You can find this in the Atlas cluster UI, https://www.mongodb.com/docs/guides/atlas/connection-string/, going to “connect your application” and then choosing the oldest driver version (choosing the newer driver version will give you a “mongodb+srv://” connection string).

2 Likes