# sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")

- **ID:** `python/flask-sqlalchemy-connection-error`
- **Domain:** python
- **Category:** network_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Flask-SQLAlchemy无法连接到数据库服务器，可能是服务未运行或配置错误

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **确保数据库服务正在运行** (90% success)
   ```
   sudo systemctl start mysql
   ```
2. **检查数据库连接配置** (95% success)
   ```
   app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://user:pass@localhost:3306/db'
   ```

## Dead Ends

- **尝试使用不同的数据库驱动** — 连接问题通常是服务器或网络问题，不是驱动问题 (50% fail)
- **修改数据库URL但忽略端口** — 端口错误会导致连接失败 (40% fail)
