pt-online-schema-change是percona公司開發的一個工具,在percona-toolkit包裏面能夠找到這個功能,它能夠在線修改表結構html
原理:mysql
使用pt-online-schema-change執行SQL的日誌
SQL語句:
ALTER TABLE tmp_task_user
ADD support tinyint(1) unsigned NOT NULL DEFAULT '1';sql
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`.
好處:bash
建議:服務器
1.去官網下載對應的版本,官網下載地址:https://www.percona.com/downl...app
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
1.參數
./bin/pt-online-schema-change --help 能夠查看參數的使用,咱們只是要修改個表結構,只須要知道幾個簡單的參數就能夠了阿里雲
--user= 鏈接mysql的用戶名 --password= 鏈接mysql的密碼 --host= 鏈接mysql的地址 P=3306 鏈接mysql的端口號 D= 鏈接mysql的庫名 t= 鏈接mysql的表名 --alter 修改表結構的語句 --execute 執行修改表結構 --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 --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)"
1.官方參考:https://www.percona.com/doc/p...
2.官方下載:https://www.percona.com/downl...