mongoDB白名单IP

问题描述

我看到了类似的帖子,但没有一个能帮助我解决问题。

按照从头开始构建 MERN 应用程序的 Udemy 教程,我卡在猫鼬连接上。

这是我的 index.js 代码

const express = require("express");
const mongoose = require("mongoose");

const app = express();

app.use(express.json());

app.listen(5000,() => console.log("Server started on port 5000"));

app.use("/snippet",require("./routers/snippetRouter"));

mongoose.connect("mongodb+srv://snippetUser:_password_@
  snippet-manager.sometext.mongodb.net/main?retryWrites=
  true&w=majority",{
    useNewUrlParser: true,useUnifiedTopology: true
},(err) => {
  if (err) return console.log("error here " + err);
  console.log("Connected to MongoDB");
});

这是我得到的错误

Server started on port 5000
error here 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 相关的类似错误

然而,在我的mongoDB账户中,我的IP似乎已经被列入白名单:

enter image description here

在上面的屏幕截图中,空白部分是我的 IP 所在的位置(就在它说“包括您当前的 IP 地址”之前)。

既然我的 IP 列在那里,那不就意味着我的 IP 被列入白名单了吗?

如果没有,我如何将我的 IP 列入白名单?

解决方法

经过几天的挫折后,我进入了 Mongo Atlas,然后进入了网络访问,并将设置更改为“允许从任何地方访问”。它删除了我的 IP 地址并将其更改为通用 IP 地址。

这与我在 Udemy 上遵循的教程有所不同,但它确实有效,我终于可以继续本课程的其余部分了。