繁体   English   中英

AWS EC2 实例(生产)中的 PostGres 超时错误

[英]PostGres Timeout error in AWS EC2 Instance (Production)

我是后端的新手,这是我对 Postgres 的配置

const pool = new Pool({
      user: "username",
      host: "hostname",
      database: "dbname",
      password: "postgres",
      port: 5432,
      max: 10,
      min: 10,
      statement_timeout: 10000,
      idleTimeoutMillis: 30000,
      connectionTimeoutMillis: 2000,
      maxUses: 7500,
    });

    console.log("requesting db");
    pool.connect((err, client, release) => {
      console.error("Error acquiring client", err);
      console.error("Error acquiring client", client);
      console.error("Error acquiring client", release);
      if (err) {
        return console.error("Error acquiring client", err.stack);
      }
    });
    pool.on("connect", () => {
      console.log("connected to the db");
    });
    pool.on("error", function (err, client) {
      console.log(client);
      console.log(err);
    });
    module.exports = pool;

在生产中,我面临以下错误,但它在本地工作我尝试在我的本地机器上连接我的产品数据库它工作正常

Error acquiring client Error: Connection terminated due to connection timeout
at Connection.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/client.js:255:9)
at Object.onceWrapper (events.js:421:28)
at Connection.emit (events.js:315:20)
at Socket.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/connection.js:78:10)
at Socket.emit (events.js:315:20)
at emitCloseNT (net.js:1656:8)
at processTicksAndRejections (internal/process/task_queues.js:83:21)
at runNextTicks (internal/process/task_queues.js:66:3)
at listOnTimeout (internal/timers.js:518:9)
at processTimers (internal/timers.js:492:7)
Error acquiring client undefined
Error acquiring client [Function: NOOP]
Error acquiring client Error: Connection terminated due to connection timeout
    at Connection.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/client.js:255:9)
    at Object.onceWrapper (events.js:421:28)
    at Connection.emit (events.js:315:20)
    at Socket.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/connection.js:78:10)
    at Socket.emit (events.js:315:20)
    at emitCloseNT (net.js:1656:8)
    at processTicksAndRejections (internal/process/task_queues.js:83:21)
    at runNextTicks (internal/process/task_queues.js:66:3)
    at listOnTimeout (internal/timers.js:518:9)
    at processTimers (internal/timers.js:492:7)

以下路线在生产中运行良好,但未建立与 postgres 的连接

 app.get("/api/v1/test", function (req, res) {
      res.send("Hello World test!");
    });

我的 EC2 配置

入站规则

在此处输入图像描述 出站规则

在此处输入图像描述

我的 RDS 配置

入站规则

在此处输入图像描述

出站规则

在此处输入图像描述

我怀疑 RDS 实例没有允许来自您的 EC2 实例的连接的安全组。 请参阅此帮助文章: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html

如果您的 RDS 实例具有公共 IP 地址,则域名将解析为该地址。 如果您的 EC2 实例和 RDS 实例都托管在同一个 VPC 中,那么我建议禁用公共 IP 地址。 这可能会使从您的个人计算机连接到数据库变得更加困难,但在这种情况下,您可以使用 SSH 隧道来完成。

设置 RDS 供公共访问时,仅打开安全组中的端口是不够的。

您还需要 select 是的“公共可访问”,它隐藏在“附加连接配置”部分中(默认情况下关闭,因此很容易忽略)。

确保你所有需要的包在你本地机器的版本中都相同(节点、Npm、Postgres)

暂无
暂无

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

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