mysql - 如何从谷歌云连接mysql数据库和nodejs

标签 mysql node.js google-app-engine google-cloud-platform

我有一个 NodeJs 应用程序和 MySQL 数据库。 我在谷歌云中部署了 nodejs 应用程序并在 google cloud 中创建了 MySQL 数据库并连接到 nodejs 应用程序 我能够成功部署应用程序,但应用程序无法连接到云 mysql dayabase。

但是当我尝试从本地 mysql 工作台连接云 mysql 时,它成功连接到数据库。并且我能够从本地 nodejs 应用程序连接云 mysql 数据库

但我无法从已部署的 nodejs 应用程序连接到云 mysql 数据库

错误 数据库未连接错误:连接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

最佳答案

在您的 mysql 连接配置中,主机在 App Engine 上不起作用。你必须使用 socketPath 。 socketPath 是要连接的 unix 域套接字的路径。套接字路径必须是这样的

var connection = mysql.createConnection({ socketPath : '/cloudsql/my-project-12345:us-central1:mydatabase', 用户:'用户名', 密码:'密码', 数据库:'db_name' });

如果您在 GCloud 上使用 Postgres,这是一个类似的过程,答案是 here

关于mysql - 如何从谷歌云连接mysql数据库和nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52579878/

相关文章:

python - 用于 Google Cloud Storage 的类似 Boto 的库

node.js - 如何传递 Node 生成中的所有当前环境变量?

regex - 如何使用 mongoose find 获取包含部分字符串的所有值?

MySQL 触发器不完全工作

c# - 我需要一些关于 mysql 的建议

javascript - 使用公钥创建比特币钱包

python-3.x - 错误: Bad Gateway 502 when opening Google App Engine Python Domain

java - 您构建应用程序时使用的 Google App Engine SDK 版本是否会影响生产应用程序引擎?

c# - select语句包含保留字c#

MySQL SELECT * 究竟是如何工作的 MySQL/Query Efficiency