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

L m*_*L m 15 mongodb node.js 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)
Run Code Online (Sandbox Code Playgroud)

这是我在运行 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)]: {}
}


Run Code Online (Sandbox Code Playgroud)

错误信息和代码

Vig*_*rni 25

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

这对我有用!

  • 他已经提到他在 IP 白名单上设置了 0.0.0.0 (大概是 /0 ),这是你建议的结果。如果它对你有用,那么你就会遇到不同的问题。 (6认同)
  • “IP 访问列表”下的“允许任何”似乎不再是一个选项 (5认同)
  • 是的,我当时不知道 0.0.0.0 是什么意思。但后来知道后,看到这个答案对人有帮助,所以就没有考虑删除。 (2认同)

小智 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!
Run Code Online (Sandbox Code Playgroud)


小智 10

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

  • 添加到此答案,如果您在将 IP 添加到 MongoDB Cloud 时打开了 VPN,并且不再打开它(反之亦然),您将收到此错误 (3认同)

小智 9

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

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


Mod*_*oud 8

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

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

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


cri*_*ari 8

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


Lak*_*mod 5

不一定是ip的问题。数据库名称、用户名或密码可能有问题。然后连接也失败。