DBAtlas · データベース運用アトラス

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

概要

ゴースト表を作成して行をコピーする。同時の変更は、トリガーではなくbinlogを読み取って適用する。スロットリング、一時停止、カットオーバーのタイミングは、ソケットまたはフラグファイルを介して実行時に制御する。

使用する場面

トリガーのオーバーヘッドが許容できない、書き込みの多い大きな表で、カットオーバーのタイミング(例: トラフィックの少ない時間帯)を自分で選びたいときに。

注意

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で指定したファイルを自ら作成し、そのファイルが削除されるまでカットオーバーは行われない。

DBAtlas で開く →

関連項目