ETIMEOUT错误| 带有NodeJS的Google Cloud SQL数据库

Meg*_*gan 3 mysql google-app-engine node.js google-cloud-sql gcloud

我已经在Google云上创建了一个mysql数据库,我想从一个单独的节点Web应用程序(也在Google Cloud上运行)访问该数据库。我首先在计算机上本地测试连接,然后在本地运行以下代码时,我可以成功建立与数据库的连接并查看其中的数据。

'use strict';

// [START app]
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const mysql      = require('mysql');

var connection = mysql.createConnection({
  host     : 'Cloud SQL IP',
  user      : 'username',
  password  : 'password',
  database  : 'db_name'
});



// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

// Make globals.js accessible
app.use(express.static(__dirname + '/'));


app.get('/', (req, res) => {
    connection.connect();

    connection.query('SELECT * FROM Users', function (error, results, fields) {
        if (error) throw error;
        console.log(results);
    });

    connection.end();
    res.status(200).send('Hello World!');
});

app.get('/login', (req, res) => {
    res.status(200).send();
});

// [START server]
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});
// [END app]
Run Code Online (Sandbox Code Playgroud)

但是,当在我的Google App引擎中运行相同的代码(用于在端口8080上进行调试并在https://myapp.appspot.com上完全部署)时,出现以下超时错误:

{ Error: connect ETIMEDOUT
    at Connection._handleConnectTimeout         (/home/megan_cooper2900/project/node_modules/mysql/lib/Connection.js:419:13)
at Socket.g (events.js:292:16)
at emitNone (events.js:86:13)
at Socket.emit (events.js:185:7)
at Socket._onTimeout (net.js:338:8)
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5)
--------------------
at Protocol._enqueue (/home/megan_cooper2900/project/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/megan_cooper2900/project/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/megan_cooper2900/project/node_modules/mysql/lib/Connection.js:130:18)
at app.get (/home/megan_cooper2900/journeyma/app.js:31:13)
at Layer.handle [as handle_request] (/home/megan_cooper2900/project/node_modules/express/lib/router/layer.js:95:5)
at next (/home/megan_cooper2900/project/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/megan_cooper2900/project/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/megan_cooper2900/project/node_modules/express/lib/router/layer.js:95:5)
at     /home/megan_cooper2900/project/node_modules/express/lib/router/index.js:281:22
    at Function.process_params         (/home/megan_cooper2900/project/node_modules/express/lib/router/index.js:335:12)
  errorno: 'ETIMEDOUT',
  code: 'ETIMEDOUT',
  syscall: 'connect',
  fatal: true }
Run Code Online (Sandbox Code Playgroud)

为什么这在Google App Engine应用程序上不起作用?

Kob*_*her 7

在mysql的连接配置中,host不适用于App Engine。您必须使用socketPath。socketPath是要连接到的Unix域套接字的路径。使用时,主机和端口将被忽略。(通过在App Engine flex上使用Loopback转移的知识。这使我在几天之内无法自拔)。它的值是您的Cloud SQL实例连接名称

因此,在您的情况下,它应如下所示:/ cloudsql / my-project-12345:us-central1:mydatabase

var connection = mysql.createConnection({
  socketPath     : '/cloudsql/my-project-12345:us-central1:mydatabase',
  user      : 'username',
  password  : 'password',
  database  : 'db_name'
});
Run Code Online (Sandbox Code Playgroud)

如果您在GCloud上使用Postgres,则过程与此类似,请在此处回答

  • 伙计,非常感谢你。6小时的痛苦。不是一个有趣的方式来度过一个星期天。 (2认同)
  • 为此,您还需要在 [IAM 表](https://cloud.google.com/iam/docs/granting-roles-) 中为“App Engine 默认服务帐户”添加“Cloud SQL 客户端”角色至服务帐户) (2认同)
  • 小事情,但它是 `'/cloudsql/my-project-12345:us-central1:instance-name` 不是 socketPath 的数据库 (2认同)