繁体   English   中英

Mongodb 图集连接失败

[英]Mongodb atlas connection failed

我在 mongodb atlas 中创建了一个名为 priceDatabase 的数据库。 我还创建了一个用户名“admin”和密码“admin”的用户。 我正在尝试将我的节点 js 应用程序连接到 mongodb 地图集。 为此,我使用 mongoose。

const mongoose = require("mongoose");

const dotenv = require("dotenv");
dotenv.config();

mongoose
  .connect(process.env.MONGO_URI)
  .then((db) => {
    console.log("Connected to the mongodb server successfuly: ");
  })
  .catch((err) => console.log(err));

.env 文件

MONGO_URI = mongodb+srv://admin:admin@pricedatabase.re29j.mongodb.net/priceDatabase?retryWrites=true&w=majority

现在,如果我运行代码,我会收到错误消息

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/
    at NativeConnection.Connection.openUri (B:\projects\priceTagServer\node_modules\mongoose\lib\connection.js:797:32)
    at B:\projects\priceTagServer\node_modules\mongoose\lib\index.js:341:10
    at B:\projects\priceTagServer\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (B:\projects\priceTagServer\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (B:\projects\priceTagServer\node_modules\mongoose\lib\index.js:1167:10)    
    at Mongoose.connect (B:\projects\priceTagServer\node_modules\mongoose\lib\index.js:340:20)
    at Object.<anonymous> (B:\projects\priceTagServer\models\db.js:10:4)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'pricedatabase-shard-00-01.re29j.mongodb.net:27017' => [ServerDescription],
      'pricedatabase-shard-00-02.re29j.mongodb.net:27017' => [ServerDescription],
      'pricedatabase-shard-00-00.re29j.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-bew5c1-shard-0',
    logicalSessionTimeoutMinutes: undefined
  }
}

这是说我的不在 mongodb 地图集中的白名单中,但我已授予 0.0.0.0/0 的网络访问权限,这意味着每个设备都可以访问数据库。 那么,为什么我仍然收到该错误? 我错过了什么吗?

要连接到 atlas 集群,您需要同时列出您的 IP 地址。 您可以从 mongoDB 地图集仪表板执行此操作。 点击network access > add ip address ,然后点击按钮“添加当前 IP 地址”,或添加0.0.0.0/0允许从任何地方访问 0.0。

mongodb 例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM