javascript - Google Cloud Functions - 通过 SSL 连接到 Cloud SQL

标签 javascript google-cloud-functions google-cloud-sql

GCF Cloud SQL documentation没有显示如何使用 SSL 通过套接字进行连接。这是我的配置,但是当我尝试连接时出现 ECONNREFUSED 错误。但是当我尝试连接到非 SSL 数据库时,它工作正常。有什么想法吗?

const mysql = require('mysql');

const mysqlConfig = {
  connectionLimit: 1,
  user: dbUser,
  password: dbPassword,
  database: dbName,
  ssl: {
    ca: await getFileContents(bucketName, ssl.ca_filename),
    key: await getFileContents(bucketName, ssl.key_filename),
    cert: await getFileContents(bucketName, ssl.cert_filename)
  }
};
if (process.env.NODE_ENV === 'production') {
  mysqlConfig.socketPath = `/cloudsql/${connectionName}`;
}

// Connection pools reuse connections between invocations,
// and handle dropped or expired connections automatically.
let mysqlPool;

exports.mysqlDemo = (req, res) => {
  // Initialize the pool lazily, in case SQL access isn't needed for this
  // GCF instance. Doing so minimizes the number of active SQL connections,
  // which helps keep your GCF instances under SQL connection limits.
  if (!mysqlPool) {
    mysqlPool = mysql.createPool(mysqlConfig);
  }

  mysqlPool.query('SELECT NOW() AS now', (err, results) => {
    if (err) {
      console.error(err);
      res.status(500).send(err);
    } else {
      res.send(JSON.stringify(results));
    }
  });
};

最佳答案

Cloud Functions 和 Cloud SQL 之间的连接与来自 App Engine 的连接的工作方式相同。因此,仅当您使用公共(public) IP 地址连接到 Cloud SQL 时才需要 SSL/TLS 连接。如果使用 Cloud SQL 代理或 Java 套接字库,默认情况下不需要设置 SSL 加密。

如何设置这个在下面的文档中设置: https://cloud.google.com/functions/docs/sql#connecting_to_cloud_sq

有关如何实现连接的解释,请查看: https://cloud.google.com/sql/docs/mysql/configure-ssl-instance

关于javascript - Google Cloud Functions - 通过 SSL 连接到 Cloud SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56663843/

相关文章:

javascript - 如何在云功能中从firestore获取用户的电子邮件?

python - 谷歌云功能 : Correct module needed for app_identity to get the APPLICATION_ID

javascript - 谷歌云功能不处理信号

用于谷歌云 SQL 的 GoLang SDK

mysql - 访问Google Cloud SQL中的mysql目录

javascript - 处理数据表行内的事件

javascript - setTimeout 函数比应有的速度要快

java - 通过 JDBC 连接到 CloudSQL for PostgreSQL 时,如何在类路径上指定 SSL 证书?

javascript - Highcharts Z-Index 实践不起作用

javascript - 如何在网站中重复使用重复的 HTML?