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

Google App Engine中PHP(PDO)连接Cloud SQL失败求助

Fixing SQLSTATE[HY000] [2002] No such file or directory in GAE Flex with 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 your app.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 in unix_socket=/cloudsql/<MY INSTANCE CONNECTION NAME> exactly matches the connection name from Cloud Console. It should follow the format project-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 (typically your-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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:59:20