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

Symfony Console连接Google Cloud SQL失败:SQLSTATE[HY000] [2002]错误排查

Hey there, let's break down the possible reasons why your Symfony Console command is failing to connect to the database while your web routes work smoothly on Google Cloud. Here are the key areas to investigate step by step:

1. Environment Variable Mismatches

Web requests and Symfony Console commands often load environment configurations differently.

  • Double-check your .env files (or environment-specific ones like .env.prod) to ensure the console command is picking up the correct database connection string. On Google Cloud, web servers might automatically pull variables from services like Secret Manager or instance-level env vars, but the CLI might not inherit these by default.
  • Quick test: Add a temporary line in your console command to output $this->getParameter('database_url') (or getenv('DATABASE_URL')) and compare it to the value used in your web routes. If they differ, that's a red flag.

2. Database Network Access Restrictions

Google Cloud databases (like Cloud SQL) enforce strict access controls, and the CLI's runtime environment might not be authorized:

  • For Cloud SQL, head to the Connections tab in your instance settings and check the Authorized networks list. The web server's IP is likely allowed, but the IP of your CLI environment (local machine, Cloud Run job runner, or GCE instance) might be missing.
  • If you're using the Cloud SQL Auth Proxy: Web environments often have this pre-configured, but CLI commands need the proxy running in the background or integrated into the command execution. Make sure the proxy is active and your CLI is pointing to the correct socket/port.

3. CLI-Specific Database Connection Settings

Symfony might use different connection configurations for CLI vs web requests:

  • Check your Doctrine config files (e.g., config/packages/doctrine.yaml or config/packages/prod/doctrine.yaml). Web environments sometimes use Unix sockets (like /cloudsql/your-instance-connection-name for Cloud SQL) which are more efficient, but CLI commands might default to TCP connections—leading to that "connection refused" error if the TCP port isn't accessible.
  • If using Unix sockets: Ensure your CLI environment has permission to access the socket (e.g., in Cloud Run jobs, enable the Cloud SQL connection to mount the socket; locally, use the auth proxy to expose the socket).

4. Cache or Dependency Discrepancies

CLI and web environments can sometimes have mismatched cache or dependencies:

  • Clear your Symfony cache for the relevant environment with ./console cache:clear --env=prod (adjust the env flag as needed). Stale cache might be loading outdated database configs for the CLI.
  • Verify PHP extensions: The web server might have pdo_mysql (or your database driver) enabled, but the CLI's PHP installation could be missing it. Run php -m in your CLI environment to check installed extensions, and compare it to the phpinfo() output from your web routes.

5. Service Account Permissions

If your app uses a service account to access Cloud SQL:

  • Confirm that the service account used by the CLI has the Cloud SQL Client role. Web environments might use the instance's default service account with proper permissions, but the CLI could be using a different account (like a local key file) that lacks access.

内容的提问来源于stack exchange,提问作者DaAmidza

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:08:54