DEV Community

Sampatakumar S V
Sampatakumar S V

Posted on

Mongoose ECONNREFUSED Error (querySrv)

Many developers recently face this error while connecting to MongoDB Atlas using mongodb+srv://:

querySrv ECONNREFUSED _mongodb._tcp.<your-cluster>.mongodb.net

πŸ” What this means

This error occurs when Node.js fails to resolve DNS SRV records, which are required for mongodb+srv:// connections.

Even if:

βœ… MongoDB Atlas is configured correctly
βœ… IP is whitelisted
βœ… Credentials are correct

πŸ‘‰ The connection still fails due to DNS resolution issues.

βœ…

Solution (Working Fix)

Add the following at the very top of your server.js or index.js:

import dns from "dns";

// Force reliable DNS servers
dns.setServers(["8.8.8.8", "1.1.1.1"]);
Enter fullscreen mode Exit fullscreen mode

OR

import dns from "node:dns/promises";   
dns.setServers(["1.1.1.1", "1.0.0.1"]);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)