Separate Mysql Slow Log Using Logrotate In Ubuntu

Server environment

1
2
3
4
$ cat /etc/issue
Ubuntu 16.04.2 LTS
$ logrotate --version
logrotate 3.8.7

Logrotate is the default Linux system installation tool

Create logrotate config file

Create file /etc/logrotate.d/3306_error.conf

1
$ vim /etc/logrotate.d/3306_error
1
2
3
4
5
6
7
8
9
10
11
12
13
/var/log/mysql/error.log {  # log absolute path 
monthly # cut once a month
rotate 13 # save 13 times then rotate, the thirteenth will cover the first file
dateext # log file named as "origin file name + 20170821"
compress # using gzip to compress
delaycompress # the last file without compress (easy to analyze)
missingok # during log rotation, any errors will be ignored,such as "File no found"
notifempty # it will not be split if no new log after last split

postrotate # command will be executed between label 'postrotate' and 'endscript'
mysql -h127.0.0.1 -uroot -puman73 --login-path=3306 -e 'flush error logs;' # mysql flush the error logs
endscript
}

Create file /etc/logrotate.d/3306_slow

1
$ vim /etc/logrotate.d/3306_slow
1
2
3
4
5
6
7
8
9
10
11
12
13
/var/log/mysql/slow-queries.log {     # log absolute path
daily # cut once a month
rotate 13 # save 13 times then rotate, the thirteenth will cover the first file
dateext # log file named as "origin file name + 20170821"
compress # using gzip to compress
delaycompress # the last file without compress (easy to analyze)
missingok # during log rotation, any errors will be ignored,such as "File no found"
notifempty # it will not be split if no new log after last split

postrotate # command will be executed between label 'postrotate' and 'endscript'
mysql -h127.0.0.1 -uroot -puman73 --login-path=3306 -e 'flush slow logs;'
endscript
}

Manual cutting the log

logrotate -f /etc/logrotate.d/3306_error

Check result

1
2
3
$ ls -lh /var/log/mysql/error.log*
-rw-r----- 1 mysql adm 0 Aug 21 21:06 /var/log/mysql/error.log
-rw-r----- 1 mysql adm 107K Aug 21 21:05 /var/log/mysql/error.log-20171227

Thank you for reading.
This post is copyrighted by Liyuliang’s Blog.
If reproduced, please indicate the source: Liyuliang’s Blog
This blog uses Creative Commons Attribution-NonCommercial-Share-Sharing 4.0 International License Agreement to license.


Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×