【问题标题】:Mongoose failed to connect and Atlas MongoDB expect to whitelist IP while all IP has allowedMongoose 连接失败,Atlas MongoDB 希望在所有 IP 都允许的情况下将 IP 列入白名单
【发布时间】:2022-01-18 15:12:34
【问题描述】:

我希望有人可以帮助我,因为我对这个问题感到困惑 我认为我的代码没问题,但 Atlas 似乎拒绝了,所以我无法连接到 Atlas。连接如此简单。我从 nodemon.json 填充这些参数:

const {
    MONGODB_ATLAS_USERNAME,
    MONGODB_ATLAS_PASSWORD,
    MONGODB_ATLAS_DBNAME
} = process.env

var mongoose = require('mongoose');
app.listen(PORT, () => {
console.info(`App is running! at http://localhost:${PORT}`)
})   
mongoose.connect(`mongodb://${MONGODB_ATLAS_USERNAME}:${MONGODB_ATLAS_PASSWORD}@cluster0.lzjty.mongodb.net/${MONGODB_ATLAS_DBNAME}?retryWrites=true&w=majority&ssl=true`,{useNewUrlParser:true,useUnifiedTopology:true});

在 Atlas 上,我将我的 IP 列入白名单并允许所有 IP 连接 但是每次 npm 运行,总是返回:

    App is running! at http://localhost:8080
[1] (node:2012) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/

【问题讨论】:

  • 你在使用你的公网IP吗?只是问,因为我以前见过人们这样做
  • 当然,但是如果我使用非公共IP,它不应该已经能够使用0.0.0.0/0 连接吗?

标签: javascript node.js mongodb mongoose ip


【解决方案1】:

这通常发生在您使用公共 IP 时。每次连接到互联网时它都会发生变化,您需要对 MONGO Atlas 进行更改。我建议您为开发做出这些更改。

关注这个:- MongoDB Atlas -> NetworkAccess -> Edit -> Allow Any

【讨论】:

  • 如果可以解释否决票以给出更好的答案,那将是一个好主意。
  • 是的,我已经使用了 Allow any。所以你可以在我的图片上看到 0.0.0.0 但我仍然无法连接
猜你喜欢
  • 1970-01-01
  • 2022-10-08
  • 2022-12-16
  • 2021-05-01
  • 1970-01-01
  • 2017-10-27
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
相关资源
最近更新 更多