GCF Cloud SQL文档没有展示如何使用SSL通过套接字进行连接。这是我的配置,但是当我尝试连接时,我收到一个 ECONNREJECT 错误。但是当我尝试连接到非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));
}
});
};
Functions 和 Cloud SQL 之间的连接的工作方式与来自 App Engine 的连接相同。因此,仅当您使用公共 IP 地址连接到云 SQL 时,才需要 SSL/TLS 连接。如果使用云 SQL 代理或 Java 套接字库,则默认情况下不需要设置 SSL 加密。
以下文档中设置了如何设置此设置:https://cloud.google.com/functions/docs/sql#connecting_to_cloud_sq
有关如何实现连接的说明,请查看:https://cloud.google.com/sql/docs/mysql/configure-ssl-instance