4
votes

I have a NodeJs application and MySQL database. I have deployed nodejs application in google cloud and created MySQL database in google cloud and connected to nodejs application i am able deploye the application successfully but application not able to connect to cloud mysql dayabase.

But when i am trying to connect cloud mysql from my local mysql workbench, it's successfully connecting to database. and i am able to connect cloud mysql database from local nodejs application

but i am not able to connect from deployed nodejs application to cloud mysql db

error Database is not connectedError: connect ETIMEDOUT

Db.js

var mysql = require('mysql');
var PropertiesReader = require('properties-reader');
var properties = PropertiesReader('./db.properties');

var connection = mysql.createConnection({
    connectionLimit : properties.get('pool'),
    host: properties.get('hostname'),
    user: properties.get('username'),
    password: properties.get('password'),
    database: properties.get('dbname')
});

connection.connect(function (err) {
    if (!err) {
        console.log("Database is connected");
    } else {
        console.log("Database is not connected" + err);
    }
}
);

module.exports = connection;

app.yaml

runtime: nodejs
env: flex
1
So there is no error ? - Sagnik Pradhan
yes it has error - Sangram Badi
Have you added the environment variables and beta_settings section in your app.yaml file as detailed here: cloud.google.com/appengine/docs/flexible/nodejs/…? - LundinCast

1 Answers

1
votes

In your connection configuration for mysql,host does not work on App Engine. You have to use socketPath . socketPath is the path to a unix domain socket to connect to. Socket path must be something like this

var connection = mysql.createConnection({ socketPath : '/cloudsql/my-project-12345:us-central1:mydatabase', user : 'username', password : 'password', database : 'db_name' });

It's a similar process if you're using Postgres on GCloud which is answered here