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

i1icjdpr  于 2022-11-13  发布在  Go
关注(0)|答案(4)|浏览(261)

我有一个问题,我试图修复了很长一段时间。我试图连接到Mongo Atlas云从nodejs通过mongoose。这不是我的第一次,但我就是找不到答案。
Mongoose 版: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我的用户名和密码在Database中的访问非常简单而且没有任何特殊字符,但总是得到同样的错误:

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

谢谢你,谢谢你

dfddblmv

dfddblmv1#

您可以尝试以下步骤:
1.单击网络访问。

1.单击位于删除按钮旁边的编辑按钮。
1.点击“添加当前IP地址”按钮,然后点击确认。
这样,您将能够从任何IP地址访问您的数据库。
让我知道它是否适合你:)

muk1a3rh

muk1a3rh2#

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

'AUTHENTICATION_DATABASE', null
'DATABASE_SSL', true

在strapi数据库.js中:

options: {
    authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
    ssl: env.bool('DATABASE_SSL', true),
  },
8wtpewkr

8wtpewkr3#

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

dtcbnfnu

dtcbnfnu4#

我也遇到过同样的问题。不要从你的终端或网络偏好中添加你的ip地址。相反...
1.转到MongoDb网络访问,然后点击编辑IP地址,或添加IP地址,如果你还没有添加任何.

1.点击添加当前的IP地址,然后点击确认... vuala你设置。

相关问题