【发布时间】:2020-10-02 04:35:46
【问题描述】:
我已经建立了一个 mongodb Atlas 免费层集群。当我尝试使用 node 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:与 52.64.0.234:27017 的连接已关闭 在 Timeout._onTimeout (C:\Users\YOUNG\node_modules\mongodb\lib\core\sdam\topology.js:430:30) 在 listOnTimeout (internal/timers.js:549:17) 在 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的IP:0.0.0.0
-
你试过
mongodb+srv://协议了吗? -
我已经尝试了上述两个建议,但仍然出现上述相同的错误
标签: javascript mongodb mongodb-atlas