pt-online-schema-change

pt-online-schema-change是percona提供的一个mysql在线修改表结构(执行ddl)的工具

一、 原理

  • 建立一个与需要操作的表相同表结构的空表
  • 给空表执行表结构修改
  • 在原表上增加delete/update/insert的after trigger(触发器比较重,算是其缺点之一)
  • copy数据到新表,拷贝过程中通过原表上的触发器在原表进行的写操作都会更新到新建的临时表
  • Rename 原表到old表中,在把临时表Rename为原表
  • 如果有参考该表的外键,根据alter-foreign-keys-method参数的值,检测外键相关的表,做相应设置的处理
  • 删除原表上的触发器
  • 默认最后将旧原表删除

二、 限制条件

  • pt-online-schema-change操作的表需要有主键或者是唯一性索引
  • 当修改的表中主键是其他表的外键时,默认情况下会失败,需要使用--alter-foreign-keys-method选项来处理
  • 表不能有trigger;
  • 在使用之前需要对磁盘容量进行评估,使用OSC会使增加一倍的空间
  • 对数据库的CPU和IOPS会产生一定的影响,请选择的业务低峰期执行

三、 常用参数

  • --alter 修改的内容,不支持 rename表重命名。    
  • --alter-foreign-keys-method  :auto 自动选择 rebuild_constraints 或者 drop_swap,优先选择rebuild_constraints
  • --no-drop-old-table 确认后再手动删除
  • --ask-pass
  • --execute 只有加了这个这条命令才会真正的执行
  • --max-load   --max-load='Threads_running=100,Threads_connected=500'
  • --critical-load 
  • --chunk-time 默认0.5s,即拷贝数据行的时候,为了尽量保证0.5s内拷完一个chunk,动态调整chunk-size的大小,以适应服务器性能的变化
  • --chunk-size 禁止动态调整,如果指定则默认1000行
  • --max-lag=5  每个chunk拷贝完成后,会查看所有复制Slave的延迟情况(Seconds_Behind_Master)。要是延迟大于该值,则暂停复制数据,直到所有从的滞后小于这个值
  • --check-interval=2
  • --check-slave-lag  指定主从复制延迟大于选项'--max-lag'指定的值之后暂停检查校验操作。默认情况下,工具会检查所有的从库,但该选项只作用于指定的从库(通过DSN连接方式)。
  • --recurse=i   
  • --recursion-method ;processlist host
  • --no-check-replication-filters
  • --dry-run  和--print配合最佳
  • --set-vars 如设置sql_log_bin=0,一般不设置
  • --no-version-check 阿里云RDS必须添加此参数
  • D 数据库名
  • t 表名

四、 使用案例

  • 添加列
pt-online-schema-change --alter 'add column c1 int' h=localhost,u=root, P=3306,D=test,t=user --ask-pass --no-drop-old-table  --execute 
  • 删除列
pt-online-schema-change --alter 'drop column c1'  h=localhost,u=root,P=3306,D=test,t=user --ask-pass --no-drop-old-table  --execute
  • 添加索引
pt-online-schema-change --alter 'add index idx_ename(ename)' h=localhost,u=root,P=3306,D=test,t=test --ask-pass  --no-drop-old-table  --execute
  • 删除索引
pt-online-schema-change --alter 'drop index idx_ename' h=localhost,u=root,P=3306,D=test,t=test  --ask-pass --no-drop-old-table  --execute
  • 修改字段长度
pt-online-schema-change --alter 'modify column remark text ' h=localhost,u=root,P=3306,D=test,t=test  --ask-pass  --no-drop-old-table  --execute
  • 阿里云RDS,重建表
pt-online-schema-change -hIP -uroot --no-version-check --no-drop-old-table  --alter='engine=innodb' D=test,t=test --ask-pass --critical-load='Threads_running=400"   --execute
  • AWS RDS,修改自增ID为bigint
pt-online-schema-change -hIP -uroot --no-drop-old-table  --alter='modify id bigint not null auto_increment' D=test,t=test --ask-pass --critical-load='Threads_running=400"   --execute

参考
https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html
https://developer.aliyun.com/article/73969
https://www.cnblogs.com/dbabd/p/10605629.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值