【问题标题】:Error : "Could not connect to any servers in your MongoDB Atlas cluster"错误:“无法连接到 MongoDB Atlas 集群中的任何服务器”
【发布时间】:2020-09-08 06:21:11
【问题描述】:

我的节点应用程序给了我这个消息 “无法连接到您的 MongoDB Atlas 集群中的任何服务器。请确保您当前的 IP 地址在 Atlas 集群的 IP 白名单中”。

我已经在 IP 白名单中添加了我当前的 IP 地址以及 0.0.0.0。

下面是错误消息的图片和我为连接它而编写的代码。 我是 node.js 和 mongodb 的新手。我查看了这里的所有解决方案,但没有一个能够解决这个问题。

var express = require('express');
var bodyparser = require('body-parser');
var mongoose = require('mongoose');

var app = express();
app.use(bodyparser.json());

mongoose
     .connect("mongodb+srv://<username:password>@my-cluster.mongodb.net/test?retryWrites=true&w=majority", 
     { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true })
     .then(() => console.log( 'Database Connected' ))
     .catch(err => console.log( err ));

    app.listen(3003)

这是我在运行 nodemon 后收到的错误。

[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
MongooseError [MongooseServerSelectionError]: Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/.
    at new MongooseServerSelectionError (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\node_modules\mongoose\lib\error\serverSelection.js:24:11)
    at NativeConnection.Connection.openUri (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\node_modules\mongoose\lib\connection.js:823:32)
    at Mongoose.connect (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\node_modules\mongoose\lib\index.js:333:15)
    at Object.<anonymous> (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\server.js:9:7)
    at Module._compile (internal/modules/cjs/loader.js:1157:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
    at Module.load (internal/modules/cjs/loader.js:1001:32)
    at Function.Module._load (internal/modules/cjs/loader.js:900:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  message: "Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/.",
  name: 'MongooseServerSelectionError',
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map {
      'cluster1-shard-00-01-m3rzz.mongodb.net:27017' => [ServerDescription],
      'cluster1-shard-00-02-m3rzz.mongodb.net:27017' => [ServerDescription],
      'cluster1-shard-00-00-m3rzz.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}


Error Message and code

【问题讨论】:

  • 欢迎使用stackoverflow!您可以添加代码 sn-p 和文本格式的日志而不是屏幕截图吗?对于可以使用移动设备、平板电脑或其他设备访问 SO 的社区来说,阅读起来更容易吗?谢谢
  • 感谢您告诉我。我重新格式化了它
  • 感谢您的推荐。但在我创建这篇文章之前,我已经尝试过了

标签: node.js mongodb express


【解决方案1】:

你在使用代理吗?请先尝试不同的互联网连接。

【讨论】:

  • 添加到这个答案,如果您在将 IP 添加到 MongoDB Cloud 时打开了 VPN,但您不再打开它(反之亦然),您将收到此错误
【解决方案2】:

尝试这样做:- MongoDB Atlas -> NetworkAccess -> Edit -> Allow Any

这对我有用!

【讨论】:

  • 他已经提到他在IP白名单上设置了 0.0.0.0 (大概是 /0 ),这是您建议的结果。如果它对您有用,那么您遇到了不同的问题。
  • 是的,我当时不知道 0.0.0.0 是什么意思。但是后来知道了,看到这个回答对人有帮助,所以没考虑删。
  • Allow AnyIP Access List 下似乎不再是一个选项
  • 为我工作 - 非常感谢
【解决方案3】:

ip 并不总是有问题。数据库名称,用户名或密码可能有问题。然后连接也失败。

【讨论】:

  • 这太真实了!我有同样的错误,但我的问题是“数据库访问”,请参阅this SO answer
【解决方案4】:

有时也可能是系统设置的原因。

在我的情况下,它是服务器系统的日期和时间,它没有更新到当前的日期和时间设置,因此从服务器到 db 的连接被拒绝。更新相同,重启服务器,连接成功。

【讨论】:

  • 谢谢!这让我绊倒了。我正在使用 USB Live Ubuntu 运行开发设置,我一直使用 UTC 以防止它重置我的计算机时钟。
【解决方案5】:

当您的 mongodB 遇到这样的问题时,请记住这些问题:

我是否在长时间休息后再次开始使用我的 mongodb 之前更改了我的 IP 地址? 如果不是,请更改它,因为通常 mongodb 会在每 6 小时后将其删除。

我是从上次工作的地方搬来的吗? 如果是,请重新更改您的IP并激活它。

请访问链接https://studio3t.com/knowledge-base/articles/mongodb-atlas-login-ip-whitelisting/#whitelist-multiple-or-additional-ip-addresses 并详细了解为什么会出现此类错误以及如何处理它们。很有帮助。

【讨论】:

    【解决方案6】:

    要将 IP 地址列入白名单,请转到 MongoDB Atlas 上的目标集群。接下来,转到“安全”选项卡下的“网络访问”。在 IP 白名单选项卡上,单击添加 IP 地址。在白名单条目下手动输入您的 IP 地址,然后单击确认。运行您的代码,它应该可以毫无问题地运行。

    【讨论】:

      【解决方案7】:

      不确定这是否是正确的原因。但是,当我断开 VPN 时,它对我有用。

      连接到 VPN 时,我收到以下错误消息。

      错误: “无法连接到您的 MongoDB Atlas 集群中的任何服务器。请确保您当前的 IP 地址在 Atlas 集群的 IP 白名单中”。

      【讨论】:

        【解决方案8】:

        如果密码正确并且您拥有allowed access from anywhere 并且仍然出现同样的错误,那么,问题在于您的网络有防火墙。我通过更改网络解决了我的问题,因为我有防火墙。

        【讨论】:

          【解决方案9】:

          我为此苦苦挣扎了很久,终于想到检查路由器上的防火墙。我曾一度将其设置为最高安全级别,然后将其设置为低一级,这样就解决了!

          路由器的默认 IP 地址通常为 192.168.0.1,但在某些情况下可能会有所不同。只需转到该 IP,以管理员身份登录,然后尝试更改防火墙设置。当然,需要权衡一些安全性,但在我的情况下,这个设置也搞乱了其他事情,比如智能设备连接。

          【讨论】:

            【解决方案10】:

            如果您使用任何基于代理或证书的互联网,则会引发此类问题。尝试不同的互联网连接。

            【讨论】:

              【解决方案11】:

              在您的 Mongo 项目中,转到网络访问部分,然后编辑 IP 以允许从任何地方 (0.0.0.0/0) 访问。当允许从任何地方访问时,确保所有数据库用户都使用强凭据(用户名和密码)。

              【讨论】:

                【解决方案12】:
                Follow below steps:
                1] Go to https://www.mongodb.com/cloud/atlas
                2] login to your mongodb atlas account
                3] Click on network access
                
                i] if you have already added ip address, then here you can see ip access list
                a] click on delete 
                
                ii] if you have not already added ip address, then just follow below steps:
                
                4] click on add ip address
                5] select 'add current ip address' or if you want you can also select 'allow access from anywhere'
                6] type in comment if you want
                7] click on confirm button
                8] this will show pending loader, wait until it shows active status
                9] now restart your server on terminal by typing 'npm start'
                
                -- That's it!
                

                【讨论】:

                • 重启服务器很重要
                【解决方案13】:

                正如lakshitha-samod所说,不只是IP地址问题,可能不一样

                (在我的情况下,我使用的是登录密码而不是 db 密码)

                mongodb+srv://<username>:<password>@<clusterName>.p9ykp.mongodb.net/<databaseName>?retryWrites=true&w=majority
                

                1. IP 地址(用于交叉检查)

                转到网络访问 -> 添加/编辑 -> 添加当前 IP 地址或允许从任何地方访问

                1.检查数据库用户/创建一个

                转到数据库访问 -> 如果不记得,请创建新/编辑并重置密码

                2。数据库名称

                转到您的集群 -> 浏览集合 -> 选择一个集合(数据库名称:ex: sample_training)

                【讨论】:

                  【解决方案14】:

                  通过进行此更改,我能够解决与 MongoDB 上服务器的连接问题。

                  MongoDB Atlas -> NetworkAccess -> 编辑 -> 允许从任何地方访问

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2022-11-27
                    • 2021-04-23
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-06-10
                    • 2018-03-05
                    • 2018-08-10
                    • 1970-01-01
                    相关资源
                    最近更新 更多