Files
012-kaopeilian/backend/数据库配置切换说明.md
111 998211c483 feat: 初始化考培练系统项目
- 从服务器拉取完整代码
- 按框架规范整理项目结构
- 配置 Drone CI 测试环境部署
- 包含后端(FastAPI)、前端(Vue3)、管理端

技术栈: Vue3 + TypeScript + FastAPI + MySQL
2026-01-24 19:33:28 +08:00

104 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 数据库配置切换说明
## 当前配置
- **公网数据库地址**
- 主机: `120.79.247.16``aiedu.ireborn.com.cn`
- 端口: `3306`
- 数据库名: `kaopeilian`
- 用户: `root`
- 密码: `Kaopeilian2025!@#`
## 连接字符串
### 原始密码(包含特殊字符)
```
Kaopeilian2025!@#
```
### URL编码后的密码
```
Kaopeilian2025%21%40%23
```
### 完整的SQLAlchemy连接字符串
```
mysql+aiomysql://root:Kaopeilian2025%21%40%23@120.79.247.16:3306/kaopeilian?charset=utf8mb4
```
## 配置方法
### 方法1使用环境变量文件推荐
创建 `.env` 文件:
```bash
DATABASE_URL=mysql+aiomysql://root:Kaopeilian2025%21%40%23@120.79.247.16:3306/kaopeilian?charset=utf8mb4
REDIS_URL=redis://localhost:6379/0
SECRET_KEY=dev-secret-key-for-testing-only-not-for-production
DEBUG=true
LOG_LEVEL=INFO
```
### 方法2使用启动脚本
使用已配置好的启动脚本:
```bash
# 使用公网数据库
python3 start_remote.py
# 使用本地数据库(需要修改回本地配置)
python3 start_mysql.py
```
### 方法3直接设置环境变量
```bash
export DATABASE_URL="mysql+aiomysql://root:Kaopeilian2025%21%40%23@120.79.247.16:3306/kaopeilian?charset=utf8mb4"
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
```
### 方法4Docker Compose
`docker-compose.dev.yml` 已更新为使用公网数据库:
```yaml
environment:
DATABASE_URL: mysql+aiomysql://root:Kaopeilian2025%21%40%23@120.79.247.16:3306/kaopeilian?charset=utf8mb4
REDIS_URL: redis://redis:6379/0
```
## 测试连接
运行测试脚本验证连接:
```bash
python3 test_remote_db.py
```
## 切换回本地数据库
如需切换回本地数据库,修改连接字符串为:
```
mysql+aiomysql://root:root@localhost:3306/kaopeilian?charset=utf8mb4
```
## 注意事项
1. **密码编码**密码中的特殊字符必须进行URL编码
- `!``%21`
- `@``%40`
- `#``%23`
2. **网络连接**:确保开发机器能够访问公网数据库服务器
3. **安全性**:生产环境不要在代码中硬编码密码,使用环境变量或密钥管理服务
4. **性能**:公网数据库可能有一定延迟,开发时请注意
## 已更新的文件
- `start_mysql.py` - 启动脚本(已改为公网数据库)
- `start_remote.py` - 新增的公网数据库启动脚本
- `docker-compose.dev.yml` - Docker配置已改为公网数据库
- `test_remote_db.py` - 数据库连接测试脚本