How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? - Printable Version +- 0Day Forums (https://0day.red) +-- Forum: Coding (https://0day.red/Forum-Coding) +--- Forum: NodeJs (https://0day.red/Forum-NodeJs) +--- Thread: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? (/Thread-How-to-fix-39-Error-querySrv-EREFUSED-39-when-connecting-to-MongoDB-Atlas) Pages:
1
2
|
How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? - unbeloved144421 - 07-21-2023 I am new to MongoDB 4.0.6 and tried to implement it into my website using Node/Express.js, but when I try to connect to `mongodb+srv://${process.env.MONGOUSER}:${process.env.MONGOPASS}@main-03xkr.mongodb.net/main` I'm getting this error: > { Error: querySrv EREFUSED _mongodb._tcp.main-03xkr.mongodb.net at QueryReqWrap.onresolve [as oncomplete] (dns.js:199:19) errno: 'EREFUSED', code: 'EREFUSED', syscall: 'querySrv', hostname: '_mongodb._tcp.main-03xkr.mongodb.net' } I've tried connecting to `mongodb://localhost:27017/main`, but this **does** seem work. Here is the relevant code: ```javascript require('dotenv').config(); const mongoose = require('mongoose'); // Database const uri = `mongodb+srv://${process.env.MONGOUSER}:${process.env.MONGOPASS}@main-03xkr.mongodb.net/main`; const localURI = 'mongodb://localhost:27017/main'; var Project = require('./models/project'); mongoose.connect(uri, { useNewUrlParser: true }); const db = mongoose.connection; db.once('open', () => console.log('Successfully connected to MongoDB')); db.on('error', (e) => console.log(e)); // Routes app.get('/', (req, res) => { Project.find({}, (e, projects) => { if (e) console.log(e); res.render('home.ejs', { projects: projects }); }); }); ``` So does anyone know how to fix this error and maybe explain what is happening here? RE: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? - agathaea515411 - 07-21-2023 You have `undefined` in your connection string. I don't know if this is typo or not. But try changing const uri = `mongodb+srv://undefined:${process.env.MONGOPASS}@main-03xkr.mongodb.net/main`; to const uri = `mongodb+srv://${process.env.MONGOUSER}:${process.env.MONGOPASS}@main-03xkr.mongodb.net/main`; I use MongoAtlas for a project and that string (minus the undefined user) looks correct. RE: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? - Sirsporangiosporermlmusvo - 07-21-2023 If you're encountering this error try to use the older connection string for Node.js 2.2.12 or later: ``` mongodb://<username>:<password>@main-shard-00-00-03xkr.mongodb.net:27017,main-shard-00-01-03xkr.mongodb.net:27017,main-shard-00-02-03xkr.mongodb.net:27017/main?ssl=true&replicaSet=Main-shard-0&authSource=admin&retryWrites=true ``` According to MongoDB, SRV is possibly not working due to Mongoose. RE: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? - torturing186304 - 07-21-2023 I had this same error when I was connecting with `Node` version `3.0 or later` and I resolved it by downgrading to `2.2.12 or later` version: [![downgrade][1]][1] [1]: RE: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? - costosternal790622 - 07-21-2023 In our case antivirus/firewall is blocking, Try to disable antivirus/firewall and check again. hope it will work. RE: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? - twiladumc - 07-21-2023 Pass option `{ useNewUrlParser: true, useUnifiedTopology: true }` to the MongoClient constructor ``` const uri = "mongodb+srv://${process.env.MONGOUSER}:${process.env.MONGOPASS}@main-03xkr.mongodb.net/main" mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true }) .catch(error => console.log(error)); ``` RE: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? - acejnev - 07-21-2023 MongoClient.connect( "mongodb://USER:[email protected]/test?ssl=true&replicaSet=mflix-shard-0&authSource=admin&retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true }, ) .catch(err => { console.error(err.stack) process.exit(1) }) .then(async client => { await MoviesDAO.injectDB(client) await UsersDAO.injectDB(client) await CommentsDAO.injectDB(client) app.listen(port, () => { console.log(`listening on port ${port}`) }) }) Maybe can works with MongoClient( not Mongoose ) RE: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? - emasculations672742 - 07-21-2023 This error occurs sometimes when you are using MongoDB Atlas and lost your internet connectivity. You will not be able to access your database. RE: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? - cobaltic117 - 07-21-2023 Make sure to change the node version to 2.2.12: ![enter image description here][1] And add IP address: ![enter image description here][2] [1]: [2]: RE: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas? - inspection854 - 07-21-2023 In my case, this error was happening when the **DNS configuration** in my TP-Link Router was missing. I've installed OpenWRT firmware on it and forgot to adjust DNS settings. I was able to open YouTube or any other website because that's not my main router, but could not connect to database. It was an internet issue, like **@Kamesh Kumar Singh** said in his answer. I think that this is not an usual answer for this question, but may help someone. |