You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署在App Engine Node.js弹性环境的Bookshelf教程应用CloudSQL连接问题

Troubleshooting Cloud SQL Connection Issues in App Engine Node.js Flexible Environment

Let’s walk through targeted fixes for your Cloud SQL connection problem in the Node.js flexible environment:

1. Fix the Incomplete Socket Path Configuration

Looking at your model-cloudsql.js code, the socket path is truncated:

options.socketPath = `/cloudsql/...`

Replace the ... with the full value from your INSTANCE_CONNECTION_NAME in config.json. Update the line to:

options.socketPath = `/cloudsql/${config.get('INSTANCE_CONNECTION_NAME')}`;

This ensures your app connects to the correct Cloud SQL instance via the Unix socket provided by App Engine’s flexible environment.

2. Verify IAM Permissions for the App Engine Service Account

Your App Engine app uses a default service account ([YOUR_PROJECT_ID]@appspot.gserviceaccount.com) to interact with Cloud SQL. Confirm this account has the Cloud SQL Client role:

  • Navigate to the IAM section of your Google Cloud Console.
  • Locate the App Engine service account in the list.
  • Check if it has the roles/cloudsql.client role assigned. If not, add it to grant the necessary connection permissions.

3. Validate Environment Variables and Production Mode

Ensure NODE_ENV is set to production when deployed—this triggers your code to use the Cloud SQL socket path. Define this in your app.yaml file:

env_variables:
  NODE_ENV: 'production'

Also double-check that all values in config.json (like INSTANCE_CONNECTION_NAME, MYSQL_USER, MYSQL_PASSWORD) exactly match the configuration of your Cloud SQL instance.

4. Confirm Cloud SQL Instance Compatibility

  • Region Alignment: Your Cloud SQL instance is in us-central1—make sure your App Engine flexible environment is deployed to the same region (use gcloud app deploy --region=us-central1 if needed to specify during deployment).
  • Instance Status: Ensure your Cloud SQL instance is running (not paused or stopped) in the Cloud Console.
  • Database Access: Verify that the database name you’re referencing in model-cloudsql.js (XXXXX) exists in your Cloud SQL instance, and the MYSQL_USER has permission to access it.

5. Check MySQL Client Dependencies

Ensure you’re using a compatible MySQL client library (like mysql2 or mysql) and it’s listed in your package.json. For example, install mysql2 with:

npm install mysql2

Then confirm your connection code correctly uses the options object:

const mysql = require('mysql2/promise');
const connection = await mysql.createConnection(options);

After applying these changes, redeploy your App Engine app and test the connection again.

内容的提问来源于stack exchange,提问作者Dinesh Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:35:43