本文介绍了连接到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:

我已经尝试连接到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,由于Mongoose,SRV可能无法正常工作.

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

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

08-11 11:41