连接到 Mongo 时 WSL2 中出现连接错误

Connection error in WSL2 when connecting to Mongo

我有一个 MongoDB 云的 POC 帐户,我正尝试使用 mongo shell 连接到该帐户。 提供通行证后,我得到以下错误。转换回 WSL1 是一个修复,但我宁愿留在 WSL2。

当我做简单的 ping google.com 时,一切似乎都正常。

MongoDB shell version v3.6.8
Enter password:
connecting to: mongodb+srv://yyyy-yyy-yyyy.yyyyy.mongodb.net/node_db
2020-11-28T13:20:18.780+0100 I NETWORK  [thread1] Starting new replica set monitor for atlas-qtxfdg-shard-0/yyyy-yyy-shop-yyy-00-02.yyyy.mongodb.net.:27017,yyyy-yyy-yyyy-shard-00-00.yyyyy.mongodb.net.:27017,yyy-yyyy-yyyy-yyyy-00-01.yyyyy.mongodb.net.:27017
2020-11-28T13:20:23.848+0100 W NETWORK  [thread1] Failed to connect to 20.67.41.218:27017 after 5000ms milliseconds, giving up.
2020-11-28T13:20:23.914+0100 W NETWORK  [ReplicaSetMonitor-TaskExecutor-0] Failed to connect to 20.56.152.73:27017 after 5000ms milliseconds, giving up.
2020-11-28T13:20:28.940+0100 W NETWORK  [thread1] Failed to connect to 20.61.168.228:27017 after 5000ms milliseconds, giving up.
2020-11-28T13:20:28.940+0100 W NETWORK  [thread1] Unable to reach primary for set atlas-qtxfdg-shard-0
2020-11-28T13:20:28.940+0100 I NETWORK  [thread1] Cannot reach any nodes for set atlas-qtxfdg-shard-0. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.
2020-11-28T13:20:34.449+0100 W NETWORK  [thread1] Failed to connect to 20.67.41.218:27017 after 5000ms milliseconds, giving up.
2020-11-28T13:20:39.459+0100 W NETWORK  [thread1] Failed to connect to 20.61.168.228:27017 after 5000ms milliseconds, giving up.
2020-11-28T13:20:44.468+0100 W NETWORK  [thread1] Failed to connect to 20.56.152.73:27017 after 5000ms milliseconds, giving up.
2020-11-28T13:20:44.468+0100 W NETWORK  [thread1] Unable to reach primary for set atlas-qtxfdg-shard-0
2020-11-28T13:20:44.468+0100 I NETWORK  [thread1] Cannot reach any nodes for set atlas-qtxfdg-shard-0. Please check network connectivity and the status of the set. This has happened for 2 checks in a row.
2020-11-28T13:20:44.471+0100 E QUERY    [thread1] Error: connect failed to replica set atlas-qtxfdg-yyyy-0/yyyy-yyyy-yyyy-yyyyy-00-02.yyyyyy.mongodb.net.:27017,yyyy-yyyy-yyy-yyyyy-00-00.yyyyy.mongodb.net.:27017,yyyy-yyyy-yyyy-yyyy-00-01.yyyyyy.mongodb.net.:27017 :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

好的,问题出在 Avast 中的防火墙设置上。

我找到的解决方案:https://github.com/MicrosoftDocs/WSL/issues/481 状态:

go to Settings -> Protection -> Firewall -> Check the "Internet Connection Sharing Mode".

如果网络是 public 或私有网络,请检查 Windows 设置,每个网络的防火墙配置可能不同(通常对 public 网络有更多限制)。我与来自 WSL 2 的 Mongo Atlas 有连接问题,这是因为网络为 public,所以一些 Node JS 操作被防火墙阻止(Windows Defender 在我的设置中).