1、MySQL开启慢查询日志
slow_query_log=ON
slow_query_log_file=/var/log/mysql/slow_query.log #新建慢查询文件,并指定文件所有者mysql
long_query_time=2
2、创建目录
mkdir -p /var/log/mysql/slow_query
chown -R mysql:mysql /var/log/mysql/slow_query
3、按天分割脚本
#!/bin/bash
log_name=$(date +%Y%m%d)
user=root
passwd=123456
#新建 slow_query 目录,然后把当前慢查询文件,重命名移动到此目录
mv /var/log/mysql/slow_query.log /var/log/mysql/slow_query/$log_name.log
#使用 flush-logs slow 命令 重新生成慢查询日志
mysqladmin -u$user -p$passwd flush-logs slow 2>/dev/null
mysqladmin flush-logs 命令可以生成新的日志,这里只需要生成 slow 慢查询日志
mysqladmin flush-logs slow
flush-logs [log_type ...]
Flush all logs.
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 bespecified for the FLUSH LOGS SQL statement.