Error: queryTxt ETIMEOUT when connecting to MongoDb Atlas using mongoose

Viewed 46333

Im trying to connect my mongoose with my MongoDB Atlas Cluster. It currently has no database or anything but whenever I try:

mongoose.connect( uri || 'mongodb://localhost/test',options)
.then(()=>{
    console.log("Connected to the Database. Yayzow!");
})
.catch(err => {
    console.log(err);
});

I get this message:

Error: queryTxt ETIMEOUT cluster0-ghis2.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:202:19) {
errno: 'ETIMEOUT',
  code: 'ETIMEOUT',
  syscall: 'queryTxt',
  hostname: 'cluster0-ghis2.mongodb.net'
}

My dependencies:

  "dependencies": {
    "express": "^4.17.1",
    "mongodb": "^3.5.7",
    "mongoose": "^5.9.14"
  }

I've whitelisted all IPs as off now (0.0.0.0/0). Also I've made sure my firewall is disconnected and so is my anti-virus. I made my friend check it out too and it did work for him but not me

Any help is appreciated!

EDIT

Solved the error by changing my DNS to Google's Public DNS! If anyone get this error just make sure to change your DNS. Throwing a link for the same here

10 Answers

The error because of the version of you node and npm, to remove this error you just need to change you connection String by going to Connect and connect to Application

  1. change the version to 2.2.122 or later
  2. copy String and paste then try to connect it again. as shown in Picture enter image description here

You need to add &ssl=true in your url, and your complete url should look like this

mongodb+srv://<user>:<password>@********.mongodb.net/<db name>?authSource=admin&compressors=zlib&retryWrites=true&w=majority&ssl=true

to solve this issue make sure to choose older version of node (2.2.12) or later

enter image description here

and then make sure to add your ip address whitelisted

enter image description here

Changing DNS to 8.8.8.8 works for me.

Before changing my DNS, I tried to reinstall mongoose, check my connection, and turn off my vpn. None of them worked.

Only switching from my mobile hotspot to fiber connection worked. I have no clue how that happened , it solved the issue. (P.S. I had whitelisted my IP when connected to mobile hotspot, so this reasoning is eliminated)

I suspect that several of these answers really come down to the same thing - the network needs to be reset. I just had an odd experience - I had to different apps running on different ports but both accessing the same mongo instance. One had the connection error, the other did not. I rebooted and both were fine going forward.

The other scenario that caused this is an instance where I did not have an async / await around the mongoose call.

I turned my wi-fi off and on again and it worked

Please check your connection string. The password must be a encoded one not as a plain text.

In my case problem was in Database Network Access. I should choose my current IP address instead of ALLOW ACCESS FROM ANYWHERE

Ensure your Connection URL is ok. In .env file(one for environment variables), remove the double quotes around the connection string. Keep it: URL = mongodb+srv://..............

Related