Skip to content

Cloud SQL socket connection fails — mysql-config.ts does not support socketPath parameter #1805

@joelstucki-taulia

Description

@joelstucki-taulia

Summary

When deploying den-api to Google Cloud Run with a Cloud SQL MySQL instance connected via Unix socket, the app fails to connect to the database. The DATABASE_URL must use ?socketPath=/cloudsql/... to specify the socket path, but parseMySqlConnectionConfig in ee/packages/den-db/src/mysql-config.ts does not parse the socketPath (or socket) query parameter and does not pass it to the mysql2 connection config.

The result is that mysql2 falls back to TCP on 127.0.0.1:3306, which is not available in Cloud Run, causing ECONNREFUSED on every database call.

Steps to Reproduce

  1. Deploy den-api to Google Cloud Run with a Cloud SQL instance attached
  2. Set DATABASE_URL=mysql://root:password@localhost/dbname?socketPath=/cloudsql/project:region:instance
  3. Any request requiring a DB call returns a 500 error
  4. Logs show: Error: connect ECONNREFUSED 127.0.0.1:3306

Fix

In ee/packages/den-db/src/mysql-config.ts, add socketPath to the ParsedMySqlConfig type and parse it from the URL:

type ParsedMySqlConfig = {
  host: string
  port: number
  user: string
  password: string
  database: string
  socketPath?: string  // add this
  ssl?: { rejectUnauthorized: boolean }
}

// In parseMySqlConnectionConfig():
return {
  host: parsed.hostname,
  port: Number(parsed.port || "3306"),
  user: decodeURIComponent(parsed.username),
  password: decodeURIComponent(parsed.password),
  database,
  socketPath: parsed.searchParams.get("socketPath") || parsed.searchParams.get("socket") || undefined,
  ssl: readSslSettings(parsed),
}

Note: drizzle-kit db:push has the same issue (it uses its own connection logic), so migrations must be run separately when using Cloud SQL socket connections.

Environment

  • Google Cloud Run + Cloud SQL (MySQL 8.0) via Unix socket
  • mysql2 driver via drizzle-orm

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions