【问题标题】:why MongoDB database connection timedout error in DigitalOcean为什么 DigitalOcean 中的 MongoDB 数据库连接超时错误
【发布时间】:2021-02-16 17:00:37
【问题描述】:

当我尝试连接到 mongodb 时出现超时错误, 我为远程访问添加了防火墙规则。 我做了什么:

传入规则 => 添加新规则 => 自定义 TCP | 27017 |所有 IPv4 所有 IPv6。

mongo 服务器正在运行,用户在特定数据库中有规则。

注意:我之前在 AWS 上使用相同的步骤成功运行过它。

我确定我得到的错误是关于远程访问 IP 的。

错误:

MongooseServerSelectionError: connection timed out
    at NativeConnection.Connection.openUri (C:\scrap-source\node_modules\mongoose\lib\connection.js:845:32)
    at C:\scrap-source\node_modules\mongoose\lib\index.js:345:10
    at promiseOrCallback (C:\scrap-source\node_modules\mongoose\lib\helpers\promiseOrCallback.js:9:12)
    at Mongoose._promiseOrCallback (C:\scrap-source\node_modules\mongoose\lib\index.js:1135:10)
    at Mongoose.connect (C:\scrap-source\node_modules\mongoose\lib\index.js:344:20)
    at Object.<anonymous> (C:\scrap-source\server.js:8:5)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  reason: TopologyDescription {
    type: 'Single',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(1) { 'My_private_IP':27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}

谢谢。

【问题讨论】:

  • 如果数据库确实在您的 My_private_IP 上运行,则无法从您的 VPC|数据中心外部访问。您需要使用公共 IP。
  • 它运行良好,我在 amazon aws 上使用了相同的步骤并且它正在工作,但在 digitalOcean 我遇到了这个问题。

标签: mongodb digital-ocean


【解决方案1】:

我按照以下步骤解决了这个问题: 通过键入显示监听 mongodb 默认端口

sudo lsof -i | grep mongo

然后通过输入允许远程 ip

sudo ufw allow from trusted_server_ip to any port 27017

以下注释完全解决了我的问题: 注意:(例如,如果您使用 0.0.0.0,则允许 0.0.0.0 和 0.0.0.0/0)

现在通过键入查看更改

sudo ufw status

现在打开 mongod.conf 文件

sudo nano /etc/mongod.conf

改变

. . .
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1

. . .

. . .
# network interfaces
net:
  port: 27017
  bindIp: your_ip

. . .

现在重启服务器

sudo systemctl restart mongod

更多信息见 https://www.digitalocean.com/community/tutorials/how-to-configure-remote-access-for-mongodb-on-ubuntu-20-04

【讨论】:

    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 2014-03-03
    • 2023-03-27
    • 1970-01-01
    • 2020-11-27
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多