错误:“无法连接到MongoDB Atlas集群中的任何服务器”

时间:2020-07-12 08:58:15

标签: node.js mongodb mongoose

我遇到一个问题,试图解决很长时间。 我尝试使用mongoose npm从nodejs连接到mongo atlas云。 现在,这不是我的第一次,但是我只是找不到答案

我的猫鼬版本:5.9.22

这是我的代码:

  const express = require('express')
    const mongoose = require('mongoose')
    const bodyparser = require('body-parser')
    
    const app = express()
    app.use(bodyparser.json())
    
  mongoose.connect("mongodb+srv://dudvil1:password@cluster0.guxmm.mongodb.net/shopping_list? 
  retryWrites=true&w=majority", {
      useNewUrlParser: true,
      useUnifiedTopology: true
  })
          useNewUrlParser: true,
          useUnifiedTopology: true
      })
      .then(() => console.log('mongo connected'))
       .catch(err => console.log(err));   
    
    const port = process.env.PORT || 5000   
    
    app.listen(port, () => console.log(`server started in port ${port}`))

我的白名单仅包含0.0.0.0/0 我在数据库acsses中的用户名和密码非常简单,没有任何特殊字符 但总是会得到相同的错误:

 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 (C:\Users\Dudu\Desktop\MERN\node_modules\mongoose\lib\connection.js:826:32)
    at Mongoose.connect (C:\Users\Dudu\Desktop\MERN\node_modules\mongoose\lib\index.js:335:15)
    at Object.<anonymous> (C:\Users\Dudu\Desktop\MERN\server.js:10:10)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11 {
  message: "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/",
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map {
      'cluster0-shard-00-02.guxmm.mongodb.net:27017' => [ServerDescription],
      'cluster0-shard-00-00.guxmm.mongodb.net:27017' => [ServerDescription],
      'cluster0-shard-00-01.guxmm.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}

非常感谢

2 个答案:

答案 0 :(得分:6)

您可以尝试以下步骤:

  1. 单击网络访问。

enter image description here

  1. 单击删除按钮旁边的编辑按钮。
  2. 单击“添加当前IP地址”按钮,然后单击“确认”。

这样,您将可以从任何IP地址访问数据库。

让我知道它是否对您有用:)

答案 1 :(得分:0)

使用当前 IP 地址 1)进入mongodb上的网络访问 2)选择当前IP地址