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"]);
OR
import dns from "node:dns/promises";
dns.setServers(["1.1.1.1", "1.0.0.1"]);
Top comments (1)
I have got another soln..
--> just Check this ---
mongosh --versionin your terminal. if you not get any info then install community server from mongodb atlas website problem will be solve. π get from here : - MongoDB Atlas