具有firebase云功能的非Google MySQL数据库连接



当我尝试连接到MySQL时,服务器会向我发送一个错误Error: connect ETIMEDOUT

这是的样本代码

const mysql = require('mysql');
const mysqlConfig = {
connectionLimit: 1,
host: "remote_host_ip",
user: "server_user",
password: "server_pass",
database: "server_db",
port: 3306
};
mysql.createConnection(mysqlConfig).connect(function (err) {
if (!err) {
console.log("Database is connected");
} else {
console.log("Database is not connected " + err);
}
});

我正在执行燃烧基地大火(现收现付(计划。

我知道问题标题已经说明了这一点,但您没有使用GCP Cloud SQL,对吧?

在这种情况下,云功能很可能无法访问MySQL服务器。你确定网络连接正常吗?

此外,即使在云函数中,使用连接池也是一个好主意。考虑使用它,它会是这样的:

var config = {
user: 'root',
password: 'akdaskdasdaE',
database: 'database1'
}
config.connectionLimit = 10
config.multipleStatements = true
// needed in GCP Cloud SQL, but it seems it's not your case
// config.socketPath = `/cloudsql/__INSTANCE_CONNECTION_NAME__`
var connectionPool = mysql.createPool(config)
connectionPool.on('connection', () => {
console.log(`[connectionPool] new connection opened`)
})
// then you use connectionPool.getConnection(..)

最新更新