You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 宝塔 + 宿主机 MySQL + Docker 应用(内网 30327)
与查价项目同一套模式:**MySQL 在宝塔,应用在 Docker**。
适用于:
- MySQL:**宝塔面板**,库名 `email_forecast`
- 应用:**Docker 容器**(web + worker)
- 代码目录:`/home/project/yubao`
- 访问:内网 `http://服务器IP:30327`
---
## 一、宝塔 MySQL 准备
1. 创建数据库 **`email_forecast`**
- 字符集:**utf8mb4**
- 校对规则:**utf8mb4_unicode_ci**(与 SQL 一致;选错会出现「字符集不一致可能导致恢复失败」)
2. 创建用户 **`email_forecast`**,赋予该库全部权限
3. 授权 Docker 网段(容器经 `host.docker.internal` 连宿主机):
```sql
-- 在宝塔 phpMyAdmin 或终端执行(按实际密码改)
CREATE USER 'email_forecast'@'172.%' IDENTIFIED BY '你的强密码';
GRANT ALL PRIVILEGES ON email_forecast.* TO 'email_forecast'@'172.%';
FLUSH PRIVILEGES;
```
4. **推荐**:导入 `数据库/email_forecast.sql`(本机完整数据);也可不导入,由 install 脚本 `prisma db push` + seed
### 测试(在服务器上)
```bash
mysql -h 127.0.0.1 -u email_forecast -p email_forecast
```
---
## 二、开发机打包
```powershell
cd X:\work\yx
powershell -ExecutionPolicy Bypass -File deploy\pack-server-upload.ps1
# 可选压缩便于上传
Compress-Archive -Path "上传到服务器\代码\*" -DestinationPath "yubao-code.zip" -Force
```
将 **`上传到服务器/代码/`** 解压到服务器 `/home/project/yubao/`。
---
## 三、配置 .env
```bash
cd /home/project/yubao
cp deploy/.env.bt-host-mysql.example .env
nano .env
```
必改项:
| 变量 | 说明 |
|------|------|
| `APP_PORT` | 对外端口,默认 `30327`(勿与 chajia 30325 冲突) |
| `PUBLIC_HOST` | 服务器内网 IP |
| `MYSQL_PASSWORD` | 宝塔里 email_forecast 用户密码 |
| `DATABASE_URL` | `mysql://email_forecast:密码@host.docker.internal:3306/email_forecast` |
| `SESSION_SECRET` | ≥32 字符随机串 |
| `APP_ADMIN_PASS` / `APP_OPS_PASS` | **不能**用 admin123/ops123,且二者须不同 |
| `OAUTH_PUBLIC_BASE_URL` | `http://内网IP:30327` |
| `CC_MOCK` | 无 CC 账号时 `true`;有账号改 `false` 并在设置页填凭据 |
密码含 `@`、`#` 等需 [URL 编码](https://www.urlencoder.org/)。
---
## 四、首次启动
```bash
chmod +x deploy/install-bt-host-mysql.sh
bash deploy/install-bt-host-mysql.sh
```
脚本会:校验 MySQL → 构建 web/worker → `prisma db push` → seed 账号 → 健康检查。
**不要**用根目录 `docker-compose.yml` 里的容器 MySQL(那是本地开发用的 7023 端口)。
---
## 五、更新代码
```bash
bash deploy/update-code-only-bt-host-mysql.sh
# Prisma schema 有变更时:
bash deploy/update-code-only-bt-host-mysql.sh --db-push
```
---
## 六、内网访问
| 用途 | 地址 |
|------|------|
| 登录 | `http://192.168.x.x:30327/login` |
| 邮件 | `http://192.168.x.x:30327/mails` |
| 健康检查 | `http://192.168.x.x:30327/api/health` |
---
## 七、宝塔防火墙
- 放行 **30327**(内网即可)
- **不要**对公网开放 3306
---
## 八、架构示意
```
内网浏览器 ──HTTP:30327──► [Docker] web (Next.js :3100)
│
├──► [Docker] worker (IMAP 拉取 + 解析)
│
└──► host.docker.internal:3306
│
[宝塔] MySQL email_forecast
```
---
## 九、故障排查
| 现象 | 处理 |
|------|------|
| `Can't connect to MySQL` | 用户是否授权 `172.%`;`DATABASE_URL` 是否用 `host.docker.internal` |
| 生产启动报 SESSION_SECRET / 弱口令 | 改 `.env` 中 `SESSION_SECRET`、`APP_*_PASS` |
| Worker 不拉信 | `docker compose -f deploy/docker-compose.bt-host-mysql.yml logs -f worker`;检查设置页邮箱绑定 |
| 端口不通 | 宝塔防火墙、安全组是否放行 30327 |
| API 404(带 `[id]` 的路由) | Windows 打包方括号路径丢失;重新 `pack-server-upload.ps1` 并核对 RequiredRoutes |
---
## 十、导出本机库
```bash
# 本机(需 MySQL 客户端 + 本地 docker mysql 在跑)
bash deploy/database/export-local.sh
# 或直接:
# docker exec yx-mysql-1 mysqldump -uapp -papp email_forecast > deploy/database/email_forecast.sql
```