I have a hosted node app and mysql on google cloud. I tried all possible ways to connect mysql from node but every time it throws this error.
let pool;
const createPool = async () => {
pool = await mysql.createPool({
// host: "35.200.129.217",
socketPath:"/cloudsql/idyllic-anvil-256103:asia-south1:database",
user: "[email protected]",
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);
})
});
Error:
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)
Make sure you follow the instructions at Connecting to Cloud SQL from App Engine. In particular, the following things are easy to miss:
Cloud SQL Client
IAM role, or the permissions listed on the pageapp.yaml
Finally, make sure to take a look at Managing Database Connections page. In particular, in the example you have above you are initializing a function with a name, and then calling query on that function. Instead, you should use the pool to do your queries: