首页 > 解决方案 > 从共享 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"

如果有人遇到同样的问题并解决了,请在这里写下解决方案。谢谢 :)。

标签: node.jsshared-hostingmongodb-atlas

解决方案


刚遇到同样的问题。尝试删除useUnifiedTopology. 对我有帮助。


推荐阅读