Hello, how are you? I'm having trouble connecting to the SQL database. I have a Node container with Sequelize managed by Kubernetes. I followed this documentation closely: https://cloud.google.com/sql/docs/mysql/connect-instance-kubernetes?hl=en#node.js
I created a service account with the necessary permissions to access SQL. When I check the cloud-sql-proxy container, I see these logs:
franciscoamuchastegui@MacBook-Pro-de-francisco sils-help-apis % kubectl logs sils-help-apis-stage-9759784bd-5fsgt -c cloud-sql-proxy {"severity":"INFO","timestamp":"2025-02-27T06:25:52.212Z","message":"Authorizing with Application Default Credentials"} {"severity":"INFO","timestamp":"2025-02-27T06:25:53.728Z","message":"[sils-help-desk-stage:us-central1:sils-help-desk-stage] Listening on 127.0.0.1:3306"} {"severity":"INFO","timestamp":"2025-02-27T06:25:53.728Z","message":"The proxy has started successfully and is ready for new connections!"} {"severity":"INFO","timestamp":"2025-02-27T06:26:04.673Z","message":"[sils-help-desk-stage:us-central1:sils-help-desk-stage] Accepted connection from 127.0.0.1:34222"} {"severity":"INFO","timestamp":"2025-02-27T06:26:05.273Z","message":"[sils-help-desk-stage:us-central1:sils-help-desk-stage] instance closed the connection"} {"severity":"INFO","timestamp":"2025-02-27T06:26:10.328Z","message":"[sils-help-desk-stage:us-central1:54450] Accepted connection from 127.0.0.1:54450"} {"severity":"INFO","timestamp":"2025-02-27T06:26:10.924Z","message":"[sils-help-desk-stage:us-central1:sils-help-desk-stage] instance closed the connection"} {"severity":"INFO","timestamp":"2025-02-27T06:26:29.544Z","message":"[sils-help-desk-stage:us-central1:sils-help-desk-stage] Accepted connection from 127.0.0.1:33474"}
And when I check the logs of my Node server image:
AccessDeniedError [SequelizeAccessDeniedError]: Access denied for user 'mysql-service-account@sils-help-desk-stage.iam.g'@'cloudsqlproxy~34.95.220.133' (using password: YES) at ConnectionManager.connect (/usr/src/app/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:94:17) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async ConnectionManager._connect (/usr/src/app/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:222:24) at async /usr/src/app/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:174:32 { parent: Error: Access denied for user 'mysql-service-account@sils-help-desk-stage.iam.g'@'cloudsqlproxy~34.95.220.133' (using password: YES) at Packet.asError (/usr/src/app/node_modules/mysql2/lib/packets/packet.js:740:17) at ClientHandshake.execute (/usr/src/app/node_modules/mysql2/lib/commands/command.js:29:26) at Connection.handlePacket (/usr/src/app/node_modules/mysql2/lib/base/connection.js:475:34) at PacketParser.onPacket (/usr/src/app/node_modules/mysql2/lib/base/connection.js:93:12) at PacketParser.executePayload (/usr/src/app/node_modules/mysql2/lib/packet_parser.js:139:14) at Socket.<anonymous> (/usr/src/app/node_modules/mysql2/lib/base/connection.js:100:25) at Socket.emit (node:events:517:28) at addChunk (node:internal/streams/readable:368:12) at readableAddChunk (node:internal/streams/readable:341:9) at Readable.push (node:internal/streams/readable:278:10) { code: 'ER_ACCESS_DENIED_ERROR', errno: 1045, sqlState: '28000', sqlMessage: "Access denied for user 'mysql-service-account@sils-help-desk-stage.iam.g'@'cloudsqlproxy~34.95.220.133' (using password: YES)", sql: undefined },
This is what I have in my Sequelize connection:
import { Sequelize } from 'sequelize'; export const sequelize = new Sequelize( `${process.env.DB_NAME}`, 'mysql-service-account@sils-help-desk-stage.iam.gserviceaccount.com', '', //I also tried with null, but it didn't work { host: 'localhost', dialect: 'mysql', logging: false, port: 3306 } ); sequelize.authenticate();
Deployment YAML:
Lastly, I'm including my deployment.yaml:
apiVersion: apps/v1 kind: Deployment metadata: name: sils-help-apis-stage spec: replicas: 2 selector: matchLabels: app: sils-help-apis-stage environment: stage template: metadata: labels: app: sils-help-apis-stage environment: stage spec: serviceAccountName: ksa-cloud-sql containers: - name: sils-help-apis-stage image: gcr.io/sils-help-desk-stage/sils-help-apis:v21 ports: - containerPort: 80 resources: requests: cpu: "100m" memory: "256Mi" limits: cpu: "500m" memory: "512Mi" initContainers: - name: cloud-sql-proxy image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.14.1 restartPolicy: Always args: - "--port=3306" - "--auto-iam-authn" # Enables automatic IAM authentication - "--structured-logs" # Enables structured logs - "sils-help-desk-stage:us-central1:sils-help-desk-stage" securityContext: runAsNonRoot: true resources: requests: cpu: 100m # 100 millicores memory: 128Mi # 128 megabytes limits: cpu: 200m # 200 millicores memory: 256Mi # 256 megabytes
Same issue
i try different variants but have the same issue, help!
Hi, @franamu.
Since you're using a service account to authenticate with CloudSQL but it's missing from your YAML file in the cloud-sql-proxy init container. I recommend reading this document for further guidance - Connect to Cloud SQL from Google Kubernetes Engine.
Regards,
Mokit
You're very close to getting this working! The issue lies in how you're configuring Sequelize to authenticate with Cloud SQL using IAM authentication. Let's break down the problem and how to fix it:
Understanding the Problem
IAM Authentication vs. MySQL User Authentication:
Cloud SQL Proxy's Role:
Solution
Create a MySQL User:
CREATE USER 'your_mysql_user'@'%' IDENTIFIED WITH mysql_native_password BY 'your_mysql_password'; GRANT ALL PRIVILEGES ON your_database.* TO 'your_mysql_user'@'%'; FLUSH PRIVILEGES;
Update Sequelize Configuration:
import { Sequelize } from 'sequelize'; export const sequelize = new Sequelize( `${process.env.DB_NAME}`, 'your_mysql_user', // Use the MySQL username 'your_mysql_password', // Use the MySQL password { host: 'localhost', // Connect to the local port exposed by the proxy dialect: 'mysql', logging: false, port: 3306, } ); sequelize.authenticate();
Ensure Environment Variables:
Key Points
By following these steps, you should be able to successfully connect to your Cloud SQL database from your Node.js application running in Kubernetes.