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

Laravel应用部署在Google App Engine无法连接Cloud SQL数据库求助

Fixing Laravel + Google App Engine Flex + Cloud SQL Connection Error (SQLSTATE[HY000] [2002])

That SQLSTATE[HY000] [2002] No such file or directory error is super common when setting up Laravel on GAE Flex with Cloud SQL—it almost always means your app is trying to connect via TCP to a local MySQL server (which doesn't exist on GAE) instead of using the Cloud SQL Unix socket. Let's walk through the fixes step by step:

1. Fix Your DB_HOST Environment Variable

This is the most critical fix. When using a Unix socket for Cloud SQL, do NOT set DB_HOST to localhost. Here's why:

  • If DB_HOST is set to localhost, PHP's MySQL driver will try to use its default local socket path (not the Cloud SQL socket you specified), which doesn't exist on GAE Flex.
  • Instead, set DB_HOST to an empty string or remove it entirely (let Laravel use the default null value from config/database.php):

Update your app.yaml env variables:

env_variables:
  # ... keep your other variables
  DB_HOST: ""  # Replace "localhost" with this
  DB_SOCKET: "/cloudsql/projectname:region:instancename"

2. Verify Your Cloud Socket Configuration

Double-check these details to make sure your socket path is correct:

  • The cloud_sql_instances value in beta_settings must exactly match the instance name in DB_SOCKET (no typos in project ID, region, or instance name).
  • The socket path format is always /cloudsql/[PROJECT_ID]:[REGION]:[INSTANCE_NAME]—no extra spaces, no missing colons.

3. Ensure Laravel Uses the Socket

Your config/database.php looks correct, but confirm these points:

  • The unix_socket line is properly pulling from env('DB_SOCKET', '') (yours is).
  • You haven't hardcoded a socket path anywhere else in the config.

4. Additional Checks (Just to Be Safe)

  • Cloud SQL IAM Permissions: Make sure your GAE service account (usually [YOUR_PROJECT_ID]@appspot.gserviceaccount.com) has the Cloud SQL Client role assigned in the Cloud SQL IAM settings. Without this, your app can't access the socket.
  • Clear Laravel Config Cache: If you ran php artisan config:cache locally before deploying, the old DB_HOST value might be cached. Re-run php artisan config:cache after updating your env vars, or deploy with fresh code (GAE Flex will run composer install and can regenerate the cache if you include the necessary commands in app.yaml).
  • Cloud SQL Instance Status: Confirm your Cloud SQL instance is in the RUNNING state (not paused or under maintenance).

Bonus: Simplify with DATABASE_URL (Laravel 8+)

If you're using Laravel 8 or newer, you can replace all your DB env vars with a single DATABASE_URL which explicitly defines the socket connection:

env_variables:
  # ... other vars
  DATABASE_URL: "mysql://root:mypassword@/data?unix_socket=/cloudsql/projectname:region:instancename"

Laravel will automatically parse this URL and use the correct socket, host, credentials, etc., so you don't need to set DB_HOST, DB_DATABASE, etc., separately.

Try these steps one by one—most people fix the issue just by updating DB_HOST to an empty string. Let me know if you still run into problems!

内容的提问来源于stack exchange,提问作者Stijn Tratsaert

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:20:33