Google Cloud Functions-通过SSL连接到Cloud SQL [英] Google Cloud Functions - Connect to Cloud SQL via SSL

查看:91
本文介绍了Google Cloud Functions-通过SSL连接到Cloud SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCF Cloud SQL 文档没有显示如何使用SSL.这是我的配置,但是当我尝试连接时,出现ECONNREFUSED错误.但是,当我尝试连接到非SSL数据库时,它可以正常工作.有什么想法吗?

The GCF Cloud SQL documentation does not show how to connect via the socket using SSL. Here is my config, however when I try to connect I get an ECONNREFUSED error. But when I try to connect to a non-SSL database it works fine. Any ideas?

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的连接相同.因此,仅当您使用公共IP地址连接到Cloud SQL时才需要SSL/TLS连接.如果使用Cloud SQL代理服务器或Java套接字库,则默认情况下不需要进行SSL加密设置.

Connections between Cloud Functions and Cloud SQL work in the same way as connections from App Engine . As such SSL/TLS connections are needed only when you are connecting to Cloud SQL using public IP addresses. If Cloud SQL Proxy or the Java Socket Library, is used, setting up SSL is not required encryption happen by default.

如何设置此设置,请参见以下文档: https://cloud.google.com/functions/docs/sql#connecting_to_cloud_sq

How to set this up is set in the following document: https://cloud.google.com/functions/docs/sql#connecting_to_cloud_sq

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

For an explanation as to how the connections are implemented please have a look at: https://cloud.google.com/sql/docs/mysql/configure-ssl-instance

这篇关于Google Cloud Functions-通过SSL连接到Cloud SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆