【问题标题】:I am not able to connect to gcloud sql from the nodejs app hosted in app engine我无法从应用引擎中托管的 nodejs 应用连接到 gcloud sql
【发布时间】:2019-10-19 17:24:28
【问题描述】:

我在谷歌云上有一个托管节点应用程序和 mysql。我尝试了所有可能的方法来从节点连接 mysql,但每次它都会抛出这个错误。

    let pool;
    const createPool = async () => {
      pool = await mysql.createPool({
       //   host: "35.200.129.217",
    socketPath:"/cloudsql/idyllic-anvil-256103:asia-south1:database",
      user: "abc@gmail.com",
      password: "pass",
      database: "db"
      });
    };
    createPool();

    app.get("/getnews", (request, response) => {
    createPool.query('SELECT * FROM db.mydb', function (err, result) {
        if (err) throw new Error(err)
        response.send(result);
    })
    });

错误:

    2019-10-19 16:33:40 default[20191019t215943]  Error: Error: connect ETIMEDOUT      at Query._callback (/srv/index.js:42:20)      at Query.Sequence.end (/srv/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)      at /srv/node_modules/mysql/lib/Pool.js:205:13      at Handshake.onConnect (/srv/node_modules/mysql/lib/Pool.js:58:9)      at Handshake.<anonymous> (/srv/node_modules/mysql/lib/Connection.js:525:10)      at Handshake._callback (/srv/node_modules/mysql/lib/Connection.js:491:16)      at Handshake.Sequence.end (/srv/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)      at Protocol.handleNetworkError (/srv/node_modules/mysql/lib/protocol/Protocol.js:369:14)      at PoolConnection.Connection._handleNetworkError (/srv/node_modules/mysql/lib/Connection.js:421:18)      at PoolConnection.Connection._handleConnectTimeout (/srv/node_modules/mysql/lib/Connection.js:417:8)
    2019-10-19 16:34:16 default[20191019t220300]  "GET / HTTP/1.1" 304
    2019-10-19 16:34:18 default[20191019t220300]  Server listening on port 8081...
    2019-10-19 16:34:18 default[20191019t220300]  Database connection was refused.
    2019-10-19 16:34:22 default[20191019t220300]  "GET /getnews HTTP/1.1" 502
    2019-10-19 16:34:22 default[20191019t220300]  /srv/index.js:42
    2019-10-19 16:34:22 default[20191019t220300]      if (err) throw new Error(err)
    2019-10-19 16:34:22 default[20191019t220300]               ^
    2019-10-19 16:34:22 default[20191019t220300]
    2019-10-19 16:34:23 default[20191019t220300]  Error: Error: connect ECONNREFUSED /cloudsql/idyllic-anvil-256103:asia-south1:database      at Query._callback (/srv/index.js:42:20)      at Query.Sequence.end (/srv/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)      at /srv/node_modules/mysql/lib/Pool.js:205:13      at Handshake.onConnect (/srv/node_modules/mysql/lib/Pool.js:58:9)      at Handshake.<anonymous> (/srv/node_modules/mysql/lib/Connection.js:525:10)      at Handshake._callback (/srv/node_modules/mysql/lib/Connection.js:491:16)      at Handshake.Sequence.end (/srv/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)      at Protocol.handleNetworkError `enter code here`(/srv/node_modules/mysql/lib/protocol/Protocol.js:369:14)      at PoolConnection.Connection._handleNetworkError (/srv/node_modules/mysql/lib/Connection.js:421:18)      at Socket.emit (events.js:198:13)

【问题讨论】:

  • 您是否按照these instructions 进行设置,包括验证应用引擎服务帐户是否具有正确的 IAM 角色?另外,您使用的是标准应用引擎还是灵活应用引擎?
  • 另外,"createPool.query(" 看起来很奇怪,因为 createPool 是您之前调用的一个函数并且没有保存结果。example you seem to be using 定义了一个池变量来保存结果. 你应该使用 pool.query(.
  • 我使用的是标准环境,使用标准环境时我应该在哪里添加默认服务帐户。
  • 我链接的文档提到了需要哪些角色。如上所述,您还应该仔细检查您的代码(但这不太可能是连接问题本身)
  • 我已经为我正在使用的帐户添加了所有者角色,并且我已经使用 pool.query 更新了他的代码

标签: google-app-engine google-cloud-sql google-appengine-node


【解决方案1】:

确保您按照Connecting to Cloud SQL from App Engine 的说明进行操作。尤其是以下几点很容易被忽略:

  • 确保您连接的服务帐户具有Cloud SQL Client IAM 角色或页面上列出的权限
  • 如果使用 App Engine Flex,请确保您在 app.yaml 中拼写正确

最后,请务必查看Managing Database Connections 页面。特别是,在上面的示例中,您正在使用名称初始化一个函数,然后对该函数调用查询。相反,您应该使用池来进行查询:


let pool; // <---- This is the pool that `createPool` will set
const createPool = async () => {
  pool = await mysql.createPool({
    socketPath:"/cloudsql/idyllic-anvil-256103:asia-south1:database",
    user: "abc@gmail.com", //<--- just FYI, this should be the database user, not the GCP account connecting
    password: "pass",
    database: "db"
  });
};
createPool(); // <----- This is where `pool` is actually created


// Now we can use the pool to query like this:
try {
  const stmt = 'INSERT INTO votes (time_cast, candidate) VALUES (?, ?)';
  // Pool.query automatically checks out, uses, and releases a connection
  // back into the pool, ensuring it is always returned successfully.
  await pool.query(stmt, [timestamp, team]);
} catch (err) {
  // If something goes wrong, handle the error in this section. This might
  // involve retrying or adjusting parameters depending on the situation.
  // ...
}

【讨论】:

    猜你喜欢
    • 2021-06-18
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2020-05-18
    相关资源
    最近更新 更多