MySQL在线DDL工具pt-osc
2019獨角獸企業重金招聘Python工程師標準>>>
導讀:
上篇文章講過MySQL原生的Online DDL還是有很多限制的,還是會遇到data meta lock的問題等諸多不便,然后就有了我們今天的話題,通過pt-osc在線執行DDL。
?
?
一、pt-osc的工作原理
?
1、創建一個和源表一樣表結構的新表
2、在新表執行DDL語句(空表嘛,所以。。。)
3、在源表創建三個觸發器分別對應insert、update、delete操作
4、從源表拷貝數據到新表,拷貝過程中源表通過觸發器把新的DML操作更新到新表中
5、rename源表到old表中,把新表rename為源表,默認最后刪除源表
?
?
二、pt-osc工具的限制
?
1、源表不能有觸發器存在
顯然不是不能有任何觸發器,只是不能有針對insert、update、delete的觸發器存在,因為一個表上不能有兩個相同類型的觸發器
?
2、源表必須要有主鍵
源表沒有主鍵會報錯:
Cannot chunk the original table `houyi`.`ga`: There is no good index and the table is oversized. at ./pt-online-schema-change line 5353.?
3、源表有外鍵,必須使用--alter-foreign-keys-method指定特定的值
?
?
三、pt-osc與原生MySQL5.6 Online DDL對比
?
l? Online DDL在必須copy table時成本較高,不宜采用
l? Pt-osc在存在觸發器時,不適用
l? 其他情況使用pt-osc
l? 選擇在業務低峰期進行online ddl
?
?
四、pt-osc常用參數
?
--host=xxx? --user=xxx? ?--password=xxx
連接實例信息,縮寫-h xxx -u xxx -p xxx,密碼可以使用參數--ask-pass?手動輸入。
?
--alter
結構變更語句,不需要?ALTER TABLE關鍵字。與原始ddl一樣可以指定多個更改,用逗號分隔。
?
D=db_name,t=table_name
指定要ddl的數據庫名和表名
?
--dry-run
創建和修改新表,但不會創建觸發器、復制數據、和替換原表。并不真正執行,可以看到生成的執行語句,了解其執行步驟與細節,和--print配合最佳。。
?
--execute
確定修改表,則指定該參數。真正執行alter。–dry-run與–execute必須指定一個,二者相互排斥
?
?
五、pt-osc使用示例
?
1、添加新列?
[root@bogon ~]# pt-online-schema-change --user=root --password=123456 --host=localhost --alter "add column age int(4) default 0" D=test,t=test01 --print --executeNo slaves found.? See --recursion-method if host bogon has slaves.Not checking slave lag because no slaves were found and --check-slave-lag was not specified.Operation, tries, wait:analyze_table, 10, 1copy_rows, 10, 0.25create_triggers, 10, 1drop_triggers, 10, 1swap_tables, 10, 1update_foreign_keys, 10, 1Altering `test`.`test01`...Creating new table...CREATE TABLE `test`.`_test01_new` (`name` varchar(3) DEFAULT NULL,`id` varchar(4) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8Created new table test._test01_new OK.Altering new table...ALTER TABLE `test`.`_test01_new` add column age int(4) default 0Altered `test`.`_test01_new` OK.2016-11-30T02:54:32 Creating triggers...CREATE TRIGGER `pt_osc_test_test01_del` AFTER DELETE ON `test`.`test01` FOR EACH ROW DELETE IGNORE FROM `test`.`_test01_new` WHERE `test`.`_test01_new`.`id` <=> OLD.`id`CREATE TRIGGER `pt_osc_test_test01_upd` AFTER UPDATE ON `test`.`test01` FOR EACH ROW REPLACE INTO `test`.`_test01_new` (`name`, `id`) VALUES (NEW.`name`, NEW.`id`)CREATE TRIGGER `pt_osc_test_test01_ins` AFTER INSERT ON `test`.`test01` FOR EACH ROW REPLACE INTO `test`.`_test01_new` (`name`, `id`) VALUES (NEW.`name`, NEW.`id`)2016-11-30T02:54:32 Created triggers OK.2016-11-30T02:54:32 Copying approximately 4 rows...INSERT LOW_PRIORITY IGNORE INTO `test`.`_test01_new` (`name`, `id`) SELECT `name`, `id` FROM `test`.`test01` LOCK IN SHARE MODE /*pt-online-schema-change 21439 copy table*/2016-11-30T02:54:32 Copied rows OK.2016-11-30T02:54:32 Analyzing new table...2016-11-30T02:54:32 Swapping tables...RENAME TABLE `test`.`test01` TO `test`.`_test01_old`, `test`.`_test01_new` TO `test`.`test01`2016-11-30T02:54:32 Swapped original and new tables OK.2016-11-30T02:54:32 Dropping old table...DROP TABLE IF EXISTS `test`.`_test01_old`2016-11-30T02:54:32 Dropped old table `test`.`_test01_old` OK.2016-11-30T02:54:32 Dropping triggers...DROP TRIGGER IF EXISTS `test`.`pt_osc_test_test01_del`;DROP TRIGGER IF EXISTS `test`.`pt_osc_test_test01_upd`;DROP TRIGGER IF EXISTS `test`.`pt_osc_test_test01_ins`;2016-11-30T02:54:32 Dropped triggers OK.Successfully altered `test`.`test01`.?
2、修改列類型
[root@bogon ~]# pt-online-schema-change h=localhost,P=3306,u=root,p=123456,D=test,t=test01 --alter "change women age int(4) default 0" --print --execute --no-check-alter?
3、添加刪除索引
[root@bogon ~]# pt-online-schema-change h=localhost,P=3306,u=root,D=test,t=test01 --ask-pass --alter "drop key index_name,add key index_age(age)" --print --execute?
4、修改主鍵
使用pt-osc去修改刪除主鍵,務必同時添加原主鍵為 UNIQUE KEY,否則很有可能導致性能問題
[root@bogon ~]# pt-online-schema-change h=localhost,u=root,p=123456,D=test,t=test01 --alter "drop primary key,add primary key(age)" --print --execute --no-check-alter?
?
七、報錯案例
?
1、報錯語句
The tool should handle this correctly, but you should test it first because if it fails the renamed columns' data will be lost!? Specify --no-check-alter to disable this check and perform the --alter.介個直接看著報錯就可以解決了
?
2、報錯語句
The table `db_name`.`table_name` has triggers.? This tool needs to create its own triggers, so the table cannot already have triggers.存在觸發器,表不能存在觸發器
?
4、pt-osc產生死鎖
當一個事務在做DDL操作,一個事務在做DML操作,有可能會造成死鎖
?
5、pt-osc導致丟表
當在做DDL的時候,還未提交,此時如果實例crash,就會導致表丟失。
?
?
參考文檔:
https://my.oschina.net/kings0/blog/807871
http://www.cnblogs.com/zengkefu/category/854034.html
http://www.dataguru.cn/article-3460-1.html
https://www.percona.com/doc/percona-toolkit/2.1/pt-online-schema-change.html
?
為了方便大家交流,本人開通了微信公眾號,和QQ群291519319。喜歡技術的一起來交流吧
轉載于:https://my.oschina.net/u/3023401/blog/818002
總結
以上是生活随笔為你收集整理的MySQL在线DDL工具pt-osc的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS验证图片格式和大小并预览
- 下一篇: 我的开源 GitBook: Python