Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 610 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas?

#1
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?
Reply

#2
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.
Reply

#3
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.
Reply

#4
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]:
Reply

#5
In our case antivirus/firewall is blocking,

Try to disable antivirus/firewall and check again. hope it will work.
Reply

#6
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));
```
Reply

#7
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 )
Reply

#8
This error occurs sometimes when you are using MongoDB Atlas and lost your internet connectivity. You will not be able to access your database.
Reply

#9
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]:
Reply

#10
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through