某日凌晨收到db故障告警,上机器检查,mysql已经被myqld守护进程重新启动。检查/var/log/messages和最近的dmesg发现mysql进程是被
某日凌晨收到db故障告警,上机器检查,mysql已经被myqld守护进程重新启动。检查/var/log/messages和最近的dmesg发现mysql进程是被oom干掉了。
信息大概摘录如下:
[13045702.638964] kthread invoked oom-killer: gfp_mask=0xd0, order=1, oomkilladj=0
[13045702.638969]
[13045702.638969] call trace: {oom_kill_process+87}
[13045702.638977] {out_of_memory+271} {autoremove_wake_function+0}
error: fatal error found, match error-keyword 'out_of_memory'
...
[13045702.716335] out of memory: kill process 25795 (mysqld) score 1591671 and children.
error: fatal error found, match error-keyword 'out of memory'
[13045702.716359] out of memory: killed process 25795 (mysqld).
.....
[13045702.802080] out of memory: kill process 1907 (mysqld) score 955002 and children.
error: fatal error found, match error-keyword 'out of memory'
[13045702.802102] out of memory: killed process 1907 (mysqld).
....
[13045762.203463] out of memory: kill process 24544 (mysqld) score 341071 and children.
error: fatal error found, match error-keyword 'out of memory'
[13045762.203485] out of memory: killed process 24544 (mysqld).
[13045762.322333] sap1002 invoked oom-killer: gfp_mask=0x201d2, order=0, oomkilladj=0
.....
[13045762.479886] out of memory: kill process 19530 (mysqldump) score 93607 and children.
error: fatal error found, match error-keyword 'out of memory'
[13045762.479907] out of memory: killed process 19530 (mysqldump).
由于内存不足,系统选择性的选择了耗用内存大的几个进程kill掉了。先kill了mysqld,最后才把mysqldump kill掉,这样有理由怀疑可能是因为mysqldump耗用了大量的内存导致的。
在这个db上,每天凌晨会使用mysqldump做单表备份并通过管道进行压缩。这个备份任务已经部署了超过1年多了。
查看了后台采集的os性能数据,也可以发现在mysqldump启动直到备份结束,内存耗用会上升比较多,这样可以肯定是因为mysqldump耗用大量内存导致此次故障发生。
check备份使用的脚本,,备份的逻辑大概如下:
1:show tables like 'xxx%' 获取需要备份的表名
2:mysqldump --uxx -pyy --skip-opt >>
至此问题就比较清晰了,mysqldump由于未使用-q参数导致耗用内存过大而导致oom现象发生。
对mysqldump -q参数的解释(取自man mysqldump)
? --quick, -q
this option is useful for dumping large tables. it forces mysqldump to retrieve rows for a table from the server a row at a time rather than retrieving the entire row set and buffering it in memory before writing it out.
这个选项被用来dump比较大的表。它强制mysqldump从服务器一行一行的获取数据而不是把获取所有行的数据在输出之前把它缓存到内存中。
以下是一个测试,可以看到-q 参数对内存耗用的影响:
1:不带-q参数
top输出如下:
pid user pr ni virt res shr s %cpu %mem time+ command
20010 mysql 15 0 18.0g 17g 4496 s 10 56.6 2765:51 mysqld
27518 mysql 25 0 4227m 4.1g 1048 r 100 13.1 0:33.05 mysqldump
内存耗用超过4g
2:带-q参数
top输出如下:
pid user pr ni virt res shr s %cpu %mem time+ command
20010 mysql 16 0 18.0g 17g 4496 s 84 56.6 2766:12 mysqld
27686 mysql 25 0 11628 1380 1052 r 98 0.0 0:23.20 mysqldump
内存耗用很小,只有几k
应对方案:
修改备份脚本,加上-q参数。
到此还没完,这个备份任务已经部署了超过1年了,为啥到最近才出现此故障呢?
对db内的数据做了个统计,需要备份表从之前100m/day的数据量突然爆增到4g/day。知会研发跟进问题,最后发现是前端配置更改导致前端不断重试导致。
体会:
在很多时候,故障的发生是很多因素交织在一起才发生的。碰到问题需要更深一层考虑去探寻问题发生的根本原因。
相关阅读:
linux下通过mysqldump备份mysql数据库成sql文件
linux中使用mysqldump对mysql数据库进行定时备份