Use the logrotate tool to cut MySQL logs and send slow log analysis to mailboxes
1. Install logrotate and percona-toolkit
Yum install logrotate-y
Yum install https://www.percona.com/redir/downloads/percona-release/redhat/percona-release-0.1-4.noarch.rpm
Yum install percona-toolkit
two。 Create a logrotate profile
Create a new 3306_error file in the / etc/logrotate.d/ directory
Vim / etc/ogrotate.d/3306_error / data/mysql/3306/error.log {# write down the specific location of your log file monthly # cut rotate 13 # once a month to save 13 logs and rotate 13 months can be compared with the same month last year compared with the same month last year. The file cut by dateext # is named compress # with a similar format of "original file name + 20171230". The file delaycompress # cut using gzip compression is the most The last split file is not compressed (convenient for analysis) missingok # during log rotation Any errors, such as "File cannot be found", will be ignored. Notifempty # if no new log is generated after the last split, the operation performed after postrotate mysql-login-path=3306-e 'flush error logs;' # is not split again. MySQL needs to execute this command to refresh the log handle endscript}
Create a new 3306_slow file in the / etc/logrotate.d/ directory
Vim / etc/logrotate.d/3306_error / data/mysql/3306/slow.log {daily rotate 13 dateext compress delaycompress missingok notifemptypostrotate mysql-- login-path=3306-e 'flush slow logs;' # use the pt-query-digest tool to analyze the sliced slow log and send it to the mailbox pt-query-digest / data/mysql/3306/slow.log- `log +% Y% m% d` | mail-s' 4Saving log log}
3. Manual cutting log
Logrotate-f / etc/logrotate.d/3306_error