我在Google Cloud上有一个托管节点应用程序和mysql。我尝试了所有可能的方法从节点连接mysql,但每次抛出此错误。
let pool;
const createPool = async () => {
pool = await mysql.createPool({
// host: "35.200.129.217",
socketPath:"/cloudsql/idyllic-anvil-256103:asia-south1:database",
user: "abc@gmail.com",
password: "pass",
database: "db"
});
};
createPool();
app.get("/getnews", (request, response) => {
createPool.query('SELECT * FROM db.mydb', function (err, result) {
if (err) throw new Error(err)
response.send(result);
})
});
错误:
2019-10-19 16:33:40 default[20191019t215943] Error: Error: connect ETIMEDOUT at Query._callback (/srv/index.js:42:20) at Query.Sequence.end (/srv/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24) at /srv/node_modules/mysql/lib/Pool.js:205:13 at Handshake.onConnect (/srv/node_modules/mysql/lib/Pool.js:58:9) at Handshake.<anonymous> (/srv/node_modules/mysql/lib/Connection.js:525:10) at Handshake._callback (/srv/node_modules/mysql/lib/Connection.js:491:16) at Handshake.Sequence.end (/srv/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24) at Protocol.handleNetworkError (/srv/node_modules/mysql/lib/protocol/Protocol.js:369:14) at PoolConnection.Connection._handleNetworkError (/srv/node_modules/mysql/lib/Connection.js:421:18) at PoolConnection.Connection._handleConnectTimeout (/srv/node_modules/mysql/lib/Connection.js:417:8)
2019-10-19 16:34:16 default[20191019t220300] "GET / HTTP/1.1" 304
2019-10-19 16:34:18 default[20191019t220300] Server listening on port 8081...
2019-10-19 16:34:18 default[20191019t220300] Database connection was refused.
2019-10-19 16:34:22 default[20191019t220300] "GET /getnews HTTP/1.1" 502
2019-10-19 16:34:22 default[20191019t220300] /srv/index.js:42
2019-10-19 16:34:22 default[20191019t220300] if (err) throw new Error(err)
2019-10-19 16:34:22 default[20191019t220300] ^
2019-10-19 16:34:22 default[20191019t220300]
2019-10-19 16:34:23 default[20191019t220300] Error: Error: connect ECONNREFUSED /cloudsql/idyllic-anvil-256103:asia-south1:database at Query._callback (/srv/index.js:42:20) at Query.Sequence.end (/srv/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24) at /srv/node_modules/mysql/lib/Pool.js:205:13 at Handshake.onConnect (/srv/node_modules/mysql/lib/Pool.js:58:9) at Handshake.<anonymous> (/srv/node_modules/mysql/lib/Connection.js:525:10) at Handshake._callback (/srv/node_modules/mysql/lib/Connection.js:491:16) at Handshake.Sequence.end (/srv/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24) at Protocol.handleNetworkError `enter code here`(/srv/node_modules/mysql/lib/protocol/Protocol.js:369:14) at PoolConnection.Connection._handleNetworkError (/srv/node_modules/mysql/lib/Connection.js:421:18) at Socket.emit (events.js:198:13)
答案 0 :(得分:1)
确保遵循Connecting to Cloud SQL from App Engine上的说明。特别是以下几件事很容易错过:
Cloud SQL Client
IAM角色或页面上列出的权限app.yaml
中正确拼写最后,请确保查看Managing Database Connections页。特别是,在上面的示例中,您正在使用名称初始化函数,然后对该函数调用查询。相反,您应该使用该池进行查询:
let pool; // <---- This is the pool that `createPool` will set
const createPool = async () => {
pool = await mysql.createPool({
socketPath:"/cloudsql/idyllic-anvil-256103:asia-south1:database",
user: "abc@gmail.com", //<--- just FYI, this should be the database user, not the GCP account connecting
password: "pass",
database: "db"
});
};
createPool(); // <----- This is where `pool` is actually created
// Now we can use the pool to query like this:
try {
const stmt = 'INSERT INTO votes (time_cast, candidate) VALUES (?, ?)';
// Pool.query automatically checks out, uses, and releases a connection
// back into the pool, ensuring it is always returned successfully.
await pool.query(stmt, [timestamp, team]);
} catch (err) {
// If something goes wrong, handle the error in this section. This might
// involve retrying or adjusting parameters depending on the situation.
// ...
}