4

My nodejs app was working fine with mongodb connection and suddenly this error got appeared. Then I tried to connect to mongodb with mongo compass and same error is there. I could not find out any reason for this.

Error: querySrv ESERVFAIL _mongodb._tcp.cluster0.abcd0.mongodb.net
[nodemon] app crashed - waiting for file changes before starting...

Then I changed the mongodb connection url to old url and after that I got this error.

Error: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
[nodemon] app crashed - waiting for file changes before starting...

I have already white listed my ips and my configurations are correct (I double checked). 0.0.0.0/0 (includes your current IP address)

What is the reason for this ?

Thank you.

John Stuart
  • 950
  • 5
  • 11
  • 28
  • Does this answer your question? [How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas?](https://stackoverflow.com/questions/55499175/how-to-fix-error-querysrv-erefused-when-connecting-to-mongodb-atlas) – Tan Nguyen Oct 21 '21 at 11:00

7 Answers7

8

querySrv ESERVFAIL is a DNS error.

This means that your local machine is not able to get a response from your DNS resolver for the SRV record _mongodb._tcp.cluster0.abcd0.mongodb.net (I assume that's not your real hostname, but it will work for an example)

From your local machine, test SRV lookup from a command line, possibly one of these:

nslookup -type=SRV _mongodb._tcp.cluster0.abcd0.mongodb.net
host -t SRV _mongodb._tcp.cluster0.abcd0.mongodb.net

If that fails, feel free to say bad things about your DNS provider.

Then go to the Atlas UI and get the pre-3.6 connection string. It will start with mongodb:// and not mongodb+srv://.

Joe
  • 25,000
  • 3
  • 22
  • 44
4

Joe's identification of the problem is spot on and help me with a resolution. This was fixed for me after adding Google's DNS server (8.8.8.8) to the Wifi settings of my computer.

On MacOS its in Settings > Network > Wi-Fi (select the appropriate network) > Advanced > DNS Then add the DNS Server 8.8.8.8

user3434785
  • 41
  • 1
  • 2
3

I was a windows10 user and I was facing exactly the same problem. I have figure out it's a DNS problem. the following process worked for me

-Open the Control Panel.
-Click Network and Internet.
-Click Network and Sharing Center.
-On the left pane, click Change adapter settings.
-Right-click the network interface connected to the internet, then click Properties.
-Choose Internet Protocol Version 4 (TCP/IPv4).
-Click Properties.
-Click Use the following DNS server addresses.
-Click Advanced.
-Enter 208.67.222.222 and 208.67.220.220 in the DNS server fields.
-Click OK, then click Close.

Check this! if you are non windows 10 user

Nasim_Reja
  • 78
  • 8
2

Stop the server and run again your server and it will solve the problem.

Denis Ouma
  • 63
  • 5
1

So I stumbled on the same issue, but a switched my ISP and it fixed the problem for me. This would be a fast fix, As mentioned earlier by other answers, this is a DNS issue, switching my ISP led to a change in the DNS server address, which was enough.

If the error changes to ETIMEOUT then as noted by @user3434785 add 8.8.8.8 under the DNS as below: enter image description here

Pierre P.
  • 890
  • 1
  • 18
  • 41
imchukwu
  • 21
  • 3
0

Hey Guys!

So i was having this weird error below :(

Error: querySrv ESERVFAIL _mongodb._tcp.cluster0.velr6at.mongodb.net

So what might be causeing this error?

  • make sure the database you trying to create n your mongoDB collections exist for me it was "userDB" that was the issue for me!

enter image description here

mongoose.connect(
    `mongodb+srv://admin-eniola:${process.env.PASSWORD}@cluster0.velr6at.mongodb.net/userDB`
);
  • makes sure you check whatever password you using, it must correlate with your user password not account password!
  • check where your password is stored your program either dotenv or secrets file and make sure it match with your user account password.

Thanks and i hope this solutions works for you as well!

0

If you are on localhost then change the internet dns to google dns

if you are on vps server then change the dns on hosting provider to google dns this worked for me. For IPv4: 8.8.8.8 For IPv6: 2001:4860:4860::8888