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

时间:2020-05-21 15:07:49

标签: node.js mongodb express

我的节点应用程序给我此消息 “无法连接到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

12 个答案:

答案 0 :(得分:7)

尝试执行此操作:-MongoDB Atlas-> NetworkAccess-> Edit-> Allow Any

这对我有用!

答案 1 :(得分:5)

您正在使用代理吗?请先尝试使用其他Internet连接。

答案 2 :(得分:2)

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

答案 3 :(得分:1)

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

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

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

答案 4 :(得分:0)

有时也可能是由于系统设置造成的。

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

答案 5 :(得分:0)

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

我在长时间休息后重新开始使用mongodb之前是否更改了IP地址? 如果没有,请进行更改,因为通常mongodb每隔6小时就会将其删除一次。

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

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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

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

答案 8 :(得分:0)

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

路由器的默认 IP 地址通常是 192.168.0.1,但在某些情况下可能会有所不同。只需转到该 IP,以管理员身份登录,然后尝试更改防火墙设置。当然,这需要牺牲一些安全性,但就我而言,此设置还会干扰其他事情,例如智能设备连接。

答案 9 :(得分:0)

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

答案 10 :(得分:0)

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

答案 11 :(得分:0)

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!