首页 > 代码库 > MySQL慢查询日志分割

MySQL慢查询日志分割

mysql> set global slow_query_log=0;
Query OK, 0 rows affected (0.00 sec)
 
mysql> set global slow_query_log_file=‘/data/mysql_33096/mysqllog/slow_query_2014-06-09.log‘;
Query OK, 0 rows affected (0.00 sec)
 
mysql> show variables like ‘slow%‘;
+---------------------+------------------------------------------------------+
| Variable_name | Value |
+---------------------+------------------------------------------------------+
| slow_launch_time | 2 |
| slow_query_log | OFF |
| slow_query_log_file | /data/mysql_33096/mysqllog/slow_query_2014-06-09.log |
+---------------------+------------------------------------------------------+
 
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘slow%‘;
+---------------------+-------------------------------------------+
| Variable_name | Value |
+---------------------+-------------------------------------------+
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | /data/mysql_33096/mysqllog/slow_query.log |
+---------------------+-------------------------------------------+
3 rows in set (0.00 sec)
 

/root/slowlog.sh脚本
 
#!/bin/bash
####split mysql slow-query-log#######
LOGDIR=/data/mysqllog
DATE=`date +"%Y-%m-%d"`
user=xxx
passwd=****
#stop slow_query_log
mysql -u$user -p$passwd -e "set global slow_query_log=0"
#reset the filename of slow_query_log
mysql -u$user -p$passwd -e "set global slow_query_log_file=‘${LOGDIR}/slow_query_${DATE}.log‘"
#enable slow_query_log
mysql -u$user -p$passwd -e "set global slow_query_log=1"
#remove slow_query_log 7days ago.
cd $LOGDIR
find ./slow_query* -ctime +7 -exec rm -f {} \;

定时任务:
 
[wy@dev88 home]$ sudo crontab -e
00 0 * * * /root/slowlog.sh

MySQL慢查询日志分割