在我的网络服务中变得随机econnrefused

时间:2019-03-25 13:55:30

标签: google-cloud-platform google-cloud-functions google-cloud-sql

我在使用所有第二代MySQL DB的所有google cloud功能文档之后创建了一些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}`;
}

1 个答案:

答案 0 :(得分:0)

Google Cloud Functions在/cloudsql/<INSTANCE_CONNECTION_NAME>提供了一个Unix套接字,可用于连接到Cloud SQL实例。您需要在配置中指定此要求:

mysqlConfig.socketPath = `/cloudsql/${connectionName}`;

如果您要连接跨区域或跨项目,则需要执行一些其他步骤,但是可以找到here