连接超时:Nodejs Google App Engine 到云 MySql

Connection timed out: Nodejs Google App Engine to Cloud MySql

代码非常基础。使用 mysql 的简单 nodejs 应用程序。 当代码尝试连接到 google 应用程序引擎上的 Google 云 MySql 服务器(第二代)时收到 Error: connect ETIMEDOUT

但是应用程序能够从我的本地机器连接到 MySql 服务器,因为我机器的 IP 地址已列入白名单。

App Engine 应用程序和 google 云服务器属于 google 云控制台中的同一个项目。所以不需要额外的权限 (?)

所以我不明白如何允许应用引擎访问 MySql 服务器。

var express    = require('express'),
    mysql      = require('mysql'),
    bodyParser = require('body-parser')
// Application initialization

var connection = mysql.createConnection({
        host     : 'ip_address',
        user     : 'root',
        password : 'password',
        database : 'database_name'
    });

var app = express();

app.use(bodyParser.urlencoded({
  extended: true
}));

app.use(bodyParser.json());

app.get('/_ah/health', (req, res) => {
    res.status(200)
    res.send({hello:'world'})
})


app.get('/', function(req, res) {
    connection.query('select * from blogs', 
        function (err, result) {
            if (err) throw err;
            res.send({result: result});
        }
    );
});

// Begin listening

app.listen(8080);
console.log("Express server listening");

连接到 mysql 时使用 socketPath 参数有帮助。

var connection = mysql.createConnection({
        host     : 'ip_address',
        user     : 'root',
        password : 'password',
        database : 'database_name'
        socketPath: “/cloudsql/projectName:zone:instance-name”
    });