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));
}
});
};
答案 0 :(得分:1)
Cloud Functions和Cloud SQL之间的连接的工作方式与来自App Engine的连接相同。因此,仅当您使用公共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