Shahmeer Rizwan
Contact
Back to blog
Backend Troubleshooting6 min read

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.

Snippet 1 (app.js fix)
1import dns from "dns";
2dns.setServers(["1.1.1.1", "8.8.8.8"]);

For most Express projects, put it near the imports so DNS is configured before any MongoDB call is executed.

Snippet 2 (full placement)
1import express from "express";
2import dotenv from "dotenv";
3import mongoose from "mongoose";
4import cors from "cors";
5import dns from "dns";
6
7dns.setServers(["1.1.1.1", "8.8.8.8"]);
8dotenv.config();

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

Related articles

E-Commerce

How to Build a Scalable E-Commerce Platform with Next.js and React

Discover why Next.js and React are the ideal stack for building high-performance, SEO-friendly e-commerce platforms that scale with your business.

ERP Systems

Why Custom ERP Systems Often Outperform Off-the-Shelf Solutions

Learn how a tailored ERP system can streamline your operations, reduce costs, and give your business a competitive edge over generic software.

LMS Development

The Complete Guide to Building a Learning Management System (LMS)

Everything you need to know about building a custom LMS — from core features and tech stack to deployment and scaling strategies.

Have a project in mind? Let's make it happen.

Whether you need a web application, mobile app, or custom software solution — let's discuss how to turn your idea into a high-quality digital product built for growth.

Free consultation · No commitment · Response within 24 hours