我有一个NodeJs应用程序和MySQL数据库。
我已经在Google云中部署了nodejs应用程序,并在google cloud
中创建了MySQL数据库,并连接到nodejs应用程序
我能够成功部署应用程序,但应用程序无法连接到云mysql dayabase。
但是当我尝试从本地mysql工作台连接云mysql时,它已成功连接到数据库。而且我能够从本地nodejs应用程序连接云mysql数据库
但是我无法从已部署的nodejs应用程序连接到云mysql数据库
错误 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
答案 0 :(得分:1)
在您的mysql连接配置中,主机在App Engine上不起作用。您必须使用socketPath。 socketPath是要连接到的Unix域套接字的路径。套接字路径必须是这样的
var connection = mysql.createConnection({
sockePath : '/cloudsql/my-project-12345:us-central1:mydatabase',
user : 'username',
password : 'password',
database : 'db_name'
});
如果您在GCloud上使用Postgres,则该过程与此类似here