Laravel应用部署在Google App Engine无法连接Cloud SQL数据库求助
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_HOSTis set tolocalhost, 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_HOSTto an empty string or remove it entirely (let Laravel use the defaultnullvalue fromconfig/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_instancesvalue inbeta_settingsmust exactly match the instance name inDB_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_socketline is properly pulling fromenv('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 theCloud SQL Clientrole 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:cachelocally before deploying, the oldDB_HOSTvalue might be cached. Re-runphp artisan config:cacheafter updating your env vars, or deploy with fresh code (GAE Flex will runcomposer installand can regenerate the cache if you include the necessary commands inapp.yaml). - Cloud SQL Instance Status: Confirm your Cloud SQL instance is in the
RUNNINGstate (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

