Connection timed out: Nodejs Google App Engine to Cloud MySql

我是研究僧i 提交于 2019-12-23 03:48:25

问题


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.

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

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

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");

回答1:


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”
    });


来源:https://stackoverflow.com/questions/49154461/connection-timed-out-nodejs-google-app-engine-to-cloud-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!