NodeJS 使用mongoose连接数据库

92vpleto  于 9个月前  发布在  Node.js
关注(0)|答案(1)|浏览(143)

我尝试使用以下代码连接到Atlas Mongo数据库:

const mongoose = require('mongoose');

// connect to MongoDB
const dbURI = 'mongodb+srv://laajili:[email protected]/node-DB?retryWrites=true&w=majority';
mongoose.connect(dbURI, {
  useNewUrlParser: true,
  useUnifiedTopology: true
}).then((result) => console.log('connected to db'))
.catch((err) => console.log(err));

字符串
但是我在终端上得到这个错误:MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster.一个常见的原因是你试图从一个没有被列入白名单的IP访问数据库。确保你当前的IP地址在你的Atlas集群的IP白名单上:https://docs.atlas.mongodb.com/security-whitelist/
有没有人对这个主题有想法?

w9apscun

w9apscun1#

使用下面的服务来获取您的公共IP地址,
https://whatismyipaddress.com/
在Atlas控制台中,转到安全->网络访问-> IP访问列表(选项卡)
在此您将有一个选项“+添加IP地址”。
在此处添加您的公共IP地址,然后重试。

相关问题