2
MongoDb Connection failed : Error: querySrv ECONNREFUSED 

How can I solve this error? My node v24.13.0 and mongoose 9.1.5 are the latest versions. Also I changed my network dns to Google's dns number 8.8.8.8, and in my mongodb atlas network acc ip address is 0.0.0.0/0 so that any ip address can access and install Locally in my system.


Here is the connection code:

import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
dotenv.config({ path: "./.env" });
const app = express();
const MONGO = process.env.MONGO_URL;
mongoose
 .connect(MONGO)
 .then(() => {
 console.log("Database Successfully connected");
 })
 .catch((error) => {
 console.error(`MongoDb Connection failed : ${error}`);
 });
app.use(express.json());
app.use(cors());
app.listen(5000, () => {
 console.log(`Server started on port Number : 5000`);
});

error png

Note that when I used this it works perfectly:

import express from "express"; 
import mongoose from "mongoose"; 
import cors from "cors"; 
import dotenv from "dotenv"; 
dotenv.config({ path: "./.env" }); 
 
/* works when I add this */
import dns from "node:dns/promises"; 
dns.setServers(["1.1.1.1", "1.0.0.1"]); 
const app = express(); 
const MONGO = process.env.MONGO_URL; 
mongoose 
 .connect(MONGO) 
 .then(() => { 
 console.log("Database Successfully connected"); 
 }) 
 .catch((error) => { 
 console.error(MongoDb Connection failed : ${error}); 
 }); 
app.use(express.json()); 
app.use(cors()); 
app.listen(5000, () => { 
 console.log(Server started on port Number : 5000); 
});

But the problem is I have so many projects that I already built, so I don't want to add this in every project:

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

Is there another solution?

tdy
42.3k45 gold badges137 silver badges132 bronze badges
asked Jan 24 at 14:55
1
  • OP states that hardcoding DNS servers fixes the problem. It seems self-explanatory that MONGO_URL is correct and dotenv is installed; otherwise, they would get an error regardless of the DNS config. Your comment does not offer useful advice. Commented Jan 25 at 12:54

1 Answer 1

11

Now I've found the root cause. This is not a problem with MongoDB. This is a problem with Node.js. When I chose Node.js 18.20.8 version, it worked perfectly.


Or you can use the latest version of Node.js like (20 , 25)

import dns from "node:dns/promises";
dns.setServers(["1.1.1.1", "1.0.0.1"]);
tdy
42.3k45 gold badges137 silver badges132 bronze badges
answered Jan 25 at 20:10
Sign up to request clarification or add additional context in comments.

Thanks — the issue was with the Node.js version. After switching to version 20.9.0, everything is working correctly.
If you're on Node 20+ with mongodb+srv://, try forcing public DNS before connecting: js import dns from "node:dns"; dns.setServers(["1.1.1.1", "1.0.0.1"]); await mongoose.connect(MONGO_URI); This fixes querySrv ECONNREFUSED caused by SRV lookup failures in newer Node.js DNS resolver. Only affects the current process, not system DNS.
Thanks, a lot for this answer, const dns = require("node:dns/promises"); dns.setServers(["1.1.1.1", "1.0.0.1"]); after using this dns perfectly works my server.
on my mongoose.ts file, i just pasted added "require("node:dns/promises").setServers(["1.1.1.1", "8.8.8.8"]); " at the top of the file before adding any imports .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.