3.0 KiB
3.0 KiB
以下示例展示 Docker 运行方式下各命令的完整用法。假设宿主机当前工作目录为升级项目目录。
# -------------------------------------------------------
# 定义镜像别名(可选,减少输入量)
# -------------------------------------------------------
alias db-operator='docker run --rm -it \
--network host \
--user $(id -u):$(id -g) \
-v $(pwd):/data \
-e DB_OPERATOR_PASSWORD \
harbor.wdd.io:8033/rmdc/db-operator:v1.2.0'
# 执行数据库密码输入
export DB_OPERATOR_PASSWORD=XXXXXX
# -------------------------------------------------------
# ① 导出现场数据库结构基线(多个库按顺序分别导出)
# -------------------------------------------------------
db-operator export-structure \
-H 192.168.1.100 -P 3306 -u root \
--databases mydb_core,mydb_order \
--output-dir ./export/
# 导出结果示例(文件名带 001/002 序号前缀,与 --databases 顺序一致):
# ./export/001_mydb_core_structure_<timestamp>.sql
# ./export/002_mydb_order_structure_<timestamp>.sql
# -------------------------------------------------------
# ② 导入结构基线到临时 MySQL(目录模式)
# -------------------------------------------------------
db-operator import-structure \
-H 127.0.0.1 -P 3306 -u root \
--input ./export/
# 目录模式会按文件名自然序执行(001 -> 002 -> ...)
# -------------------------------------------------------
# ②-补充:单文件导入(仍保留)
# -------------------------------------------------------
db-operator import-structure \
-H 127.0.0.1 -P 3306 -u root \
--input ./export/001_mydb_core_structure_<timestamp>.sql
# -------------------------------------------------------
# ③-a 重置数据库(--database 必须与 baseline SQL 内数据库名一致)
# -------------------------------------------------------
db-operator reset-database \
-H 127.0.0.1 -P 3306 -u root -d mydb_core \
--baseline ./export/001_mydb_core_structure_<timestamp>.sql \
--yes
# -------------------------------------------------------
# ③-b 执行增量升级
# -------------------------------------------------------
db-operator run-upgrade \
-H 127.0.0.1 -P 3306 -u root -d mydb_core \
--migration-dir ./migrations/
# -------------------------------------------------------
# ③-c 验证升级
# -------------------------------------------------------
db-operator verify-upgrade \
-H 127.0.0.1 -P 3306 -u root -d mydb_core \
--baseline ./export/001_mydb_core_structure_<timestamp>.sql
# -------------------------------------------------------
# ④ 比对差异(纯文件操作,无需 --network host)
# -------------------------------------------------------
db-operator diff-schema \
--source ./export/001_mydb_core_structure_before.sql \
--target ./export/001_mydb_core_structure_after.sql
# -------------------------------------------------------
# ⑤ 打包交付物
# -------------------------------------------------------
db-operator pack \
--project myproject --version 2.3.1