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
- Deploy
den-api to Google Cloud Run with a Cloud SQL instance attached
- Set
DATABASE_URL=mysql://root:password@localhost/dbname?socketPath=/cloudsql/project:region:instance
- Any request requiring a DB call returns a 500 error
- 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
Summary
When deploying
den-apito Google Cloud Run with a Cloud SQL MySQL instance connected via Unix socket, the app fails to connect to the database. TheDATABASE_URLmust use?socketPath=/cloudsql/...to specify the socket path, butparseMySqlConnectionConfiginee/packages/den-db/src/mysql-config.tsdoes not parse thesocketPath(orsocket) 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, causingECONNREFUSEDon every database call.Steps to Reproduce
den-apito Google Cloud Run with a Cloud SQL instance attachedDATABASE_URL=mysql://root:password@localhost/dbname?socketPath=/cloudsql/project:region:instanceError: connect ECONNREFUSED 127.0.0.1:3306Fix
In
ee/packages/den-db/src/mysql-config.ts, addsocketPathto theParsedMySqlConfigtype and parse it from the URL:Note:
drizzle-kit db:pushhas the same issue (it uses its own connection logic), so migrations must be run separately when using Cloud SQL socket connections.Environment
mysql2driver via drizzle-orm