在線修改大表結構之pt-online-schema-change

安裝

sudo yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
sudo yum install percona-toolkit

查看觸發器:

方式一:

use information_schema;
select * from TRIGGERS;

方式二:

use database_name;
show triggers;

刪除觸發器:

drop trigger trigger_name

有觸發器pt-online-schema-change將不工作
會出現如下提示:The table fr.xaxa has triggers. This tool needs to create its own triggers, so the table cannot already have triggers.

基本原理:

  1. 使用用戶密碼登錄
  2. 然後檢查表的創建語句show create table table_name進行臨時表的創建,
  3. 根據l--alter參數對臨時表進行結構更改
  4. 創建觸發器,在更新、刪除、插入的時候對臨時表的修改
  5. 從舊錶向臨時表中拷貝數據
  6. 修改舊錶名字, 修改臨時表名字
  7. 刪除舊錶,刪除觸發器

使用實例:爲表加唯一索引, 添加列, 刪除列

time pt-online-schema-change --user=kratos --password=3vUbY52IJ2fJq7KwWPeItNrz8 --host=127.0.0.1 --port=3306  --alter="add unique index tel_name(tel, name)" D=test,t=tb_user --nocheck-replication-filters --nocheck-unique-key-change --max-lag=5  --check-interval=2 --alter-foreign-keys-method=auto --execute --print
# 添加列 --alter="add column address varchar(50) not null default ''"
# 刪除列 --alter="drop column address"

不同的操作就是改一下--alter裏面的內容

問題:

主從複製 --max-lag=5 --check-interval=2 (設定延遲) 這裏使用默認也可以
外鍵約束 --alter-foreign-keys-method=auto
磁盤空間(1倍)
–no-drop-old-table 不刪除原來的表

性能:
–critical-load=“Threads_running=200”

特別參數:
–nocheck-unique-key-change
–nocheck-replication-filters

測試過程:
-–dry-run --print

使用:
–execute

權威 文檔

查看主從狀態:

SHOW SLAVE STATUS \G
SHOW status;

擴展:類似工具gh-ost

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章