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/