fix: 修复远程部署MySQL初始化流程

This commit is contained in:
Nixevol
2026-07-05 18:18:33 +08:00
parent d73c8d3969
commit 4c864becaa
3 changed files with 29 additions and 22 deletions
+1 -1
View File
@@ -63,7 +63,7 @@ python scripts/deploy_remote.py
MySQL / Redis 只暴露在 Artveris 专属 Docker 网络内,不映射宿主机 `3306` / `6379`,因此不会和服务器已有 MySQL / Redis 端口冲突。
首次部署且数据卷为空时,MySQL 容器会按脚本生成的 `MYSQL_DATABASE` 自动创建业务库,API 容器启动时会自动执行 Prisma 迁移创建表结构;重复部署时会复用已有数据卷,不会重置数据库。
部署包还会生成 `mysql-init.sql` 并由一次性 `mysql-init` 容器执行,确保业务库、`MYSQL_USER` 以及 `'%'` 远程授权存在;API 会等该授权步骤成功后再启动,避免旧数据卷或 MySQL 用户授权异常导致 API 连不上数据库。
部署包还会生成 `mysql-init.sql`,脚本会先启动 MySQL/Redis,再进入 MySQL 容器内部执行该 SQL,确保业务库、`MYSQL_USER` 以及 `'%'` 授权存在;随后才启动 API/Web,避免旧数据卷或 MySQL 用户授权异常导致 API 连不上数据库。
HTTPS 与域名继续由 1Panel 反向代理配置,例如:
+26 -20
View File
@@ -148,8 +148,8 @@ def ask_config() -> DeployConfig:
cdn_base_url = normalize_url(ask("CDN_BASE_URL"))
include_base_images = ask_yes_no(
"是否同时打包 MySQL/Redis/Nginx 基础镜像(服务器拉取 Docker Hub 不稳定时建议开启)",
True,
"是否同时打包 MySQL/Redis/Nginx 基础镜像(服务器拉取 Docker Hub 不稳定时可开启)",
False,
)
log("请填写 SSH 信息,用于上传并启动远程 Docker。")
@@ -349,6 +349,7 @@ services:
- "3306"
volumes:
- artveris_mysql:/var/lib/mysql
- ./mysql-init.sql:/artveris-init.sql:ro
networks:
- artveris_private
healthcheck:
@@ -357,21 +358,6 @@ services:
timeout: 5s
retries: 30
mysql-init:
image: mysql:8.0
restart: "no"
depends_on:
mysql:
condition: service_healthy
environment:
MYSQL_PWD: ${MYSQL_ROOT_PASSWORD}
volumes:
- ./mysql-init.sql:/artveris-init.sql:ro
networks:
- artveris_private
command: >-
sh -c 'mysql -hmysql -uroot --default-character-set=utf8mb4 < /artveris-init.sql'
redis:
image: redis:7-alpine
restart: unless-stopped
@@ -401,8 +387,8 @@ services:
image: ${API_IMAGE}
restart: unless-stopped
depends_on:
mysql-init:
condition: service_completed_successfully
mysql:
condition: service_healthy
redis:
condition: service_healthy
environment:
@@ -700,6 +686,15 @@ def remote_connection_check_command(remote_dir: str) -> str:
)
def remote_mysql_init_command(remote_dir: str) -> str:
return (
f"cd {shlex.quote(remote_dir)} && "
"docker compose --env-file .env -f docker-compose.remote.yml "
"exec -T mysql sh -c "
+ shlex.quote('MYSQL_PWD="$MYSQL_ROOT_PASSWORD" mysql -uroot --default-character-set=utf8mb4 < /artveris-init.sql')
)
def deploy_remote(cfg: DeployConfig, archive_path: Path, api_image: str) -> None:
ssh = ssh_connect(cfg)
remote_archive = f"/tmp/{archive_path.name}"
@@ -732,7 +727,18 @@ def deploy_remote(cfg: DeployConfig, archive_path: Path, api_image: str) -> None
" && ".join(
[
f"cd {remote_dir_q}",
"docker compose --env-file .env -f docker-compose.remote.yml up -d",
"docker compose --env-file .env -f docker-compose.remote.yml up -d mysql redis",
"docker compose --env-file .env -f docker-compose.remote.yml ps mysql redis",
]
),
)
remote_exec(ssh, remote_mysql_init_command(cfg.remote_dir))
remote_exec(
ssh,
" && ".join(
[
f"cd {remote_dir_q}",
"docker compose --env-file .env -f docker-compose.remote.yml up -d api web",
"docker compose --env-file .env -f docker-compose.remote.yml ps",
f"rm -f {remote_archive_q}",
]