node.js - 从共享 linux 托管服务器连接到 mongodb Atlas 时出错
问题描述
我正在尝试从我的 nodejs 应用程序连接到 mongoDB Atlas,它给了我以下错误:
SERVER STARTED...
listening on port 'http://localhost:8080'
Could not connect to the MongoDB database. Exiting now... { Error: connect ECONNREFUSED 13.234.134.161:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
我还在 Mongodb Cloud (Atlas) 上将 IP 列入白名单。我将其设置为“访问所有人”,即:0.0.0.0/0
相同的代码在我的本地机器上运行良好。
SERVER STARTED...
listening on port 'http://localhost:8080'
Successfully connected to the MongoDB database
当我将代码部署到 GoDaddy 共享 linux 托管服务器并尝试从那里连接时,就会出现问题。
有什么不同...?
我的nodejs代码是:
app.set('port', process.env.PORT || 8080);
var options = {
useNewUrlParser: true,
useUnifiedTopology: true
}
mongoose.connect(dbConfig.dbUrl, options, function(err){
if(err){
console.log('Could not connect to the MongoDB database. Exiting now...', err);
process.exit();
} else {
console.log("Successfully connected to the MongoDB database");
}
});
const server = app.listen(app.get('port'), function(){
console.log(`SERVER STARTED...\nlistening on port 'http://localhost:${app.get('port')}'`);
});
数据库网址如下:
dbUrl: "mongodb+srv://<USERNAME>:<PASSWORD>@cluster0-qsfll.mongodb.net/<DATABASE>?retryWrites=true&w=majority"
如果有人遇到同样的问题并解决了,请在这里写下解决方案。谢谢 :)。
解决方案
刚遇到同样的问题。尝试删除useUnifiedTopology
. 对我有帮助。
推荐阅读
- amazon-web-services - 需要在客户端浏览器中创建 Excel 文件并下载 - 是否可以创建文件流并在 Node Express 响应中发送?
- php - 如何在负载均衡的 Apache+PHP 服务器上设置服务器名称?
- python - 使用 ctypes 从 python 中的 C 访问结构内的二维数组
- linux - 检查库构建的标志或选项(-frecord-gcc-switches)
- javascript - 如何将 javascript 数组发送到我的数据库?
- javascript - Fullcalendar js事件颜色为json
- sql - 有没有办法通过 SQL 在 Snowflake 中创建一个临时表,而不必每次都编写列?与,插入
- python - 如何在 Python 中连接两个列表?
- python - 剪辑二维列表直到内容形成一个矩形(Python)
- c - 套接字上的 ioctl FIONREAD 在运行时是否会阻止其他套接字上的操作?