wecom-dashboards/deploy/CONFIG.md

267 lines
5.8 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.

# 使用现有 MySQL 和 Redis 容器的配置说明
## 📋 当前配置
你的现有容器:
- **MySQL**: `mysql-jijin-test` (端口 3316)
- **Redis**: `redis` (端口 6379)
## 🔧 配置步骤
### 1. 修改 docker-compose.yml
已经配置为使用 `host.docker.internal` 连接宿主机的容器。
关键配置:
```yaml
environment:
# MySQL 连接(注意端口是 3316
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3316/ry-vue?...
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=password # 修改为你的密码
# Redis 连接
- SPRING_REDIS_HOST=host.docker.internal
- SPRING_REDIS_PORT=6379
- SPRING_REDIS_PASSWORD= # 如果有密码,填写这里
extra_hosts:
- "host.docker.internal:host-gateway" # 允许容器访问宿主机
```
### 2. 修改数据库配置
**重要**:请根据你的实际情况修改以下配置:
#### 数据库名称
默认使用 `ry-vue`,如果你的数据库名称不同,修改:
```yaml
SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3316/你的数据库名?...
```
#### 数据库密码
修改为你的 MySQL root 密码:
```yaml
SPRING_DATASOURCE_PASSWORD=你的密码
```
#### Redis 密码
如果你的 Redis 设置了密码,修改:
```yaml
SPRING_REDIS_PASSWORD=你的redis密码
```
### 3. 准备数据库
在你的 MySQL 容器中创建数据库并导入数据:
```bash
# 方式 1: 进入 MySQL 容器
docker exec -it mysql-jijin-test mysql -uroot -p
# 创建数据库
CREATE DATABASE IF NOT EXISTS `ry-vue` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
# 退出
exit
# 方式 2: 导入 SQL 文件
docker exec -i mysql-jijin-test mysql -uroot -p你的密码 ry-vue < /path/to/your.sql
```
### 4. 启动服务
```bash
cd deploy
docker-compose up -d
```
## 🔍 验证连接
### 检查后端日志
```bash
docker-compose logs -f backend
```
如果看到类似以下内容,说明连接成功:
```
Started RuoYiApplication in X seconds
```
### 测试数据库连接
```bash
# 进入后端容器
docker exec -it wecom-backend sh
# 测试 MySQL 连接
wget -O- http://host.docker.internal:3316 2>&1 | grep -i mysql
# 测试 Redis 连接
ping host.docker.internal
```
## 🐛 常见问题
### 1. 无法连接到 host.docker.internal
**Windows/Mac**: `host.docker.internal` 自动可用
**Linux**: 需要手动添加,已在 docker-compose.yml 中配置:
```yaml
extra_hosts:
- "host.docker.internal:host-gateway"
```
### 2. 数据库连接被拒绝
检查 MySQL 容器是否允许远程连接:
```bash
# 进入 MySQL 容器
docker exec -it mysql-jijin-test mysql -uroot -p
# 检查用户权限
SELECT host, user FROM mysql.user WHERE user='root';
# 如果 host 只有 localhost需要授权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的密码';
FLUSH PRIVILEGES;
```
### 3. 端口连接错误
确认你的 MySQL 端口是 3316
```bash
docker ps | grep mysql
```
如果端口不同,修改 docker-compose.yml 中的端口号。
### 4. Redis 连接失败
检查 Redis 是否允许远程连接:
```bash
# 查看 Redis 配置
docker exec -it redis redis-cli CONFIG GET bind
# 如果绑定了 127.0.0.1,需要修改为 0.0.0.0
docker exec -it redis redis-cli CONFIG SET bind "0.0.0.0"
```
## 🔄 替代方案:使用 Docker 网络
如果 `host.docker.internal` 不工作,可以让新容器加入现有容器的网络:
### 1. 查看现有容器的网络
```bash
docker inspect mysql-jijin-test | grep NetworkMode
docker inspect redis | grep NetworkMode
```
### 2. 修改 docker-compose.yml
假设现有容器在 `bridge` 网络:
```yaml
services:
backend:
# ... 其他配置
environment:
# 直接使用容器名连接
- SPRING_DATASOURCE_URL=jdbc:mysql://mysql-jijin-test:3306/ry-vue?...
- SPRING_REDIS_HOST=redis
networks:
- default
networks:
default:
external: true
name: bridge # 或者你的网络名称
```
### 3. 或者创建自定义网络
```bash
# 创建网络
docker network create my-network
# 将现有容器连接到网络
docker network connect my-network mysql-jijin-test
docker network connect my-network redis
# 修改 docker-compose.yml
networks:
wecom-network:
external: true
name: my-network
```
## 📝 完整配置示例
### docker-compose.yml使用 host.docker.internal
```yaml
version: '3.8'
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile
container_name: wecom-backend
restart: always
ports:
- "8080:8080"
volumes:
- upload_data:/home/ruoyi/uploadPath
environment:
- SPRING_PROFILES_ACTIVE=prod
- TZ=Asia/Shanghai
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3316/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=your_password
- SPRING_REDIS_HOST=host.docker.internal
- SPRING_REDIS_PORT=6379
- SPRING_REDIS_PASSWORD=
extra_hosts:
- "host.docker.internal:host-gateway"
networks:
- wecom-network
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
container_name: wecom-frontend
restart: always
ports:
- "8081:80"
depends_on:
- backend
networks:
- wecom-network
volumes:
upload_data:
driver: local
networks:
wecom-network:
driver: bridge
```
## 🎯 快速检查清单
- [ ] 修改了 MySQL 密码配置
- [ ] 修改了数据库名称(如果不是 ry-vue
- [ ] 修改了 Redis 密码(如果有)
- [ ] 确认 MySQL 端口是 3316
- [ ] 确认 Redis 端口是 6379
- [ ] 在 MySQL 中创建了数据库
- [ ] 导入了数据库初始化脚本
- [ ] MySQL 允许远程连接
- [ ] Redis 允许远程连接
完成以上检查后,运行 `docker-compose up -d` 即可启动服务。