MongoDB:“无法连接到 MongoDB Atlas 集群(白名单)中的任何服务器”

问题描述 投票:0回答:6

我遇到了一个我尝试解决很长时间的问题。 我正在尝试通过

nodejs
mongoose
连接到 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:[email protected]/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 我在数据库访问中的用户名和密码非常简单,没有任何特殊字符,但总是得到相同的错误:

 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
  }
}

非常感谢。

node.js mongodb mongoose
6个回答
25
投票

您可以尝试以下步骤:

  1. 单击网络访问。

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

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

让我知道它是否适合您:)


1
投票

我的 Strapi 应用程序也遇到同样的问题,我通过在数据库配置中添加 2 个选项解决了该问题:

'AUTHENTICATION_DATABASE', null
'DATABASE_SSL', true

在 Strapi Database.js 中:

options: {
    authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
    ssl: env.bool('DATABASE_SSL', true),
  },

1
投票

如果您将其用于学习测试目的,您可以选择“允许从任何地方访问”选项。又快又简单。


0
投票

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


0
投票

我也有同样的问题。不要从您的终端或网络首选项添加您的 IP 地址。相反...

  1. 转到MongoDb网络访问,然后单击IP地址上的编辑,或者如果您还没有添加IP地址,则添加IP地址。

  2. 单击“添加当前 IP 地址”,然后单击“确认...”,您已设置完毕。


0
投票

非常感谢,这很有帮助!所以我的提供商确实发生了一些事情,他开始为我分配一个动态 IP 地址

© www.soinside.com 2019 - 2024. All rights reserved.