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

问题描述 投票:0回答:1

我无法将猫鼬与我的应用程序连接。它显示错误“MongooseServerSelectionError:无法连接到 MongoDB Atlas 集群中的任何服务器。一个常见原因是您尝试从未列入白名单的 IP 访问数据库。请确保您当前的 IP 地址是在您的 Atlas 集群的 IP 白名单上”

当我将IP地址设置为0.0.0.0时,它可以正常工作。但这种方法只有在开发时才有效。我想通过我当前的 IP 地址连接数据库。

这是代码。

import mongoose from "mongoose"

const Connection = async () => {
    const URL = `mongodb+srv://*****:*****@chittagongpestcontrol.hviumcx.mongodb.net/?retryWrites=true&w=majority`;
    try {
        await mongoose.connect(URL, { useNewUrlParser: true})
        console.log('Databease connect successfully.')
    } catch(error) {
        console.log('Error while connecting with the database.',error);
    }
}

export default Connection;

它显示的错误:

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://www.mongodb.com/docs/atlas/security-whitelist/
    at _handleConnectionErrors (D:\Web\Node JS project\chittagongepestcontrol\server\node_modules\mongoose\lib\connection.js:792:11)
    at NativeConnection.openUri (D:\Web\Node JS project\chittagongepestcontrol\server\node_modules\mongoose\lib\connection.js:767:11)
    at async Connection (file:///D:/Web/Node%20JS%20project/chittagongepestcontrol/server/database/db.js:6:9) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-jodubob-shard-00-01.hviumcx.mongodb.net:27017' => [ServerDescription],
      'ac-jodubob-shard-00-00.hviumcx.mongodb.net:27017' => [ServerDescription],
      'ac-jodubob-shard-00-02.hviumcx.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-bwulgu-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}
node.js mongodb express
1个回答
0
投票

从 MONGO Atlas 的网络访问添加您当前的 IP 地址。然后重新启动服务器,事情就会得到解决

© www.soinside.com 2019 - 2024. All rights reserved.