无法连接Atlas MongoDB示例

4sup72z8  于 2023-03-22  发布在  Go
关注(0)|答案(1)|浏览(192)

使用Express连接atlas mongodb示例出错

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 Connection.openUri (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/connection.js:825:32)
    at /Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:411:10
    at /Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
    at Mongoose._promiseOrCallback (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:1285:10)
    at Mongoose.connect (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:410:20)
    at connectDB (/Users/aviralgupta/Development/NodeJs/MongoDB/config/dbConfig.js:8:24)
    at Object.<anonymous> (/Users/aviralgupta/Development/NodeJs/MongoDB/index.js:12:1)
    at Module._compile (node:internal/modules/cjs/loader:1275:14) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-a6to6lu-shard-00-00.nfmpr49.mongodb.net:27017' => [ServerDescription],
      'ac-a6to6lu-shard-00-01.nfmpr49.mongodb.net:27017' => [ServerDescription],
      'ac-a6to6lu-shard-00-02.nfmpr49.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-tvdhx4-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}
Error in dbConfig.js

dbConfig.js

const mongoose = require("mongoose");

mongoose.set('strictQuery', true);

const connectDB = async () => {

    try {
        await mongoose.connect(process.env.MONGO_URL, {
            useUnifiedTopology: true, 
            useNewUrlParser: true,
        });

    } catch (error) {
        console.error(error);
        console.error("Error in dbConfig.js");
    }

}

module.exports = connectDB;

正如它提到的you're trying to access the database from an IP that isn't whitelisted,我试图添加我当前的IP地址,但仍然显示相同的问题。
此外,这个项目是运行完美的罚款一个星期前,但现在它不工作,也有一件事要注意的是,我没有改变我的互联网连接时,我建立,测试它,但现在它不工作。

j8yoct9x

j8yoct9x1#

现在,在您获得static IP之前,只需允许任何IP地址,以避免每次添加新IP的麻烦。

您不需要更改ISP(Internet服务提供商)来获得不同的IP address。每次连接到Internet时,您的ISP(Internet服务提供商)都会给您一个IP地址。
访问Mongo DB Jmeter 板,按下按钮将IP白名单更改为您当前的IP。

相关问题