使用 gh-ost 进行无触发器的在线结构变更
MySQL在线操作命令
# Connects to a replica and reads its binlog; postpones cut-over until the flag file is removed
gh-ost --host=db2 --port=3306 --user=ghost --password='Gh0st#2024' \
--database=shop --table=orders \
--alter="ADD COLUMN channel VARCHAR(20) NULL, ADD INDEX ix_channel (channel)" \
--max-load=Threads_running=50 --critical-load=Threads_running=200 --max-lag-millis=1500 \
--exact-rowcount --concurrent-rowcount --default-retries=120 \
--postpone-cut-over-flag-file=/tmp/ghost.postpone --panic-flag-file=/tmp/ghost.panic \
--serve-socket-file=/tmp/gh-ost.shop.orders.sock --ssl --ssl-allow-insecure --execute
# While running: status / throttle / cut-over
echo status | nc -U /tmp/gh-ost.shop.orders.sock
echo throttle | nc -U /tmp/gh-ost.shop.orders.sock
rm /tmp/ghost.postpone作用
构建 ghost 表并复制行;通过读取 binlog 而不是触发器来应用并发变更。限流、暂停和切换时机可在运行时通过 socket 或标志文件控制。
适用场景
用于大型写入密集的表,触发器开销不可接受,且希望自行选择切换时机(例如低流量窗口)时。
注意
需要 binlog_format=ROW 和 binlog_row_image=FULL;所连接的副本需要 log_replica_updates=ON。用户需要 SUPER 或 REPLICATION CLIENT / REPLICATION SLAVE,以及该表上的 ALL 权限。不支持有外键(父表或子表)的表。切换后旧表保留为 _<table>_del;验证后请手动删除(--ok-to-drop-table 会自动删除)。1.1.8 之前的 gh-ost 版本使用 SHOW SLAVE STATUS,在 MySQL 8.4 上会以错误 1064 停止;8.4 支持在 1.1.8 中加入。gh-ost 会自行创建 --postpone-cut-over-flag-file 中指定的文件;在删除该文件之前不会进行切换。