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.

126 lines
3.5 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 应用(内网 30325
适用于:
- MySQL**宝塔面板**,已导入 `chajia.sql`
- Redis**本项目 Docker Compose 内的 redis 容器**
- 应用:**Docker 容器**next-app + rpa-worker + scheduler
- 访问:内网 `http://服务器IP:30325`
---
## 一、宝塔 MySQL 准备
1. 已创建数据库 `chajia`,字符集 **utf8mb4**
2. 已导入 `数据库/chajia.sql`
3. 已创建数据库用户(如 `chajia_user`),并赋予 **chajia 库全部权限**
### 允许 Docker 容器连接宿主机 MySQL
容器通过 `host.docker.internal` 访问宿主机。宝塔 MySQL 默认可能只监听 `127.0.0.1`,需确认:
1. 宝塔 → **数据库** → **root 密码****权限** / 或 MySQL 设置
2. 确保 MySQL 对 Docker 网段可连(常见做法二选一):
**做法 A推荐**:在宝塔为该用户授权远程主机 `172.%``172.17.%`
```sql
-- 在宝塔 phpMyAdmin 或终端执行(按实际用户名改)
CREATE USER 'chajia_user'@'172.%' IDENTIFIED BY '你的密码';
GRANT ALL PRIVILEGES ON chajia.* TO 'chajia_user'@'172.%';
FLUSH PRIVILEGES;
```
**做法 B**MySQL 配置 `bind-address = 0.0.0.0`(需评估安全,仅内网服务器可考虑)
### 测试(在服务器上)
```bash
# 查看 Docker 网桥网关(多为 172.17.0.1
ip addr show docker0
# 从宿主机测试宝塔用户能否登录
mysql -h 127.0.0.1 -u chajia_user -p chajia
```
---
## 二、配置 .env
```bash
cd /opt/chajia # 你的代码目录
cp deploy/.env.bt-host-mysql.example .env
nano .env
```
必改项:
| 变量 | 示例 |
|------|------|
| `APP_PORT` | `30325` |
| `PUBLIC_HOST` | 服务器内网 IP`192.168.1.100` |
| `DATABASE_URL` | `mysql://chajia_user:密码@host.docker.internal:3306/chajia` |
| `JWT_SECRET` | 随机长串 |
| `HOST_SERVICE_TOKENS` | 给内网其它系统的 API Token |
密码含 `@`、`#` 等需 [URL 编码](https://www.urlencoder.org/)。
---
## 三、启动
```bash
chmod +x deploy/install-bt-host-mysql.sh
bash deploy/install-bt-host-mysql.sh
```
**不要**再执行 `install.sh`(那个会再起一个容器 MySQL
---
## 四、内网访问
| 用途 | 地址 |
|------|------|
| 管理端 | `http://192.168.x.x:30325/admin/alerts` |
| API | `http://192.168.x.x:30325/api/...` |
| 默认账号 | `admin_demo` / `Demo@123` |
内网其它系统 Base URL`http://<服务器IP>:30325`
详见 `deploy/API.md`
---
## 五、宝塔防火墙 / 安全组
- 放行 **30325**(内网互通即可,不必对公网开放)
- **不要**对公网开放 3306MySQL、6379Redis
若用宝塔 **Docker 管理器** 发布,端口映射填:`30325 → 3000`。
---
## 六、故障排查
| 现象 | 处理 |
|------|------|
| `Can't connect to MySQL` | 检查用户是否授权 `172.%``DATABASE_URL` 主机是否为 `host.docker.internal` |
| 一直 `processing` | `docker compose -f deploy/docker-compose.bt-host-mysql.yml logs -f rpa-worker` |
| 端口不通 | 宝塔防火墙、iptables、云安全组是否放行 30325 |
| 连不上 Redis | 确认 redis 容器 Up应用应使用 `redis://redis:6379`compose 已注入) |
---
## 七、架构示意
```
内网其它系统 ──HTTP:30325──► [Docker] next-app
├──► [Docker] redis
└──► host.docker.internal:3306
[宝塔] MySQL chajia
```