node.js 连接到 MongoDb Atlas 服务器时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49894913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:46:15  来源:igfitidea点击:

Error at connecting to MongoDb Atlas Server

node.jsmongodbmongoosemongodb-atlas

提问by Lucas Engleder

So I'm currently wirting a web application and I need database so I decided to go with mongodb and mongoose. Until now I tested everything on localhost and it worked but I wanted to move the data to a server. I had heard of Atlas and registered myself and "uploaded" the data.

所以我目前正在编写一个 Web 应用程序,我需要数据库,所以我决定使用 mongodb 和 mongoose。到目前为止,我在 localhost 上测试了所有内容并且它工作正常,但我想将数据移动到服务器。我听说过 Atlas 并注册了自己并“上传”了数据。

Now I want to connect to the cluster via a node.js app.

现在我想通过 node.js 应用程序连接到集群。

mongoose.connect('mongodb+srv://engllucas:p%[email protected]/test');

I got the string of the mongodb Atlas site at the point Connect Your Application

我在Connect Your Application点得到了 mongodb Atlas 站点的字符串

Then I swapped my password.

然后我换了密码。

mongoose.connect('mongodb://engllucas:p%[email protected]/test');

This didn't work either.

这也不起作用。

Thats the error message:

那是错误信息:

{ MongoError: failed to connect to server [insight-shard-00-02-quhku.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 18.194.163.64:27017]
at Pool.<anonymous> (U:\WEBT\ProjectInsight\Quiz\node_modules\mongodb-core\lib\topologies\server.js:503:11)
at emitOne (events.js:115:13)
at Pool.emit (events.js:210:7)
at Connection.<anonymous> (U:\WEBT\ProjectInsight\Quiz\node_modules\mongodb-core\lib\connection\pool.js:326:12)
at Object.onceWrapper (events.js:318:30)
at emitTwo (events.js:125:13)
at Connection.emit (events.js:213:7)
at TLSSocket.<anonymous> (U:\WEBT\ProjectInsight\Quiz\node_modules\mongodb-core\lib\connection\connection.js:245:50)
at Object.onceWrapper (events.js:316:30)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
  name: 'MongoNetworkError',

message: 'failed to connect to server [insight-shard-00-02-quhku.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 18.194.163.64:27017]' } (node:4920) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoNetworkError: failed to connect to server [insight-shard-00-02-quhku.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 18.194.163.64:27017] (node:4920) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

消息:'无法连接到服务器 [insight-shard-00-02-quhku.mongodb.net:27017] 首次连接 [MongoNetworkError: connect ECONNREFUSED 18.194.163.64:27017]' } (node:4920) UnhandledPromiseRejectionWarning: UnhandledPromiseRejectionWarning拒绝(拒绝 ID:1):MongoNetworkError:在第一次连接时无法连接到服务器 [insight-shard-00-02-quhku.mongodb.net:27017] [MongoNetworkError: connect ECONNREFUSED 18.194.163.64:27017](节点:4920) ) [DEP0018] 弃用警告:不推荐使用未处理的承诺拒绝。将来,未处理的承诺拒绝将使用非零退出代码终止 Node.js 进程。

回答by Waweru wa Kamau

I also had a similar issue, and I was able to solve it by adding my white-listing my IP address under Clusters -> security -> IP Whitelist. Instead of clicking my current ip address, just search what's my ip on google and paste that instead. I hope it works!

我也遇到了类似的问题,我能够通过在集群 -> 安全 -> IP 白名单下添加我的 IP 地址白名单来解决它。而不是点击my current ip address,只需在谷歌上搜索我的 ip 并粘贴它。我希望它有效!

回答by prestondoris

I have had this issue connecting to a MongoDB Atlas cluster. I have had to resolve this issue twice because I was working on a project from two different computers (a Macbook Pro and a Windows PC).

我在连接到 MongoDB Atlas 集群时遇到了这个问题。我不得不解决这个问题两次,因为我正在使用两台不同的计算机(Macbook Pro 和 Windows PC)处理一个项目。

Here is my comprehensive answer for everything that I have figured out to resolve the issue of my application not connecting to the cluster.

这是我为解决我的应用程序未连接到集群的问题而想出的所有内容的综合答案。

FirstMake sure your IP is whitelisted like the above suggestions. The easiest solution is the "All IPs" whitelist of 0.0.0.0/0

首先确保您的 IP 像上述建议一样被列入白名单。最简单的解决方案是0.0.0.0/0的“所有IP”白名单

SecondCheck your VPN if you are using one. I was trying to connect to the cluster while connected to a VPN. Once I turned off my VPN, I was able to connect.

第二如果您正在使用 VPN,请检查您的 VPN。我在连接到 VPN 时尝试连接到集群。一旦我关闭了我的 VPN,我就可以连接了。

ThirdMake sure your internet settings are not preventing you from connecting to the cluster. My home Xfinity wifi security settings were set too high and this was preventing the connection. I think the router was not allowing a connection to port 27017. One way to test if this issue is occurring is to tether your computer to your phone for internet instead of your wifi and try connecting. I was able to connect using my iPhone as a hotspot for internet. I reset my router to factory settings which fixed the issue.

第三确保您的互联网设置没有阻止您连接到集群。我的家庭 Xfinity wifi 安全设置设置得太高,这阻止了连接。我认为路由器不允许连接到端口 27017。测试是否发生此问题的一种方法是将您的计算机连接到您的手机以进行互联网而不是您的 wifi 并尝试连接。我能够使用我的 iPhone 作为互联网热点进行连接。我将路由器重置为出厂设置,解决了问题。

回答by lineus

if you are using mongoose previous to version 5.0.15:

如果您使用的是 5.0.15 版之前的 mongoose:

themongodb+srv://uri is not valid to the underlying native driver.

mongodb+srv://URI是无效的底层本地驱动程序

just removing the +srv will not work either, as the uri will need to include your replica hosts and the replicaSet/authSource parameters.

仅删除 +srv 也不起作用,因为 uri 将需要包含您的副本主机和 replicaSet/authSource 参数。

you can either

你可以

1. Upgrade to mongoose 5.0.15 and use the shorter +srv format

1.升级到mongoose 5.0.15并使用更短的+srv格式

or

或者

2. Use your current version with the full uri from your Atlas cluster dashboard, selecting the older driver(see Notebelow) version uri.

2. 使用您当前版本的 Atlas 集群仪表板中的完整 uri,选择较旧的驱动程序(请参阅下面的注释版本 uri。

from your cluster dashboard, press the connect button, then (assuming you have whitelisted your node server's IP ) select "connect your application", then press "I am using 3.4 driver or older". Use the resulting uri string substituting your password.

在您的集群仪表板中,按下连接按钮,然后(假设您已将节点服务器的 IP 列入白名单)选择“连接您的应用程序”,然后按下“我使用的是 3.4 或更低版本的驱动程序”。使用生成的 uri 字符串代替您的密码。

Note:In the dialog for choosing your URI string in the connect helper on the Atlas cluster dashboard, it says "I am using driver 3.* or (newer|older)". This is a poor choice of words, as the 3.6 and 3.4 don't refer to the driver version, but the server version. Further, using one version of the server or the other has nothing to do with the driver being used to connect, which is where the actual dependency lies that determines which version of the URI to use.

注意:在 Atlas 集群仪表板上的连接助手中选择 URI 字符串的对话框中,显示“我正在使用驱动程序 3.* 或(较新|较旧)”。这是一个糟糕的选择,因为 3.6 和 3.4 不是指驱动程序版本,而是指服务器版本。此外,使用服务器的一个版本或另一个版本与用于连接的驱动程序无关,这是确定要使用哪个版本的 URI 的实际依赖关系所在。

回答by Sohan

It's an issue of your "IP Whitelist". Edit and update it once, it should work fine then.

这是您的“IP 白名单”的问题。编辑并更新一次,它应该可以正常工作。

回答by Pramod Kharade

Above answer is right but just i want to added one point:

以上答案是正确的,但我想补充一点:

From security perspective it is not good;

从安全的角度来看,这是不好的;

  1. I also had a similar issue, and I was able to solve it by adding my white-listing my IP address under Clusters -> security -> IP Whitelist.Instead of clicking my current ip address, just search what's my ip on google and paste that instead. I hope it works!

  2. I also had a similar issue, and I was able to solve it by adding my white-listing my IP address under Clusters -> security -> IP Whitelist.added this IP in list: 0.0.0.0/0it will open to all.

  1. 我也遇到了类似的问题,我能够通过将我的 IP 地址添加到白名单中来解决它,Clusters -> security -> IP Whitelist.而不是单击我的current ip address,只需在 google 上搜索我的 ip 并粘贴它即可。我希望它有效!

  2. 我也有一个类似的问题,我能够通过Clusters -> security -> IP Whitelist.在列表中添加这个 IP下添加我的白名单我的 IP 地址来解决它:0.0.0.0/0它将向所有人开放。

回答by Mukesh Arora

I also faced the similar issue while I was connecting MongoDB Compass to https://cloud.mongodb.com/cluster.

我在将 MongoDB Compass 连接到https://cloud.mongodb.com/集群时也遇到了类似的问题。

The following steps resolve the issue.

以下步骤可解决此问题。

  1. Visit google.com > Enter My IP Address > Copy the Public IP Address of your system/network
  2. Go to https://cloud.mongodb.com/Cluster > Select Security > Network Access > Click on IP whilelist Tab > Add IP Address > Enter your public IP address.
  1. 访问 google.com > 输入我的 IP 地址 > 复制系统/网络的公共 IP 地址
  2. 转到https://cloud.mongodb.com/集群 > 选择安全 > 网络访问 > 单击 IP whilelist 选项卡 > 添加 IP 地址 > 输入您的公共 IP 地址。

回答by Raunak Singha

Apparently, changing

显然,改变

mongoose.connect(keys.mongoURI);

to

mongoose.connect(keys.mongoURI, () => { }, { useNewUrlParser: true })
    .catch(err => {
        console.log(err);
    });

worked for me.

对我来说有效。

回答by PleatherShaman

Check that your current IP address is on the MongoAtlas IP whitelist. I came across this problem after I updated my VPN.

检查您当前的 IP 地址是否在 MongoAtlas IP 白名单中。我更新 VPN 后遇到了这个问题。

回答by Arunas Bartisius

I had same problem, while IP whitelisting was set correctly!

我有同样的问题,而 IP 白名单设置正确!

The reason was, that MongoDB Enterprise Atlas requires SSL excryption layer trying to connect without that, produces non-reasonable and not self explanatory error like:

原因是 MongoDB Enterprise Atlas 需要 SSL 解密层尝试在没有它的情况下进行连接,会产生不合理且不自我解释的错误,例如:

Failed to connect to mongodb-m0-nnxxx.mongodb.net:27017
No chance to Authorize

Trying to connect using mondodb client with IP whitelisting being the problem, error message like this is appearing:

尝试使用 mondodb 客户端进行连接,IP 白名单是问题所在,出现如下错误消息:

mongo "mongodb+srv://mongodb-m0-nnxxx.mongodb.net/test" --username admin

DBClientConnection failed to receive message from mongodb-m0-shard-00-00-nnxxx.mongodb.net.:27017 - HostUnreachable: Connection closed by peer
Unable to reach primary for set mongodb-m0-shard-0
Cannot reach any nodes for set mongodb-m0-shard-0. Please check network connectivity and the status of the set. This has happened for 5 checks in a row.

For example using Robo 3T client, the checkbox "Use SSL Protocol" must be enabled, and **Authentication Method: Self-Signed Certificate **

例如使用 Robo 3T 客户端,必须启用复选框“使用 SSL 协议”,并且**身份验证方法:自签名证书**

Test produces Diagnostic output:

测试产生诊断输出:

v Connected to mongodb-m0-nnxxx.mongodb.net:27017 via SSL tunnel
v Authorized on admin database as admin

Hope this helps somebody.

希望这可以帮助某人。

回答by Emre Mutlu

I was getting that problem and I fixed it. Below is the steps that I used:

我遇到了这个问题,我解决了它。以下是我使用的步骤:

  1. First of all add new user in mongodb (Security > Database Access > ADD NEW USER > )
  2. then write to .env file new username and password.
  3. Kill the server and rerun the server.
  1. 首先在 mongodb 中添加新用户(Security > Database Access > ADD NEW USER > )
  2. 然后写入 .env 文件新的用户名和密码。
  3. 杀死服务器并重新运行服务器。

This is the solution that worked for me.

这是对我有用的解决方案。