javascript - MongoDB atlas 连接失败,出现错误 MongoServerSelectionError : connection <monitor> to 52. 64.0.234:27017 closed

标签 javascript mongodb mongodb-atlas

我已经建立了一个 mongodb Atlas 免费层集群。当我尝试使用节点 js 连接到它时,它会引发错误。我已经手动和选择当前将我的 IP 列入白名单。我也尝试将 +srv 添加到我的连接网址,但这只会导致更多错误。

这是我试图连接的节点 js 代码

const { MongoClient } = require("mongodb");                                                                                                                                       

const url = "mongodb://user1:password1!@cluster0-shard-00-00-bc7dh.mongodb.net/test?retryWrites=true&w=majority&useNewUrlParser=true&useUnifiedTopology=true";

const client = new MongoClient(url);

async function run() {
    try {
        await client.connect();
        console.log("Connected correctly to server");

    } catch (err) {
        console.log(err.stack);
    }
    finally {
        await client.close();
    }
}

run().catch(console.dir);


这是我得到的错误

MongoServerSelectionError: connection to 52.64.0.234:27017 closed at Timeout._onTimeout (C:\Users\YOUNG\node_modules\mongodb\lib\core\sdam\topology.js:430:30) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7)



有类似问题的人可以通过将他们的 IP 地址列入白名单来解决它,但它对我不起作用。可能是什么问题?

我尝试允许访问所有 ip,但错误仍然存​​在,当我将 uri 与 +srv 一起使用时,出现以下错误
MongoServerSelectionError: Authentication failed.
at Timeout._onTimeout (C:\Users\YOUNG\node_modules\mongodb\lib\core\sdam\topology.js:430:30)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7)

最佳答案

这是因为您的 IP 地址未列入白名单。
转至 网络访问在您的项目中的 mongodb map 集和 添加IP地址 .仅测试从任何地方添加访问权限选项
在那里你回去再试一次连接字符串。
enter image description here

关于javascript - MongoDB atlas 连接失败,出现错误 MongoServerSelectionError : connection <monitor> to 52. 64.0.234:27017 closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62342929/

相关文章:

javascript - 从函数参数中获取图像名称

javascript - mongodb根据字段公共(public)值进行分组收集

javascript - 动态定义 Mongoose 模型方法

javascript - Mongoose:find() - 匹配两个值并仅返回匹配的文档

mongodb - MongoDB Atlas 中的多个集群与一个集群

javascript - 如何使用 systemJS 将 libphonenumber-js 包含在我的 angular2 应用程序中?

Javascript - 从 JSON 到对象的数组

javascript - Element.getBoundingClientRect 何时保证更新/准确?

json - 使用 JSON/HTTP/REST 接口(interface)查询 MongoDB

node.js - Node.JS 对 MongoDB Atlas 授权失败