简书地址:
http://www.jianshu.com/p/2c25842d58d3
http://www.jianshu.com/p/2c25842d58d3
本案例是一个朋友的案例他也写了出来如下:
https://mp.weixin.qq.com/s/XSnFkuYzIlGWMaXIl-oPeQ
但是和他交流后他也准备改因为分析有一些小问题。
一、触发条件
-
<李>
binlog_gtid_simple_recovery=false。
李>
<李>
5.7.6以上版本。
李>
<李>
Gtid关闭或者Gtid中途开启有大量的未开启Gtid的binlog。
李>
二、本案例回顾
-
<李>
版本:MySQL版本5.7.19。
李>
<李>
故障为:大概每半小时发生一次故障,整个Mysql压力巨大,很多简单的操作都相应缓慢。使用iotop,最高等工具都发现Mysql某个线程有大量的I/O。
李>
<李>
分析方法:使用strace发现有大量的binlog文件读取。
李>
<李>
binlog_gtid_simple_recovery=false。
李>
<李>
Gtid关闭,中途开启,但是留下了很多未开启Gtid的binlog。
李>
<李>
数据库没有重启,但是由于expire_logs_days触发了binlog删除。
李>
三,故障分析
其实本案例就是前文第七部分总结中的:
<>以前Gtid关闭,simple_recovery=误 5.7.6以上:这种方式一定得到正确的Gtid集合 重启Mysql不扫秒全部的binlog,如果是中途打开GTID重启任然需要扫描多个binlog因为需要找到GTID事件。 清洗binlog或者超过参数expire_logs_days参数设置不触发全binlog扫描,如果是中途打开GTID重启任然需要扫描多个binlog因为需要找到GTID事件。
从案例中我们得知是中途开启的Gtid,但是留下了很多未开启Gtid的binlog,从第六部分源码bool MYSQL_BIN_LOG:: init_gtid_sets()函数的分析,我们知道删除binlog后也会触发正向查找来获取gtid_purged (Gtid_state.lost_gtids)。当读取到第一个binlog的时候虽然获取到了先前Gtid事件但是没有Gtid事件,而simple_recovery=误所以需要继续查找下一个文件,直到找到同时包含先前Gtid事件和Gtid事件的那个binlog才会停止,那么显然这种情况下那些Gtid关闭的时候生成的binlog将会全部扫描一遍,如果量大那么代价将是巨大的。
而案例中每半个小时会触发一次binlog切换,因为触发超过expire_logs_days参数设置导致binlog进行删除,触发了大量的binlog扫描。
显然有了前面的基础这个案例很容易分析。
四、案例模拟
这个案例非常好模拟。我打算直接使用strace查看。因为不是每位朋友都能方便使用GDB调试。
使用测试版本社区版本5.7.17:
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Log_name | File_size | + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | binlog。000027 | 198 | | binlog。000028 | 198 | | binlog。000029 | 198 | | binlog。000030 | 198 | | binlog。000031 | 198 | | binlog。000032 | 198 | | binlog。000033 | 198 | | binlog。000034 | 198 | | binlog。000035 | 198 | | binlog。000036 | 198 | | binlog。000037 | 198 | | binlog。000038 | 198 | | binlog。000039 | 198 | | binlog。000040 | 198 | | binlog。000041 | 198 | | binlog。000042 | 198 | | binlog。000043 | 154 | + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + mysql>显示变量如“% gtid %”; + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + | | Variable_name |值 + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + | binlog_gtid_simple_recovery | | | enforce_gtid_consistency | alt=" Mysql 5.7 Gtid内部学习(九)实际案例(一)">Mysql 5.7 Gtid内部学习(九)实际案例(一)