本文介绍了连接到 MongoDB Atlas 时如何修复“错误:querySrv EREFUSED"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 MongoDB 4.0.6 的新手,并尝试使用 Node/Express.js 将其实现到我的网站中,但是当我尝试连接到 mongodb+srv://${process.env.MONGOUSER}:${process.env.MONGOPASS}@main-03xkr.mongodb.net/main 我收到这个错误:

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:

{ 错误:querySrv EREFUSED _mongodb._tcp.main-03xkr.mongodb.net在 QueryReqWrap.onresolve [as oncomplete] (dns.js:199:19)errno: 'EREFUSED',代码:'EREFUSED',系统调用:'querySrv',主机名:'_mongodb._tcp.main-03xkr.mongodb.net' }

我已经尝试连接到 mongodb://localhost:27017/main,但这确实似乎有效.

I've tried connecting to mongodb://localhost:27017/main, but this does seem work.

相关代码如下:

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?

推荐答案

如果您遇到此错误,请尝试对 Node.js 2.2.12 或更高版本使用旧的连接字符串:

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

根据 MongoDB,SRV 可能由于 Mongoose 无法工作.

According to MongoDB, SRV is possibly not working due to Mongoose.

这篇关于连接到 MongoDB Atlas 时如何修复“错误:querySrv EREFUSED"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 11:41