diff --git a/aidocs/project_context.md b/aidocs/project_context.md index a274ae3..aae9262 100644 --- a/aidocs/project_context.md +++ b/aidocs/project_context.md @@ -177,6 +177,7 @@ - 远程部署镜像拉取 fallback(2026-07-05):`docker/Dockerfile.api` 新增 `ARG NODE_IMAGE=node:20-slim`,`deploy_remote.py` 构建 API 镜像时先用官方 `node:20-slim`,若 Docker Hub 拉取/构建失败则自动切换 `docker.1panel.live/library/node:20-slim` 重试。远程部署脚本在用户选择打包基础镜像时,也会对 `mysql:8.0`、`redis:7-alpine`、`nginx:alpine` 做官方失败后切 1Panel 代理的 fallback,并把代理镜像 tag 回官方名,保证远程 compose 不需要改镜像名。已用脚本级 monkeypatch 验证 fallback 分支命令生成正确。 - 远程部署容器隔离(2026-07-05):`deploy_remote.py` 生成的生产 compose 显式创建项目专属 `artveris_private` bridge 网络,`mysql`、`redis`、`api`、`web` 都仅加入该网络;MySQL/Redis 只声明容器内 `expose: 3306/6379`,不写宿主机 `ports` 映射,因此不会占用服务器已有 MySQL `3306` 或 Redis `6379`。首次部署且 `artveris_mysql` 数据卷为空时,MySQL 镜像会按 `MYSQL_DATABASE` 自动创建业务库;API 容器启动命令会执行 Prisma `migrate deploy` 创建/更新表结构。重复部署会复用已有数据卷,不会重置数据库,若更换 MySQL 密码需保持旧卷密码或手动迁移。对外只绑定 `BIND_HOST:ADMIN_HTTP_PORT/CLIENT_HTTP_PORT`,默认 `127.0.0.1:18080/18081` 供 1Panel 反代。已用临时 `.env` 执行 `docker compose config --quiet` 校验通过。 - 远程部署连通性校验(2026-07-05):已专项核对 API 连接 MySQL/Redis 的生产环境变量链路:Prisma 读取 `DATABASE_URL`,Redis/BullMQ 读取 `REDIS_HOST/REDIS_PORT/REDIS_PASSWORD`;远程部署 `.env` 生成 `DATABASE_URL=mysql://...@mysql:3306/...`、`REDIS_HOST=redis`、`REDIS_PORT=6379`,且三者都在 `artveris_private` 网络内。`deploy_remote.py` 在 `docker compose up -d` 后新增远程烟测,会在 API 容器里用 Node `net` 模块连接 `mysql:3306` 和 `redis:6379`,若 Docker DNS、网络、服务名或端口异常会直接让部署失败并输出原因。 +- 远程 MySQL 授权修复(2026-07-05):线上部署出现过 API 日志 `Host '172.22.0.4' is not allowed to connect to this MySQL server`,说明 Docker 网络正常但业务用户缺少容器网段授权。`deploy_remote.py` 现在会在部署包生成 `mysql-init.sql`(Python 负责 SQL 字符串/标识符转义),生产 compose 新增一次性 `mysql-init` 服务:等待 `mysql` healthy 后以 root 执行建库、`CREATE USER IF NOT EXISTS '${MYSQL_USER}'@'%'`、`ALTER USER`、`GRANT ALL PRIVILEGES`、`FLUSH PRIVILEGES`;`api` 改为依赖 `mysql-init: service_completed_successfully` 与 `redis: service_healthy`,确保旧数据卷或初始化授权异常时也能自动修正后再启动 API。已用临时 compose 校验 `service_completed_successfully`、挂载 SQL 和特殊字符转义均正常。 ## 当前代码基准与注意事项 diff --git a/scripts/README.md b/scripts/README.md index 0826ebb..a9c7ee5 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -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 反向代理配置,例如: diff --git a/scripts/deploy_remote.py b/scripts/deploy_remote.py index dbc3317..fe21129 100644 --- a/scripts/deploy_remote.py +++ b/scripts/deploy_remote.py @@ -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'))}", ]