MongoDB:“无法连接到您的 MongoDB Atlas 集群(白名单)中的任何服务器" [英] MongoDB: “Could not connect to any servers in your MongoDB Atlas cluster (whitelist)”

查看:116
本文介绍了MongoDB:“无法连接到您的 MongoDB Atlas 集群(白名单)中的任何服务器"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我尝试解决了很长时间.我正在尝试通过 mongoosenodejs 连接到 Mongo Atlas 云.这不是我第一次,但我就是找不到答案.

I am having an issue which I tried to fix for a long time. I am trying to connect to Mongo Atlas cloud from nodejs viamongoose. It's not my first time but I just can't find the answer.

猫鼬版本:5.9.22

Mongoose version: 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我在数据库访问中的用户名和密码很简单,没有任何特殊字符,但总是得到同样的错误:

My whitelist settings include only 0.0.0.0/0 My username and password in the Database access is very simple and without any special characters, but always get the same error:

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

非常感谢.

推荐答案

您可以尝试以下步骤:

  1. 点击网络访问.

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

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

This way you will be able to access your database from any IP address.

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

Let me know if it works out for you :)

这篇关于MongoDB:“无法连接到您的 MongoDB Atlas 集群(白名单)中的任何服务器"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆