同一GCloud项目中Laravel 5.5应用无法连接MySQL实例
Hey there! Let's figure out why your Laravel app's login flow is failing to connect to your Cloud SQL database. Since your homepage loads perfectly, the app itself is deployed correctly—so the issue is almost certainly tied to your database connection setup. Here's how to fix it:
1. Add Missing Database Environment Variables to app.yaml
GAE Flexible doesn't use your local .env file; all environment variables need to be defined directly in app.yaml under env_variables. Right now, your config doesn't include any database-related variables, which is a big red flag. Update your app.yaml like this:
runtime: php env: flex runtime_config: document_root: public skip_files: - .env env_variables: APP_LOG: errorlog APP_DEBUG: true APP_KEY: MY-APP-KEY-HERE # Add these database variables DB_CONNECTION: mysql DB_HOST: 127.0.0.1 DB_PORT: 3306 DB_DATABASE: your-database-name DB_USERNAME: your-db-username DB_PASSWORD: your-db-password # Critical for Cloud SQL socket connection DB_SOCKET: "/cloudsql/your-project-id:your-region:your-sql-instance-name"
Make sure to replace the placeholders (like your-project-id, your-database-name) with your actual Cloud SQL details. The DB_SOCKET is essential for connecting to Cloud SQL without needing a public IP.
2. Verify Cloud SQL Authorization Settings
Your GAE service account needs permission to access the Cloud SQL instance:
- Go to your Cloud SQL instance in the GCP Console, navigate to the Connections tab.
- Under Cloud SQL Auth Proxy, ensure the default GAE service account (
your-project-id@appspot.gserviceaccount.com) has the Cloud SQL Client role assigned to it. - If you're using a public IP instead of the socket, add GAE's outbound IP addresses to your Cloud SQL instance's authorized networks (though socket connections are recommended for security and reliability).
3. Check Laravel's Database Configuration
Open config/database.php and confirm your MySQL driver is pulling values from environment variables:
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), // Keep other default settings like charset, collation, etc. ],
The key line here is 'unix_socket' => env('DB_SOCKET', '')—this ensures Laravel uses the Cloud SQL socket you defined in app.yaml.
4. Dig Into Application Logs for Specific Errors
To get the exact error message (like "Access denied" or "Can't connect to MySQL server"), check your GAE logs:
- Use the GCP Console: Go to App Engine > Versions > View Logs for your deployed version.
- Or run this command in your terminal:
gcloud app logs tail -s default
The logs will tell you exactly what's going wrong—whether it's a wrong password, missing permissions, or incorrect socket path.
5. Redeploy Your App
After updating app.yaml, deploy the changes with:
gcloud app deploy
This ensures the new environment variables are loaded into your GAE instance.
Once you work through these steps, your login flow should successfully connect to the Cloud SQL database. If you still hit snags, share the specific error from the logs and we can troubleshoot further!
内容的提问来源于stack exchange,提问作者Luis-Orlando Guzman

