Search code examples
mysqlnode.jsgoogle-app-enginegoogle-cloud-platformgoogle-cloud-sql

Connection timed out: Nodejs Google App Engine to Cloud MySql


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

Solution

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