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.
Is this a DNS/SRV issue with mongodb+srv?
Should I try switching to the non-SRV connection string (mongodb://host1,host2,...)?
Are there network/firewall issues that could cause queryTxt ETIMEOUT?
Is there a Mongoose-specific setting I might be missing?
srvhandling not being installed. I had a similar issue in python space until last year.pymongoalso neededpymongosrvinstalled to usemongo+srvscheme. As of June 2024, new python driver no longer uses it (usesdnspythoninstead)