简体   繁体   English

为什么本地 Robo3t 连接到远程 Mongo 数据库实例,但 NodeJs 失败并出现 IP 白名单错误?

[英]Why does local Robo3t connect to a remote Mongo DB instance, but NodeJs fails with IP whitelist error?

I am having issues connecting to MongoDb running remotely, and the connection error response I am getting from the server is somewhat weird.我在连接到远程运行的 MongoDb 时遇到问题,我从服务器获得的连接错误响应有点奇怪。

My network access whitelist is set to allow all (0.0.0.0/0).我的网络访问白名单设置为允许所有 (0.0.0.0/0)。 Hence, my local robo3t installation was able to connect.因此,我的本地 robo3t 安装能够连接。 However, I could not connect from my NodeJs code.但是,我无法从我的 NodeJs 代码连接。 Error is: "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"错误是:“MongooseServerSelectionError:无法连接到您的 MongoDB Atlas 集群中的任何服务器。一个常见原因是您尝试从未列入白名单的 IP 访问数据库。确保您当前的 IP 地址在您的 Atlas 上集群的IP白名单”

IP whitelist seems to be an unlikely error, given that my local robo3t client is able to connect remotely to the same remote Mongo Atlas instance, as IP whitelist is allow-all. IP 白名单似乎是一个不太可能的错误,因为我的本地 robo3t 客户端能够远程连接到同一个远程 Mongo Atlas 实例,因为 IP 白名单是允许的。

How do I debug this kind of thing, please?请问这种情况如何调试?

UPDATE: this is how I connect to MongoDb.更新:这就是我连接到 MongoDb 的方式。 Works well on local, too.在本地也运行良好。

try {
    const connectionString =
        process.env.APP_ENV == "test"
            ? await getInMemoryMongoDbAdapter()
            : `mongodb://${process.env.MONGODB_HOSTNAME}:${process.env.MONGODB_PORT}/${process.env.CBT_DATABASE_NAME}`;

    logger.info(`Connecting to MongoDB service: ${connectionString}`);
    

    await mongoose.connect(connectionString, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    });
} catch (error) {
    reject(error);
}

The logger line correctly shows: Connecting to MongoDB service: mongodb://<user>:<password>@cluster0-xxx.yyy.zzz.net:<port>/<database>记录器行正确显示: Connecting to MongoDB service: mongodb://<user>:<password>@cluster0-xxx.yyy.zzz.net:<port>/<database>

UPDATE 2 : My localhost also does not connect via this node app;更新 2 :我的本地主机也没有通过这个节点应用程序连接; whereas my robo3t (local MongoDb client) connects.而我的 robo3t(本地 MongoDb 客户端)连接。 I guess that means Heroku-specific issues can now be comfortably ruled out我想这意味着现在可以轻松排除 Heroku 特定的问题

A decade later, I found that for the connection parameters, I needed to supply the authSource and ssl options, as below:十年后,我发现对于连接参数,我需要提供authSourcessl选项,如下所示:

{
    useNewUrlParser: true,
    useUnifiedTopology: true,
    authSource: "admin",
    ssl: true,
}

Neither one works without the other.如果没有另一个,任何一个都不起作用。 Big shout-out to @darklightcode for all the insights he gave, leading me to dig deeper.@darklightcode的所有见解大喊大叫,让我深入挖掘。 Thanks man!谢啦!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM