fix: 修复远程部署MySQL授权

This commit is contained in:
Nixevol
2026-07-05 18:11:08 +08:00
parent 53b63b52fb
commit d73c8d3969
3 changed files with 43 additions and 3 deletions
+1
View File
@@ -63,6 +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 连不上数据库。
HTTPS 与域名继续由 1Panel 反向代理配置,例如:
+41 -3
View File
@@ -308,6 +308,28 @@ def write_env_file(path: Path, cfg: DeployConfig, image_tag: str) -> None:
path.write_text("\n".join(lines), encoding="utf-8", newline="\n")
def sql_string(value: str) -> str:
return "'" + value.replace("\\", "\\\\").replace("'", "''") + "'"
def sql_identifier(value: str) -> str:
return "`" + value.replace("`", "``") + "`"
def write_mysql_init_sql(path: Path, cfg: DeployConfig) -> None:
database = sql_identifier(cfg.mysql_database)
user = sql_string(cfg.mysql_user)
password = sql_string(cfg.mysql_password)
sql = f"""CREATE DATABASE IF NOT EXISTS {database}
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER IF NOT EXISTS {user}@'%' IDENTIFIED BY {password};
ALTER USER {user}@'%' IDENTIFIED BY {password};
GRANT ALL PRIVILEGES ON {database}.* TO {user}@'%';
FLUSH PRIVILEGES;
"""
path.write_text(sql, encoding="utf-8", newline="\n")
def compose_text() -> str:
return """name: artveris
@@ -335,6 +357,21 @@ 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
@@ -364,8 +401,8 @@ services:
image: ${API_IMAGE}
restart: unless-stopped
depends_on:
mysql:
condition: service_healthy
mysql-init:
condition: service_completed_successfully
redis:
condition: service_healthy
environment:
@@ -549,6 +586,7 @@ def build_release(cfg: DeployConfig) -> tuple[Path, Path, str]:
(bundle_dir / "docker-compose.remote.yml").write_text(compose_text(), encoding="utf-8", newline="\n")
(bundle_dir / "nginx.conf").write_text(nginx_text(), encoding="utf-8", newline="\n")
write_env_file(bundle_dir / ".env", cfg, image_tag)
write_mysql_init_sql(bundle_dir / "mysql-init.sql", cfg)
(bundle_dir / "manifest.json").write_text(
json.dumps(
{
@@ -678,7 +716,7 @@ def deploy_remote(cfg: DeployConfig, archive_path: Path, api_image: str) -> None
" && ".join(
[
f"cd {remote_dir_q}",
"rm -rf admin-dist client-dist images docker-compose.remote.yml nginx.conf .env manifest.json",
"rm -rf admin-dist client-dist images docker-compose.remote.yml nginx.conf mysql-init.sql .env manifest.json",
f"tar -xzf {remote_archive_q} -C {remote_dir_q} --strip-components=1",
f"docker load -i {shlex.quote(posixpath.join(cfg.remote_dir, 'images/artveris-api.tar'))}",
]