0
votes

Here is my node js code

var mysql = require('mysql');

exports.helloGET = (req, res) => {

console.log(req.body.message);

var con = mysql.createConnection({
      //socketPath: '/cloudsql/' + 'project-id:region-name:instance',
      host: 'xxx.xxx.xxx.xxx',
      user: 'user',
      password: 'root',
      database: 'test'
    });

    con.connect(function(err) {
        console.log("Connection Error - " + err)
        if (err) throw err;
        console.log("Connected to Cloud SQL");
    });

    con.query(`SELECT now()`, null, function (err, result) {
        //made reply here
        console.log("Query Error - " + err)
        if (err) throw err;
        console.log("Result: " + result);
    });

con.end();
res.status(200).send('Acknowledgement : ' + req.body.message);
};

Error: connect ETIMEDOUT xxx.xxx.xxx.xxx:3306

I tried using socketPath instead of host -

var con = mysql.createConnection({
      socketPath: '/cloudsql/' + 'project-id:region-name:instance',
      //host: 'xxx.xxx.xxx.xxx',
      user: 'user',
      password: 'root',
      database: 'test'
    });

For this I am getting - Error: connect ECONNREFUSED

1

1 Answers

1
votes

That feature is currently not supported. However there is a feature request open that you can follow in the issue tracker #36388165.

Like is mentioned in this comment from Jason Polites you can fill this early access form for access Cloud SQL from a Cloud Function.