【问题标题】:How to connect mysql database with nodejs from google cloud如何从谷歌云连接mysql数据库和nodejs
【发布时间】:2019-03-05 21:36:26
【问题描述】:

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

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

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

错误 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

【问题讨论】:

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


【解决方案1】:

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

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

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

【讨论】:

  • 注意 - 代码错误。 socketPath 应该是 socketPath
  • 是的,正确。修改
猜你喜欢
  • 2020-05-18
  • 2019-12-08
  • 2020-03-18
  • 1970-01-01
  • 2016-02-27
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多