当前位置: 首页 > 知识库问答 >
问题:

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

法风畔
2023-03-14

我有一个问题,我试图解决了很长一段时间。我正试图通过mongoosenodejs连接到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我在数据库访问中的用户名和密码非常简单,没有任何特殊字符,但总是出现相同的错误:

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

谢谢。

共有3个答案

龙正初
2023-03-14

使用当前IP地址1)转到mongoDB上的网络访问2)选择当前IP地址

楚皓君
2023-03-14

我的strapi应用程序也有同样的问题,我解决了在数据库配置中添加两个选项的问题:

'AUTHENTICATION_DATABASE', null
'DATABASE_SSL', true

在strapidatabase.js:

options: {
    authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
    ssl: env.bool('DATABASE_SSL', true),
  },
宿镜
2023-03-14

您可以尝试以下步骤:

  1. 单击网络访问。

这样,您将能够从任何IP地址访问您的数据库。

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

 类似资料:
  • 我的节点应用程序向我发送了这样一条消息:“无法连接到MongoDB Atlas群集中的任何服务器。请确保您当前的IP地址在Atlas群集中的IP白名单上。”。 我已经添加了我当前的IP地址以及IP白名单上的0.0.0.0。 下面是错误消息的图片和我编写的连接它的代码。我是node.js和mongoDB的新手。我查看了这里的所有解决方案,但没有一个能够解决这个问题。 这是我在运行nodemon后收到

  • 我使用node创建了一个RESTAPI,并使用Kubernetes和Docker对其进行了容器化。出于开发目的,kubernetes吊舱在minikube环境中运行。 该应用程序运行良好,现在给出了以下错误。 该问题似乎是MongoDB连接URL/Access问题,但连接字符串是正确的。(使用MongoDB Cloud支持仔细检查)。每个人都有相关的网络访问权限 我还可以确认可以使用相同的连接UR

  • sudo ufw状态:活动状态 从---------22/TCP允许任意位置10000 允许任意位置Nginx完全 允许任意位置3333 允许任意位置27017 允许任意位置22/TCP(v6) 允许任意位置(v6)10000(v6) 允许任意位置(v6)Nginx完全(v6) 允许任意位置(v6)27017(v6) 允许任意位置(v6) 从我的mac连接到它会引发错误: mongo MongoD

  • 我有一个问题已经一个月了,但我没有找到解决办法。我的笔记本电脑和个人电脑都有这些问题(我以前启动过mongod): 错误:无法连接到服务器 127.0.0.1:27017 src/mongo/shell/mongo.js:91 异常:连接失败 错误:无法连接到服务器127.0.0.1:27017 src/mongo/shell/mongo。js:93异常:连接失败 错误:无法连接到服务器127.0

  • 我在localhost上有一个带有mongo数据库的Spring Boot应用程序,一切都很好。 当我运行我的应用程序时,我得到“com.mongodb.mongoSocketOpenException:Exception opening Socket”由“regused by:java.net.SocketTimeoutException:connect timed out”引起

  • OpenStack-Kilo OS-CentOSLinux7.2.1511(Core) 应用 /var/tmp/packstack_ /var/tmp/packstack 应用 /root_redis.pp 10.xx.xxx.xx_mongodb.pp:[ERROR] 应用Puppet清单[ERROR] 错误:在Puppet运行期间出现错误:10.xx.xxx.xx_mongodb.pp 错误: