我按照所有使用第二代MySQL数据库的Google Cloud Functions文档创建了一些Web服务。一切运行顺利,但我收到随机的"Econnrefused"错误:
{ Error: connect ECONNREFUSED /cloudsql/calculator-226400:us-central1:vanessasqlus at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1191:14) -------------------- at Protocol._enqueue (/srv/node_modules/mysql/lib/protocol/Protocol.js:144:48) at Protocol.handshake (/srv/node_modules/mysql/lib/protocol/Protocol.js:51:23) at PoolConnection.connect (/srv/node_modules/mysql/lib/Connection.js:118:18) at Pool.getConnection (/srv/node_modules/mysql/lib/Pool.js:48:16) at Pool.query (/srv/node_modules/mysql/lib/Pool.js:202:8) at Object.get_user (/srv/request_post/user.js:15:15) at exports.landing_ws (/srv/index.js:84:18) at /worker/worker.js:783:7 at /worker/worker.js:766:11 at _combinedTickCallback (internal/process/next_tick.js:132:7) errno: 'ECONNREFUSED', code: 'ECONNREFUSED', syscall: 'connect', address: '/cloudsql/calculator-226400:us-central1:vanessasqlus', fatal: true }
我迁移了美国的所有内容,但仍然收到随机连接错误。这是我的连接字符串:
const connectionName =
process.env.INSTANCE_CONNECTION_NAME || 'calculator-226400:us-central1:vanessasqlus';
const dbUser = process.env.SQL_USER || '*';
const dbPassword = process.env.SQL_PASSWORD || '*';
const dbName = process.env.SQL_NAME || '*';
let mysqlConfig = {
connectionLimit: 1,
user: dbUser,
password: dbPassword,
database: dbName,
};
...
if (!mysqlPool) {
mysqlPool = mysql.createPool(mysqlConfig);
}
if (process.env.NODE_ENV === 'production') {
mysqlConfig.socketPath = `/cloudsql/${connectionName}`;
}
Google Cloud
Functions 在 /cloudsql/<INSTANCE_CONNECTION_NAME>
上提供了一个 Unix 套接字,可用于连接到 Cloud SQL 实例。您需要在配置中指定此要求:
mysqlConfig.socketPath = `/cloudsql/${connectionName}`;
如果要跨区域或跨项目连接,则需要遵循一些其他步骤,但可以在此处找到这些步骤。