2

Mongoose connection to MongoDB Atlas fails with queryTxt ETIMEOUT

I’m trying to connect my Node.js app to MongoDB Atlas using Mongoose, but the connection fails with a timeout error.

Here’s the error I get in my terminal:

MongoServerSelectionError: queryTxt ETIMEOUT cluster0.mongodb.net
 {
reason: TopologyDescription {
code: 'ETIMEOUT'
}

My connection code:

const mongoose = require("mongoose");
mongoose.set("strictQuery", true);
mongoose.connect("mongodb+srv://kevinlangat419:[email protected]/",)
.then(() => console.log("MongoDB connected..."))
.catch(err => console.error("Connection error:", err));

What I’ve tried so far:

Whitelisted my IP on Atlas (also tried 0.0.0.0/0).

Tested the URI in MongoDB Compass → works fine.

My endpoint works perfectly in Postman, but writing data fails with this error.

Using the exact connection string from Atlas dashboard.

  1. Is this a DNS/SRV issue with mongodb+srv?

  2. Should I try switching to the non-SRV connection string (mongodb://host1,host2,...)?

  3. Are there network/firewall issues that could cause queryTxt ETIMEOUT?

  4. Is there a Mongoose-specific setting I might be missing?

8
  • I do not know your version of javascript and mongoose but this smacks of srv handling not being installed. I had a similar issue in python space until last year. pymongo also needed pymongosrv installed to use mongo+srv scheme. As of June 2024, new python driver no longer uses it (uses dnspython instead) Commented Aug 19 at 13:53
  • @BuzzMoschetti The code in question is not Python Commented Aug 19 at 18:04
  • Are you sure about the connection string? The project id (a random 7 character string) is missing. Commented Aug 19 at 18:06
  • queryTxt ETIMEOUT is a DNS error indicating the DNS server did not respond in time. Try using the pre-3.6 connection string that starts with mongodb:// Commented Aug 19 at 22:06
  • @Joe Yes that is true. Let me also try that solution. Did a good research about this and the this error can emanate from the following 1:DNS issue or ISP blocking certain ports(maybe port 27017 used by Mongo Atlas is blocked ) 2: Poor Networks(switched between mobile hotspots and Wi-Fi but didn't work) 3: Incorrect URI(doubled checked this a couple of times) Now the solution for me ended up being weird: I updated the mongoose to version 8 and created a new cluster in Atlas. Commented Aug 19 at 22:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.