MySQL PT-Online工具安装使用

在生产环境下mysql执行 ddl为不影响线上正常使用,经常使用工具pt-online

安装pt-online

  • 安装DBI

    wget http://www.cpan.org/modules/by-module/DBD/DBI-1.634.tar.gztar -xf DBI-1.634.tar.gzcd DBI-1.634/
    perl Makefile.PLmake && sudo make install
  • 安装DB-MySQL

    wget http://www.cpan.org/modules/by-module/DBD/DBD-mysql-4.027.tar.gz
    tar -xf DBD-mysql-4.027.tar.gz
    cd DBD-mysql-4.027/
    perl Makefile.PL --mysql_config=/export/servers/mysql/bin/mysql_config --with-mysql=/export/servers/mysql  make && sudo make install 
    
  • 安装percona-toolkit

    wget https://www.percona.com/downloads/percona-toolkit/3.0.8/binary/tarball/percona-toolkit-3.0.8_x86_64.tar.gztar -xf percona-toolkit-3.0.8_x86_64.tar.gzcd percona-toolkit-3.0.8perl Makefile.PL make && sudo make install 
    

简单使用pt-online工具

修改表的ddl语句

pt-online-schema-change --alter "add address varchar(255) not NULL default ''" D=test,t='test_type' --execute --print --statistics --no-check-alter --user root --password secret --host 127.0.0.1 --port 3358

pt-online 工具使用过程解析:

  • 创建新表

CREATE TABLE `test`.`_test_type_new` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12970 DEFAULT CHARSET=utf8
  • 新表增加列

ALTER TABLE `test`.`_test_type_new` add address varchar(255) not NULL default ''
  • 对老表增加delete触发器

CREATE DEFINER=`root`@`%` TRIGGER `pt_osc_test_test_type_del` AFTER DELETE ON `test`.`test_type` FOR EACH ROW DELETE IGNORE FROM `test`.`_test_type_new` WHERE `test`.`_test_type_new`.`id` <=> OLD.`id`
  • 对老表增加update触发器

CREATE DEFINER=`root`@`%` TRIGGER `pt_osc_test_test_type_upd` AFTER UPDATE ON `test`.`test_type` FOR EACH ROW BEGIN DELETE IGNORE FROM `test`.`_test_type_new` WHERE !(OLD.`id` <=> NEW.`id`) AND `test`.`_test_type_new`.`id` <=> OLD.`id`;REPLACE INTO `test`.`_test_type_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`);END
  • 对老表增加insert触发器

CREATE DEFINER=`root`@`%` TRIGGER `pt_osc_test_test_type_ins` AFTER INSERT ON `test`.`test_type` FOR EACH ROW REPLACE INTO `test`.`_test_type_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`)
  • analyze 新表
    作用是分析关键字分布: 因为是批量写入数据,所以需要analyze 关键字

ANALYZE TABLE `test`.`_test_type_new` /* pt-online-schema-change *
  • rename 表

RENAME TABLE `test`.`test_type` TO `test`.`_test_type_old`, `test`.`_test_type_new` TO `test`.`test_type`
  • 扫尾工作

## 删除老表 DROP TABLE IF EXISTS `_test_type_old` /* generated by server */## 删除触发器  DROP TRIGGER IF EXISTS `test`.`pt_osc_test_test_type_del`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_test_type_upd`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_test_type_ins`

总结

以上就是pt-online 在线ddl 增加列原理

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值