Files
ylt_diy/docs/数据库变更与部署文档.md

377 lines
7.3 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.

# 订单日报系统 数据库变更与部署文档
## 一、数据库变更说明
### 1.1 新增表:t_daily_report_global_config
**表结构**:
```sql
CREATE TABLE IF NOT EXISTS t_daily_report_global_config (
id BIGINT NOT NULL DEFAULT 1 COMMENT '主键(固定为1)',
sum_data_fields VARCHAR(1000) NULL COMMENT '求和数据采集和显示字段列表(JSON格式)',
create_time DATETIME NULL COMMENT '创建时间',
update_time DATETIME NULL COMMENT '更新时间'
)
UNIQUE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES("replication_num" = "1")
```
**字段说明**:
| 字段名 | 类型 | 说明 |
|--------|------|------|
| id | BIGINT | 主键,固定为1(全局配置只有一条记录) |
| sum_data_fields | VARCHAR(1000) | 求和数据采集和显示字段列表,JSON格式 |
| create_time | DATETIME | 创建时间 |
| update_time | DATETIME | 更新时间 |
**数据示例**:
```sql
INSERT INTO t_daily_report_global_config (id, sum_data_fields, create_time, update_time)
VALUES (1, '["sharp_electricity","peak_electricity","flat_electricity","valley_electricity"]', NOW(), NOW());
```
### 1.2 自动初始化
系统启动时会自动执行以下操作:
1. 检查表是否存在,不存在则创建
2. 检查ID=1的记录是否存在,不存在则初始化一条空记录
**相关代码**:[db_init.py](file:///d:/py/code/ylt_power_diy/ylt_diy/lib/db_init.py#L186-L211)
## 二、部署步骤
### 2.1 开发环境部署
**前置条件**:
- Python 3.12+
- MySQL/Apache Doris 数据库
- Git
**步骤**:
1. **克隆代码**
```bash
git clone <仓库地址>
cd ylt_diy
```
2. **安装依赖**
```bash
pip install -r requirements.txt
```
3. **配置环境变量**(可选)
```bash
# 设置数据库连接(默认值见下文)
export DB_HOST=haoslm2.xicp.net
export DB_PORT=10216
export DB_USER=root
export DB_PASSWORD=your_password
export DB_DATABASE=ylt_power_diy
export DEPLOY_RUN_PORT=5000
```
4. **启动开发服务器**
```bash
python app.py
```
5. **访问系统**
打开浏览器访问:http://localhost:5000
### 2.2 生产环境部署
**前置条件**:
- Linux 服务器(推荐 CentOS 7+)
- Python 3.12+
- MySQL/Apache Doris 数据库
- Nginx(可选,用于反向代理)
**步骤**:
1. **克隆代码**
```bash
git clone <仓库地址>
cd ylt_diy
```
2. **安装依赖**
```bash
pip install -r requirements.txt
```
3. **配置环境变量**
创建 `.env` 文件:
```bash
DEPLOY_RUN_PORT=5000
DB_HOST=haoslm2.xicp.net
DB_PORT=10216
DB_USER=root
DB_PASSWORD=your_password
DB_DATABASE=ylt_power_diy
```
4. **创建服务配置**
创建 `/etc/systemd/system/ylt_daily_report.service`:
```ini
[Unit]
Description=订单日报系统
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/path/to/ylt_diy
Environment="DEPLOY_RUN_PORT=5000"
Environment="DB_HOST=haoslm2.xicp.net"
Environment="DB_PORT=10216"
Environment="DB_USER=root"
Environment="DB_PASSWORD=your_password"
Environment="DB_DATABASE=ylt_power_diy"
ExecStart=/usr/bin/python3 app.py
Restart=always
[Install]
WantedBy=multi-user.target
```
5. **启动服务**
```bash
systemctl daemon-reload
systemctl start ylt_daily_report
systemctl enable ylt_daily_report
```
6. **配置 Nginx 反向代理**(可选)
创建 `/etc/nginx/conf.d/ylt_daily_report.conf`:
```nginx
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /public/ {
root /path/to/ylt_diy;
expires 30d;
}
}
```
7. **重启 Nginx**
```bash
nginx -t
systemctl restart nginx
```
### 2.3 数据库配置
**默认配置**:
| 配置项 | 默认值 | 说明 |
|--------|--------|------|
| DB_HOST | haoslm2.xicp.net | 数据库主机地址 |
| DB_PORT | 10216 | 数据库端口 |
| DB_USER | root | 数据库用户名 |
| DB_PASSWORD | (空) | 数据库密码 |
| DB_DATABASE | ylt_power_diy | 数据库名 |
**连接方式**:
系统使用 PyMySQL 连接数据库,支持 MySQL 和 Apache Doris(MySQL 协议兼容)。
**相关代码**:[db.py](file:///d:/py/code/ylt_power_diy/ylt_diy/lib/db.py)
## 三、版本升级
### 3.1 升级步骤
1. **拉取最新代码**
```bash
cd /path/to/ylt_diy
git pull
```
2. **安装新依赖**(如果有)
```bash
pip install -r requirements.txt
```
3. **重启服务**
```bash
systemctl restart ylt_daily_report
```
### 3.2 数据库自动迁移
系统启动时会自动执行数据库初始化,包括:
1. 创建缺失的表
2. 添加缺失的字段
3. 初始化全局配置记录
**注意**:如果出现数据库迁移失败,请检查数据库连接和权限。
## 四、备份与恢复
### 4.1 数据库备份
```bash
# 使用 mysqldump 备份
mysqldump -h haoslm2.xicp.net -P 10216 -u root -p ylt_power_diy > backup.sql
# 使用 Doris 备份命令(如果使用 Doris)
curl -X POST http://doris-be:8030/api/bootstrap -d '{"type":"backup","backup_name":"daily_report_backup","storage":"hdfs://your-hdfs/path"}'
```
### 4.2 数据库恢复
```bash
# 使用 mysql 命令恢复
mysql -h haoslm2.xicp.net -P 10216 -u root -p ylt_power_diy < backup.sql
```
### 4.3 文件备份
```bash
# 备份报表文件
tar -czvf reports_backup.tar.gz public/reports/
# 备份配置文件
tar -czvf config_backup.tar.gz lib/
```
## 五、常见部署问题
### 5.1 数据库连接失败
**问题**:系统启动时提示数据库连接失败
**解决**:
- 检查数据库主机和端口是否正确
- 确认数据库服务正常运行
- 检查数据库用户名和密码
- 确认网络连通性(ping 数据库主机)
### 5.2 端口被占用
**问题**:启动时提示端口 5000 被占用
**解决**:
```bash
# 查看占用进程
lsof -i :5000
# 杀死占用进程
kill -9 <PID>
# 或修改端口
export DEPLOY_RUN_PORT=5001
```
### 5.3 静态文件加载失败
**问题**:页面样式和脚本无法加载
**解决**:
- 检查 `public/static/` 目录是否存在
- 确认 Flask 应用的 `static_folder` 配置正确
- 清除浏览器缓存(Ctrl + Shift + R)
### 5.4 定时任务不执行
**问题**:每天 08:01 的定时任务没有执行
**解决**:
- 确认服务正常运行(`systemctl status ylt_daily_report`)
- 检查日志文件(`/app/work/logs/bypass/`)
- 确认系统时间正确
- 检查配置是否启用(`is_active = 1`)
### 5.5 内存不足
**问题**:服务运行一段时间后崩溃
**解决**:
- 增加服务器内存
- 优化查询语句,减少内存使用
- 配置定时清理任务
## 六、日志管理
### 6.1 日志位置
- 应用日志:`/app/work/logs/bypass/`
- 系统日志:`/var/log/syslog`(Linux)
### 6.2 日志级别
- INFO:普通信息
- WARNING:警告信息
- ERROR:错误信息
### 6.3 查看日志
```bash
# 查看应用日志
tail -f /app/work/logs/bypass/app.log
# 查看系统日志
journalctl -u ylt_daily_report -f
```
## 七、安全建议
### 7.1 数据库安全
- 使用强密码
- 限制数据库访问 IP
- 定期备份数据库
### 7.2 应用安全
- 使用 HTTPS
- 定期更新依赖
- 关闭调试模式(生产环境)
- 配置防火墙
### 7.3 服务器安全
- 禁用 SSH 密码登录(使用密钥)
- 定期更新系统
- 禁用不必要的服务