{
  "id": "cloud/gcp-cloud-sql-connection-limit-exceeded",
  "signature": "OperationalError: (pymysql.err.OperationalError) (1040, 'Too many connections')",
  "signature_zh": "连接错误：(pymysql.err.OperationalError) (1040, '连接数过多')",
  "regex": "OperationalError.*\\(1040.*Too many connections",
  "domain": "cloud",
  "category": "resource_error",
  "subcategory": null,
  "root_cause": "Cloud SQL instance has reached its max_connections limit, often due to connection leaks or insufficient instance tier.",
  "root_cause_type": "generic",
  "root_cause_zh": "Cloud SQL 实例已达到 max_connections 限制，通常由连接泄漏或实例规格不足引起。",
  "versions": [
    {
      "version": "Cloud SQL for MySQL 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Cloud SQL for PostgreSQL 15",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Cloud SQL Proxy 2.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Restarting the instance resets connections but doesn't fix the leak; it recurs quickly.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Only increasing max_connections in flags without addressing connection pooling or instance memory leads to OOM or crash.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Adding more application instances without connection limits just multiplies the connections hitting the same limit.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use a connection pool like HikariCP (Java) or SQLAlchemy pool_pre_ping=True with pool_size=10 and max_overflow=0. In Python: engine = create_engine('mysql+pymysql://user:pass@host/db', pool_size=10, max_overflow=0, pool_pre_ping=True)",
      "success_rate": 0.9,
      "how": "Use a connection pool like HikariCP (Java) or SQLAlchemy pool_pre_ping=True with pool_size=10 and max_overflow=0. In Python: engine = create_engine('mysql+pymysql://user:pass@host/db', pool_size=10, max_overflow=0, pool_pre_ping=True)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Temporarily increase max_connections via Cloud SQL flags: gcloud sql instances patch INSTANCE_NAME --database-flags max_connections=500, then scale instance tier if needed.",
      "success_rate": 0.85,
      "how": "Temporarily increase max_connections via Cloud SQL flags: gcloud sql instances patch INSTANCE_NAME --database-flags max_connections=500, then scale instance tier if needed.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Kill idle connections manually: SELECT CONCAT('KILL ', id, ';') FROM information_schema.processlist WHERE command = 'Sleep' AND time > 300; then execute the generated KILL statements.",
      "success_rate": 0.75,
      "how": "Kill idle connections manually: SELECT CONCAT('KILL ', id, ';') FROM information_schema.processlist WHERE command = 'Sleep' AND time > 300; then execute the generated KILL statements.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "使用连接池如 HikariCP (Java) 或 SQLAlchemy pool_pre_ping=True 配合 pool_size=10 和 max_overflow=0。Python 示例：engine = create_engine('mysql+pymysql://user:pass@host/db', pool_size=10, max_overflow=0, pool_pre_ping=True)",
    "临时通过 Cloud SQL 标志增加 max_connections：gcloud sql instances patch INSTANCE_NAME --database-flags max_connections=500，然后根据需要扩展实例规格。",
    "手动清理空闲连接：SELECT CONCAT('KILL ', id, ';') FROM information_schema.processlist WHERE command = 'Sleep' AND time > 300；然后执行生成的 KILL 语句。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://cloud.google.com/sql/docs/mysql/troubleshoot",
  "official_doc_section": null,
  "error_code": "1040",
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-06-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}