连接超时:Nodejs Google App Engine到Cloud MySql [英] Connection timed out: Nodejs Google App Engine to Cloud MySql

查看:63
本文介绍了连接超时:Nodejs Google App Engine到Cloud MySql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码非常基础.使用mysql的简单nodejs应用程序. 当代码尝试连接到Google App Engine上的Google Cloud MySql服务器(第二代)时,会收到Error: connect ETIMEDOUT.

The code is very basic. Simple nodejs app using mysql. Error: connect ETIMEDOUT is received when code tries connecting to Google Cloud MySql server (second generation) on google app engine.

但是,由于我的计算机的IP地址已列入白名单,因此该应用程序能够从我的本地计算机连接到MySql服务器.

However app is able to connect to MySql server from my local machine since IP address of my machine is whitelisted.

App Engine应用程序和Google Cloud Server在Google Cloud Console中属于同一项目.因此,不需要额外的权限(?)

App engine application and google cloud server belong to same project in google cloud console. So no extra permissions are required (?)

所以我无法理解如何允许应用引擎访问MySql服务器.

So I'm failing to understand how to allow app engine to access MySql server.

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参数有帮助.

Using socketPath param while connecting to mysql helped.

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

这篇关于连接超时:Nodejs Google App Engine到Cloud MySql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆