一、pt-online介紹
pt-online-schema-change是percona公司開發的一個工具,在percona-toolkit包里面可以找到這個功能,它可以在線修改表結構
原理:
-
首先它會新建一張一模一樣的表,表名一般是_new后綴
-
-
然后在原表上加三個觸發器,DELETE/UPDATE/INSERT,將原表中要執行的語句也在新表中執行
-
最后將原表的數據拷貝到新表中,然后替換掉原表
使用pt-online-schema-change執行SQL的日志 SQL語句:
ALTER TABLE `tmp_task_user` ADD support tinyint(1) unsigned NOT NULL DEFAULT '1'; sh pt.sh tmp_task_user "ADD COLUMN support tinyint(1) unsigned NOT NULL DEFAULT '1'"
日志輸出:
tmp_task_user ADD COLUMN support tinyint(1) unsigned NOT NULL DEFAULT '1' No slaves found. See --recursion-method if host h=127.0.0.1,P=3306 has slaves. Not checking slave lag because no slaves were found and --check-slave-lag was not specified. Operation, tries, wait: analyze_table, 10, 1 copy_rows, 10, 0.25 create_triggers, 10, 1 drop_triggers, 10, 1 swap_tables, 10, 1 update_foreign_keys, 10, 1 Altering `test_db`.`tmp_task_user`... Creating new table... Created new table test_db._tmp_task_user_new OK. Altering new table... Altered `test_db`.`_tmp_task_user_new` OK. 2018-05-14T18:14:21 Creating triggers... 2018-05-14T18:14:21 Created triggers OK. 2018-05-14T18:14:21 Copying approximately 6 rows... 2018-05-14T18:14:21 Copied rows OK. 2018-05-14T18:14:21 Analyzing new table... 2018-05-14T18:14:21 Swapping tables... 2018-05-14T18:14:21 Swapped original and new tables OK. 2018-05-14T18:14:21 Dropping old table... 2018-05-14T18:14:21 Dropped old table `test_db`.`_tmp_task_user_old` OK. 2018-05-14T18:14:21 Dropping triggers... 2018-05-14T18:14:21 Dropped triggers OK. Successfully altered `test_db`.`tmp_task_user`.
好處:
-
降低主從延時的風險
-
可以限速、限資源,避免操作時MySQL負載過高
建議:
-
在業務低峰期做,將影響降到最低
二、pt-online安裝
1.去官網下載對應的版本,官網下載地址:https://www.percona.com/downl...
2.下載解壓之后就可以看到pt-online-schema-change
3.該工具需要一些依賴包,直接執行不成功時一般會有提示,這里可以提前yum安裝
yum install perl-DBI yum install perl-DBD-MySQL yum install perl-Time-HiRes yum install perl-IO-Socket-SSL
三、pt-online-schema-change使用
1.參數 ./bin/pt-online-schema-change --help 可以查看參數的使用,我們只是要修改個表結構,只需要知道幾個簡單的參數就可以了
--user= 連接mysql的用戶名
--password= 連接mysql的密碼
--host= 連接mysql的地址
P=3306 連接mysql的端口號
D= 連接mysql的庫名
t= 連接mysql的表名
--alter 修改表結構的語句
--execute 執行修改表結構
--charset=utf8 使用utf8編碼,避免中文亂碼
--no-version-check 不檢查版本,在阿里雲服務器中一般加入此參數,否則會報錯
2.為避免每次都要輸入一堆參數,寫個腳本復用一下,pt.sh
#!/bin/bash table=$1 alter_conment=$2 cnn_host='127.0.0.1' cnn_user='user' cnn_pwd='password' cnn_db='database_name' echo "$table" echo "$alter_conment" /root/percona-toolkit-2.2.19/bin/pt-online-schema-change --charset=utf8 --no-version-check --user=${cnn_user} --password=${cnn_pwd} --host=${cnn_host} P=3306,D=${cnn_db},t=$table --alter "${alter_conment}" --execute
3.添加表字段 如添加表字段SQL語句為:
ALTER TABLE `tb_test` ADD COLUMN `column1` tinyint(4) DEFAULT NULL;
那么使用pt-online-schema-change則可以這樣寫
sh pt.sh tb_test "ADD COLUMN column1 tinyint(4) DEFAULT NULL"
4.修改表字段 SQL語句:
ALTER TABLE `tb_test` MODIFY COLUMN `num` int(11) unsigned NOT NULL DEFAULT '0';
pt-online-schema-change工具:
sh pt.sh tb_test "MODIFY COLUMN num int(11) unsigned NOT NULL DEFAULT '0'"
5.修改表字段名 SQL語句:
ALTER TABLE `tb_test` CHANGE COLUMN age adress varchar(30);
pt-online-schema-change工具:
sh pt.sh tb_test "CHANGE COLUMN age address varchar(30)"
6.添加索引 SQL語句:
ALTER TABLE `tb_test` ADD INDEX idx_address(address);
pt-online-schema-change工具:
sh pt.sh tb_test "ADD INDEX idx_address(address)"
四、其他
-
pt-online-schema-change工具還有很多其他的參數,可以有很多限制,比如限制CPU、線程數量、從庫狀態等等,不過我做過一個超過6000W表的結構修改,發現幾乎不影響性能,很穩定很流暢的就修改了表結構,所以,對以上常規參數的使用基本能滿足業務
-
一定要在業務低峰期做,這樣才能確保萬無一失