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

时间:2017-10-28 21:33:57

标签: mysql node.js google-app-engine google-cloud-sql gcloud

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

'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]

但是,当我在谷歌应用引擎中运行相同的代码时(对于端口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 }

为什么这不适用于Google App Engine应用程序?

1 个答案:

答案 0 :(得分:4)

在mysql的连接配置中,host在App Engine上不起作用。您必须使用socketPath。 socketPath是连接到的unix域套接字的路径。当使用主机和端口被忽略时。 (在App Engine flex上使用Loopback转移了知识。它让我头疼了好几天)。它的价值在于您的Cloud SQL实例连接名称

所以在你的情况下,它应该是这样的:/ cloudsql / my-project-12345:us-central1:mydatabase

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

如果您在GCloud上使用Postgres,这是一个类似的过程,已经回答here