Search code examples
mysqlgoogle-cloud-platformgoogle-app-enginegoogle-cloud-sql

Error connecting GCP Cloud Sql with GCP App Engine


I'm getting error in connecting GCP cloud sql with GCP App Engine. Its working fine in locally using google auth proxy but when deploy app using gcloud app deploy and after successful deploy getting error on server

Error: connect ETIMEDOUT at Connection._handleTimeoutError (/layers/google.nodejs.yarn/yarn_modules/node_modules/mysql2/lib/connection.js:205:17) at listOnTimeout (node:internal/timers:569:17) at process.processTimers (node:internal/timers:512:7) 
 {
 errorno: 'ETIMEDOUT',
 code: 'ETIMEDOUT',
 syscall: 'connect',
 fatal: true
 },

getting above error in the below code

const Sequilize = require("sequelize").Sequelize;
const dotenv = require("dotenv");
dotenv.config();


const sequilize = new Sequilize(
  process.env.DB_NAME,
  process.env.DB_USER,
  process.env.DB_PASSWORD,
  {
    host: process.env.DB_HOST, //db host public ip
    dialect: "mysql",
    socketPath: process.env.INSTANCE_CONNECTION, //`/cloudsql/INSTANCE_CONNECTION_NAME`,
  }
);

module.exports = { sequilize };

app.yml code

runtime: nodejs
env: flex

runtime_config:
  operating_system: "ubuntu22"
  runtime_version: "18"

beta_settings:
  cloud_sql_instances: /cloudsql/INSTANCE_CONNECTION_NAME

Solution

  • Remove host and instead use socketPath.

    App Engine will run the Cloud SQL Proxy for you and make a Unix socket connected to your instance available at /cloudsql/<INSTANCE_CONNECTION_NAME>.

    For more info, see https://cloud.google.com/sql/docs/mysql/connect-app-engine-standard#node.js.

    const sequilize = new Sequilize(
      process.env.DB_NAME,
      process.env.DB_USER,
      process.env.DB_PASSWORD,
      {
        dialect: "mysql",
        socketPath: process.env.INSTANCE_CONNECTION, //`/cloudsql/INSTANCE_CONNECTION_NAME`,
      }
    );