Google App Engine中PHP(PDO)连接Cloud SQL失败求助
I’ve run into this exact issue before, so let’s walk through the most likely fixes to get your connection working:
Add Cloud SQL instance configuration to your
app.yaml
This is the #1 oversight when setting up Cloud SQL with GAE Flex. The platform won’t mount the unix socket into your container unless you explicitly define it. Add this section to yourapp.yaml:beta_settings: cloud_sql_instances: "your-project-id:your-region:your-instance-name"Replace the value with your actual instance connection name (found in Cloud Console > Cloud SQL > Your Instance > Overview > Connection name). Don’t forget to redeploy your app after making this change.
Double-check your PDO DSN for typos
Ensure the path inunix_socket=/cloudsql/<MY INSTANCE CONNECTION NAME>exactly matches the connection name from Cloud Console. It should follow the formatproject-id:region:instance-id—no extra slashes, spaces, or misspellings.Enable the Cloud SQL Admin API
Even if you’ve set up IAM roles, the connection will fail if this API is disabled. Head to Cloud Console > APIs & Services > Library, search for "Cloud SQL Admin API", and confirm it’s enabled for your project.Verify your App Engine service account permissions
The default App Engine service account (typicallyyour-project-id@appspot.gserviceaccount.com) needs at least the Cloud SQL Client IAM role. Assign this via Cloud Console > IAM & Admin > IAM > Find the service account > Edit > Add role > Cloud SQL > Cloud SQL Client.Confirm your Cloud SQL instance is running
It sounds trivial, but instances often get stopped to cut costs. Check Cloud Console > Cloud SQL > Your Instance to ensure it’s in the "Running" state.Validate your MySQL user’s host setting
While the error points to a socket issue, a restrictive host on your MySQL user can cause secondary problems. Ensure your user allows connections from the proxy (which runs locally in the Flex container). Use this query to check:SELECT user, host FROM mysql.user WHERE user = 'your-username';If the host is limited to a specific IP, update it with:
GRANT ALL PRIVILEGES ON your-database.* TO 'your-username'@'%' IDENTIFIED BY 'your-password'; FLUSH PRIVILEGES;
After applying these steps, redeploy your app and test the connection again. If you’re still stuck, check the App Engine logs (Cloud Console > App Engine > Services > Your Service > Logs) for more detailed hints about why the socket couldn’t be mounted.
内容的提问来源于stack exchange,提问作者Ohiri Vincent

