How to connect mysql database with nodejs from google cloud

杀马特。学长 韩版系。学妹 提交于 2020-01-02 09:23:08

问题


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:


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({ sockePath : '/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



来源:https://stackoverflow.com/questions/52579878/how-to-connect-mysql-database-with-nodejs-from-google-cloud

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