Laravel部署GAE后无法连接Cloud SQL:实例未找到问题求助
Fixing Laravel 5.6 + Cloud SQL Connection Issues on App Engine Flexible
Let’s work through your connection problems step by step to get your Laravel app talking to Cloud SQL:
1. Resolve the gcloud sql instances describe Error
The error you’re seeing means either you’re in the wrong Google Cloud project, or there’s a typo in your instance name. Here’s how to fix it:
- First, list all your projects to confirm the correct project ID:
gcloud projects list - Set your active project to the one where you created the
laraveljwtstarterinstance:gcloud config set project YOUR_CORRECT_PROJECT_ID - Re-run the describe command to verify access:
This will return your instance’s details, including the connection name (format:gcloud sql instances describe laraveljwtstarterPROJECT_ID:REGION:INSTANCE_NAME). Save this value—you’ll need it for both local and App Engine connections.
2. Connect Locally with Cloud SQL Proxy
Once you have the connection name, set up the proxy correctly to link your MacBook to Cloud SQL:
- Start the proxy using your instance’s connection name:
./cloud_sql_proxy -instances=YOUR_CONNECTION_NAME=tcp:3306 - Update your Laravel
.envfile for local development:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=YOUR_DATABASE_NAME # Ensure this exists in your Cloud SQL instance DB_USERNAME=YOUR_DB_USER # The user you created for the instance DB_PASSWORD=YOUR_DB_PASSWORD # The user's password - Test the connection by running migrations:
php artisan migrate
3. Configure App Engine Flexible to Connect to Cloud SQL
For your deployed app to connect to Cloud SQL, update your app.yaml with these settings:
- Add environment variables for the database connection:
env_variables: APP_KEY: YOUR_LARAVEL_APP_KEY DB_CONNECTION: mysql DB_HOST: '/cloudsql/YOUR_CONNECTION_NAME' DB_DATABASE: YOUR_DATABASE_NAME DB_USERNAME: YOUR_DB_USER DB_PASSWORD: YOUR_DB_PASSWORD - Add the beta setting to link the Cloud SQL instance to your App Engine service:
beta_settings: cloud_sql_instances: YOUR_CONNECTION_NAME - Redeploy your app to apply changes:
gcloud app deploy
4. Double-Check Critical Details
Make sure these boxes are ticked:
- The database name you’re using exists in your Cloud SQL instance (create it via the GCP Console or using the proxy with a MySQL client).
- Your database user has
SELECT,INSERT,UPDATE,DELETEpermissions on the target database (configure this in the GCP Console under Cloud SQL > Users).
Follow these steps, and your Laravel app should connect to Cloud SQL both locally and on App Engine Flexible without issues.
内容的提问来源于stack exchange,提问作者Connor Leech
相关产品推荐
相关产品推荐

