案例——percona-online-schema-change各种坑

  

线上环境复制使用连续模式,对于上亿的表,使用pt> <李>

——bin-log

<李>

允许二进制日志(,)。默认二进制日志是关闭,因为在大多数情况下,——tmp-table 不需要复制只


而在pt工具2.2版本以后,会默认打binlog,好处是在不用分别在各个节点执行一次改表操作,只需要在主库执行一次改表,就会通过binlog让下面的从库的表都被修改


pt工具3.0版本,有一个,——set-vars=皊ql_log_bin=0”参数能替代——bin-log=0效果


有一个1.5亿表加索引的需求,预计1.5亿生成的binlog预计会有20克,为了不产生binlog,准备在每个点执行一次,先在主库执行如下命令

pt-online-schema-change ,,,,,,,,,,,,,,,,,,,,,,,,,,,,   ——主机=主机,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,   ——港口=端口号,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,   ——用户=节点号,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,   ——数据库=数据库名,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,   t=t_room_impeach ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,   ——改变=" ADD  INDEX  idx_psr (A, B, C)”,,   ——set-vars=' sql_log_bin=0 ',,,,,,,,,,,,,,,,,,,,,,,,,,,   ——执行

这条语句一下去,主库下面的4个从库同步都中断了,显示奴隶状态报错

Last_SQL_Errno: 1146   Last_SQL_Error: Error  executing  row 事件:,“Table ”live_oss._t_room_impeach_new”,并't 存在“

报错_t_room_impeach_new表存在,为什么这张临时表在从库要存在呢?


posc工具的原理是,先创建一个临时表,表名是_原来的表名的_new,这张临时表是已经加入了你想要的索引,不停把旧表的数据拷贝到这张临时表,新插入,修改,删除的旧表的数据,都会根据触发器,同样新插入,修改,删除到临时表,等拷贝数据,旧表和临时表就是一模一样了,这个时候把临时表重命名成为就表的名字,而实际的旧表就会被删除掉,在线完成


当主库执行命令是会显示创建临时表,创建触发器

Creating  new 表…   Created  new  table  live_oss._t_room_impeach_new 好吧。   Altering  new 表……   Altered ‘live_oss’。‘__t_room_impeach_new’,好吧。   2017 - 08 - 02 t16:38:48  Creating 触发……   2017 - 08 - 02 t16:38:48  Created  triggers 好吧。   2017 - 08 - 02 t16:38:48  Copying  approximately  141559863,行……

因为——set-vars=皊ql_log_bin=0”的原因,创建表的DDL语句,无法通过binlog在从库建表,所以从库是表不存在的,问题是从库不需要存在临时表啊,因为只操作主库一个点就足够了



这时解决方法是在从库,去建立同样一张临时表_xxxx_new,好让触发器的数据,能够顺利插入到这张表,当建了以后可以看到从库的临时表有数据了,再次验证sql_log_bin=0没有效果

explain  select  count(*),得到,__t_room_impeach_new;   + - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - +   | |,id  select_type  |, table ,,,,,,,,,,,,,,, |, type , |, possible_keys  |, key , |, key_len  |, ref , |, rows ,,,, |, Extra ,,,,, |   + - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - +   |,,1,|,SIMPLE ,,,,, |, __t_room_impeach_new  |, index  |, NULL ,,,,,,,,, |, uid , |, 4,,,,,,, |, NULL  |, 176 |, Using  index  |   + - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - +

几个从库都有176条数据,再看看主库的临时表,有差不多1亿数据,因为除了触发器还有来自旧表的


explain  select  count(*),得到,__t_room_impeach_new;   + - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - +   null   null   null   null   null   null   null   null   null   null   null   null

案例——percona-online-schema-change各种坑