1. 程式人生 > 其它 >RDS for MySQL之pt-online-schema-change工具使用

RDS for MySQL之pt-online-schema-change工具使用

##工具介紹:
pt-online-schema-change是percona toolkit工具箱功能之一,主要是在避免鎖表的情況下做增加/刪除欄位等DDL操作。
大概原理是新建一個表,並且這個表已經是最新的表結構,
從舊錶往新表insert資料,同時會在舊錶上建立增刪改三個觸發器,舊錶上所有增刪改操作會插入到新表,最後新表舊錶做換名操作。

【注意事項】
**要操作的表必須有主鍵**
**會有一定概率發生死鎖,如一直有死鎖發生建議使用Gh-ost軟體**

##1、安裝
參考官方文件
https://www.percona.com/downloads/percona-toolkit/LATEST/

##2、使用
pt-online-schema-change --help
可以檢視到該命令的引數列表

先介紹幾個常用引數
h=:指定主機名
P=:指定主機埠
u=:指定資料庫使用者名稱
p=:指定資料庫密碼
--execute:確定修改表
--alter:要執行的語句
--no-version-check:不檢查版本資訊

--recursion-method:Mater尋找Slave的方法,在RDS中使用需要設定為none
著重介紹一下這個引數,因為pt-online-schema-change這個工具預設會檢測主從延遲,而RDS使用者看到的都是邏輯IP,PT工具通過show slave status;獲取到Slave的IP,這個IP是物理機IP,所以PT工具連線這個IP網路會不通,導致使用PT工具一直卡住很長時間;設定為none,不檢測主從延遲。

##3、示例
###建立測試環境:
cretae database ceshi;
use ceshi;
create table (t1 id int primary key);

###執行新增欄位name列
pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "add column name varchar(200)" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1


###輸出日誌如下:
No slaves found. See --recursion-method if host a48c13173.cloud.na62 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 `ceshi`.`t1`...
Creating new table...
Created new table ceshi._t1_new OK.
Altering new table...
Altered `ceshi`.`_t1_new` OK.
2019-09-15T09:57:30 Creating triggers...
2019-09-15T09:57:31 Created triggers OK.
2019-09-15T09:57:31 Copying approximately 1 rows...
2019-09-15T09:57:31 Copied rows OK.
2019-09-15T09:57:31 Analyzing new table...
2019-09-15T09:57:31 Swapping tables...
2019-09-15T09:57:31 Swapped original and new tables OK.
2019-09-15T09:57:31 Dropping old table...
2019-09-15T09:57:31 Dropped old table `ceshi`.`_t1_old` OK.
2019-09-15T09:57:31 Dropping triggers...
2019-09-15T09:57:31 Dropped triggers OK.
Successfully altered `ceshi`.`t1`.

###Successfully 結尾代表執行成功。

##4、常用指南
1、新增欄位:
pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "add column name varchar(200)" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

2、刪除欄位:
pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "drop column name" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

3、修改欄位型別
pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "modify name varchar(300)" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

4、建立索引
pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "add key idx_name (name)" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

5、刪除索引
pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "drop key idx_name" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

6、碎片整理,釋放空間
pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "engine=innodb" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

##總結
除了--alter部分內容是變動的,其它引數內容都是固定的;