Fix MongoDB querySrv ECONNREFUSED in Node.js: Atlas DNS/SRV Resolution Guide
A practical, production-ready guide to understanding and fixing MongoDB querySrv ECONNREFUSED errors in Node.js by configuring reliable DNS resolvers.
On this page
If you are seeing `querySrv ECONNREFUSED _mongodb._tcp.cluster0.caopp.mongodb.net`, your application is failing before it even reaches MongoDB authentication. This is usually a DNS lookup problem while resolving Atlas SRV records, not an issue with your username or password.
What This Error Means
MongoDB Atlas connection strings commonly use the `mongodb+srv://` format. That format depends on DNS SRV lookups so the driver can discover hosts in your cluster. When that lookup is refused, Node.js cannot resolve the cluster address and the connection process fails with `ECONNREFUSED`.
Why It Happens
The most common root cause is DNS resolution failure in the network path your server is using. In practical terms, regional DNS/network instability may affect Atlas SRV resolution. This can happen on local ISP DNS, office networks, restricted VPS environments, or intermittently unstable routing paths.
- Local DNS resolver refusing SRV queries
- Temporary ISP or corporate DNS outages
- Firewall or network policy blocking DNS responses
- Environment-specific instability (works locally, fails in production)
Exact Node.js Fix (dns.setServers)
Force Node.js to use trusted public resolvers before your MongoDB connection starts. Place the following at the top of your app entry file.
For most Express projects, put it near the imports so DNS is configured before any MongoDB call is executed.
Additional Troubleshooting Checklist
- Confirm your Atlas URI starts with `mongodb+srv://` and has no typos
- Test DNS manually: `nslookup -type=SRV _mongodb._tcp.<your-cluster-host>`
- Retry from a different network to isolate ISP DNS issues
- Verify firewall rules allow outbound DNS (port 53) and MongoDB TLS traffic
- Check Atlas IP access list if your environment uses strict network controls
- Restart app process after changing DNS settings to ensure clean resolver state
Official-Style Error Explanation
`MongoDB connection failed: querySrv ECONNREFUSED _mongodb._tcp.cluster0.caopp.mongodb.net` indicates the runtime could not resolve Atlas SRV DNS records required by the `mongodb+srv` connection string. This is a DNS transport/resolution failure, not a credential validation failure. Configure reliable DNS resolvers and retry the connection to restore service.
FAQ
Is this a MongoDB Atlas outage every time?
No. In many cases Atlas is healthy, but your network path to DNS resolvers is unstable or refusing SRV lookups.
Can I switch from mongodb+srv to mongodb://?
You can, but it is usually better to fix DNS first. The SRV format is the recommended Atlas connection approach and supports topology discovery.
Where should dns.setServers be called?
Call it once at startup, before `mongoose.connect()` or any code that initializes MongoDB clients.
Shahmeer Rizwan
Full-Stack Developer