3

I'm learning Node.js and just started working with MongoDB.

I'm making a connection with the MongoDB Cluster I've created

const dbURI = 'mongodb+srv://testuser:test1234@nodelearning.fzofb.mongodb.net/mydb?retryWrites=true&w=majority';
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })
    .then((result) => console.log('connected to db'))
    .catch((err) => console.log(err));

When I run it nodemon app I get this error:

Error: querySrv ENOTFOUND _mongodb._tcp.mydb.fzofb.mongodb.net at QueryReqWrap.onresolve [as oncomplete] (node:dns:206:19) { errno: undefined, code: 'ENOTFOUND', syscall: 'querySrv',
hostname: '_mongodb._tcp.mydb.fzofb.mongodb.net' }

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116
  • 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

10 Answers10

12

Also if you have your mongodb Atlas cluster inactive after a while. It could get paused and then when you run a server it returns the error you had.

So another bet is to login to your Atlas account and confirm if it hasn't been paused. Well this option of mine should only be considered after trying the options above.

  • @Bracken How is this not an answer to the question? I got the same error and this solved my problem. It couldn't be any more clear. – avisk Jan 13 '22 at 14:13
  • Apologies, your post does contain an answer, but you've written it in the form of a commentary on the previous answer. Try to write your answers in a self contained manor, as if they might be the top answer. Don't start with "Also". – Bracken Jan 13 '22 at 14:24
5

The error indicates that there is no error in the code. This leaves you with three potential possibilities:

  1. Ensure you have MongoDB installed on your computer.
  2. Make sure you're connected to wifi that is not public.
  3. Make sure you have allowed the IP in network access of MongoDB as shown in the image below:

enter image description here

In my case, I was connected to public wifi in a coworking space. I change my connection to my personal hotspot and it worked.

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116
4

Try disabling VPN if enabled. It helped me.

Ihor
  • 131
  • 1
  • 2
4

You need to convert special characters in connection string password to percent encoding.

I had the same issue with mongodb-compass. For me it was incorrect connection string format. So in my case it required to convert @ symbol in my password to %40 (percent encoding).

You can read more about this in here. and here.

Nipun Ravisara
  • 3,629
  • 3
  • 20
  • 35
3

I had the same issue. I was using the latest srv string. I don't know if this will help you. But using the olderve

mongodb://<username>:<password>@cluster0-shard-00-00.2pznz.mongodb.net:27017,cluster0-shard-00-01.2pznz.mongodb.net:27017,cluster0-shard-00-02.2pznz.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-s5orlv-shard-0&authSource=admin&retryWrites=true&w=majority```
  • This comment helped me to resolve an issue running MongoDB on my localhost in WSL2. By removing `+srv` from the connection string, the connection worked as expected. – gregsonian Feb 01 '22 at 03:32
1

This error happens if the IP Address you try to access the database with is not on the IP Access List. Go to MongoDB Login and under security click on the Network Access and check if your IP is added there.

Kipruto
  • 721
  • 6
  • 16
0

In my case, the problem got solved when I sign in to my MongoDB atlas account.

Himanshu
  • 71
  • 3
0

In my case, I was re-using a string connection from another project. I copied my original string connection from MongoDB site and worked fine.

martigej
  • 25
  • 6
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33044113) – 2A-66-42 Nov 01 '22 at 22:13
0

Most of the answers are mentioning about the network connectivity. Even though there is a rare scenario can make this error. That is the mismatch of mongo URI in the mongodb Compass and the mongo atlas URI. I had the same issue when I initiated with the passwords, the URI was setup there. After that I havent used the mongoDb through compass. After 6 months when I run the project. I got this connection issue. So I try to connect that from the compass. It failed, then I tried in the mongo Atlas, by restarting the paused cluster services. Then I copy the URI and put in the compass and try again to run the project, it worked.

As summarized steps we need to check for this errors are,

  1. Check the URI in the project and the mongo Atlas are same,
  2. Check the network connected to your local IP in mongo atlas settings,
  3. Check whether the cluster is paused due to inactive for 6months. Then reactivate
  4. click connect in the mongo Atlas and run the project
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Abishethvarman V
  • 129
  • 1
  • 1
  • 9
-1

FIX : : previously my cluster password was "chaudhary@786" which contained the special character "@", due to which the compass string connecting compiler got confused hence in the next turn I changed my password to "gghjkl98", i.e without any special characters. and I got connected without any errors.

SOLUTION : password of the cluster should not contain characters like \backslash or @ or any other characters