【问题标题】:MongoServerSelectionError: connection <monitor> to xx.xxx.xxx.xxx:27017 closed (whitelisting IP and opening the port didn't work)MongoServerSelectionError: connection <monitor> to xx.xxx.xxx.xxx:27017 closed(将IP列入白名单并打开端口不起作用)
【发布时间】:2022-07-05 01:53:58
【问题描述】:

我正在学习 MongoDB,很抱歉打扰你,但我收到了这个错误:

MongoServerSelectionError: connection <monitor> to xx.xxx.xxx.xxx:27017 closed
at Timeout._onTimeout (C:\...\node_modules\mongodb\lib\sdam\topology.js:305:38)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
  'ac-c9obg9r-shard-00-00.onq7cwz.mongodb.net:27017' => [ServerDescription],
  'ac-c9obg9r-shard-00-02.onq7cwz.mongodb.net:27017' => [ServerDescription],
  'ac-c9obg9r-shard-00-01.onq7cwz.mongodb.net:27017' => [ServerDescription]
},


stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-up12ch-shard-0',
logicalSessionTimeoutMinutes: undefined
},

  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

我尝试启用端口 27017 并在网络访问选项卡中重置 ip(已经被列入白名单),但没有运气,错误仍然存​​在。重新安装了我使用的模块,什么也没有。 我的代码昨天还在工作,但是在 Windows 更新后我无法连接(这就是我认为它是端口的原因)。 我替换的 xx.xxx.xxx.xxx:27017 的数字不是我的 ip 号码,我不知道这是否有帮助。

如果你有任何想法,我很感激你的意见。

【问题讨论】:

    标签: database mongodb connection database-connection


    【解决方案1】:
    MongoServerSelectionError: connection <monitor> to xx.xxx.xxx.xxx:27017 closed 
        at Timeout._onTimeout (C:\...\node_modules\mongodb\lib\sdam\topology.js:305:38)
        at listOnTimeout (node:internal/timers:559:17)
        at processTimers (node:internal/timers:502:7)
    

    我在尝试使用新的 Node 服务器连接到 MongoDB 时遇到了同样的错误。

    查看您是否将网络连接更改为与您在 MongoDB 中列入白名单的网络不同的网络。 如果是这样,whitelist 当前 IP 地址 在 mongoDB 中。 (PS:我看到您已经尝试在“网络访问”选项卡中重置 IP 地址,但请再次检查。这是我修复它的方法。)

    另外,检查 .env 文件 variables 是否使用 MongoDB 链接正确声明,并更改密码和数据库名称。

    如果问题仍未解决,我建议您删除旧集群并在 MongoDB 中创建一个新集群。同时重新初始化节点和包。

    希望这能解决您的问题。

    【讨论】:

    • 感谢 Ramesan 的意见。我在 google 上找到了类似问题的答案并最终工作:将 { useUnifiedTopology: true }) 更改为 { useUnifiedTopology: false }) 如果有人遇到同样的问题,我建议您这样做,保存并重新启动服务器。跨度>
    【解决方案2】:

    对我来说,发生此错误是因为我的连接字符串错误。具体来说 - 我从我正在学习的课程中复制了示例连接字符串,并用我的凭据替换了用户名和密码。因此,凭据是正确的,但连接字符串的其余部分不正确。

    只是为了理解。请看下面:

    mongodb+srv://myusername:mypassword@courseproject.h1mpg.mongodb.net/?retryWrites=true&amp;w=majority"

    myusernamemypassword 是正确的,即属于我的 atlas 帐户中的集群,但字符串的其余部分是错误的,因为我从某个地方复制它而不是从它复制它我自己的 MongoDB atlas 帐户。

    所以请务必仔细检查您的整个连接字符串是否正确。

    【讨论】:

      猜你喜欢
      • 2020-10-02
      • 2021-06-22
      • 1970-01-01
      • 2015-05-25
      • 2018-06-06
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多