问题描述:
mysql开启慢查询功能,再正常不过,那么存在这样一种情况:慢查询写入的文件位置和文件名是指定好的,如果慢查询时间设定严苛,不出意外,记录慢查询的单个文件大小会日益增大,几十兆或者上百兆,带来的问题也就是慢查询日志分析处理不够方便。
解决思路:
切割日志,再通过定时任务(crontab)按规定时间切分,一般按“天”切分。
测试环境:
mysql5.6和mysql5.7
实现方法:
方法一:通过mysqladmin flush-logs [log_type] (注:mysql5.6无法指定日志类型,见测试过程)
方法二:通定时任务每次去设定慢查询路径实现
说明:以下测试过程是方法一的测试,最终实现脚本见页底。
测试过程:
1. 截取mysql5.7官方文档中的mysqladm部分说明
flush-logs [log_type ...]Flushalllogs.
The mysqladmin flush-logs command permits optional log types to be given, to specify which logs to flush. Following the flush-logs command, you can provide a space-separated list of one or more of the following log types: binary, engine, error, general, relay, slow. These correspond to the log types that can be specified for the FLUSH LOGS SQL statement.
注意:“[log_type ...]” 这个附加参数仅适用于mysql5.7.x或以上版本,也就是说,mysql5.6可以使用mysqladm flush-logs命令,但不能指定刷新某种指定日志类型,这很重要,因为这样意味着mysql5.6执行该命令,就刷新了所有类型的日志,所以mysql5.6根据具体情况选择使用。
2. 确定一下mysql版本和对应的数据存放目录(开起了binLog和慢查询)
说明:以下只显示二进制日志和慢查询日志文件详情,留意文件时间变化
[root@server-10 ~]# mysql -V
mysql Ver14.14 Distrib 5.7.23, forLinux (x86_64) using EditLine wrapper[root