简体   繁体   English

从 Heroku 上的 Node.js 连接到 CloudSQL

[英]Connecting to CloudSQL from Node.js on Heroku

I have a Node.js app (running on AppEngine) connecting to a GCP CloudSQL (MySQL) instance.我有一个 Node.js 应用程序(在 AppEngine 上运行)连接到 GCP CloudSQL (MySQL) 实例。 Now I want to connect to the same database from Node.js (Knex) running on Heroku.现在我想从运行在 Heroku 上的 Node.js (Knex) 连接到同一个数据库。

From AppEngine, Node.js connects via user/password and socketPath.在 AppEngine 中,Node.js 通过用户/密码和 socketPath 进行连接。 I'm also connecting to the same MySQL DB from MySQL Workbench via host IP (over SSL).我还通过主机 IP(通过 SSL)从 MySQL Workbench 连接到同一个 MySQL 数据库。

I'm trying to use the same host, port, user and pass as Workbench from Heroku and it's not working.我正在尝试使用与 Heroku 的 Workbench 相同的主机、端口、用户和传递,但它不起作用。 To try and make it easy, I've temporarily allowed all networks to connect (0.0.0.0/0) and I've allowed non-SSL connections.为了简化操作,我暂时允许所有网络连接 (0.0.0.0/0) 并且允许非 SSL 连接。

Here's the error: ER_ACCESS_DENIED_ERROR: Access denied for user 'usernamehere'@'xx.xxx.xxx.xx' (using password: YES)"这是错误: ER_ACCESS_DENIED_ERROR: Access denied for user 'usernamehere'@'xx.xxx.xxx.xx' (using password: YES)"

The environment variables are stored in the Heroku app and they must be working because the username is correct.环境变量存储在 Heroku 应用程序中,它们必须工作,因为用户名是正确的。

It's not very helpful, but here's the code:这不是很有帮助,但这是代码:

import Knex = require('knex');
const envConfig = require('../config/environments').get(process.env.NODE_ENV);
module.exports = knex;

The only way I found to resolve this issue was to connect to CloudSQL over SSL.我发现解决此问题的唯一方法是通过 SSL 连接到 CloudSQL。

const mysql = require("mysql");
const fs = require('fs');
const knex = require('knex')({
    client: 'mysql',
    version: '5.7',
    connection: {
      host : 'xx.xx.xx.xx',
      ssl: {
        ca: fs.readFileSync('ca.pem'),
        key: fs.readFileSync('client-key.pem'),
        cert: fs.readFileSync('client-cert.pem'),
      },
      user : 'root',
      password : 'xxxxxxxxx',
      database : 'mydbname',
    },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM