Depending on your needs, you can make a day-to-days dump of the MySQL-related log files (error logs, common query log files, and slow query log files) and keep only the backup files for a specified number of days, as an example:
1), install Crond service (if yum install logrotate crontabsis not installed)
Rpm-qa | grep crontabs
Yum Install Crontabs
Chkconfig Crond--list #如果未设置为自动重启这执行如下命令修改
Chkconfig Crond on
Service Crond Restart
2), create the MySQL root password file
Vi/root/.my.cnf
[Mysqladmin]
user = sysadmin
Password = MySQL
chmod 600/root/.my.cnf
3), copy the mysql-log-rotate to the/ETC/LOGROTATE.D directory and modify its contents as follows:
# Mysql_error.log Error log file
# Mysql_general.log General Query log file
# mysql_slow.log Slow Query log file
/app/mysql/mysql3306/logs/mysql_error.log
/app/mysql/mysql3306/logs/mysql_general.log
/app/mysql/mysql3306/logs/mysql_slow.log {
# create MySQL MySQL
Notifempty
Daily
Rotate 5
Copytruncate
Nocreate
Missingok
Compress
Dateext
Postrotate
# just if Mysqld is really running
If Test-x/app/mysql/mysql3306/bin/mysqladmin && \
/app/mysql/mysql3306/bin/mysqladmin Ping &>/dev/null
Then
/app/mysql/mysql3306/bin/mysqladmin Flush-logs
Fi
Endscript
}
4) Execute the following command to test whether the configuration is correct
/usr/sbin/logrotate-f/etc/logrotate.d/mysql-log-rotate
Note: You need to view the backed up compressed files This execution Gunzip file_name.gz can
5) timed execution (early morning execution)
# Vim/etc/crontab
* * * * root/usr/sbin/logrotate-f/etc/logrotate.d/mysql-log-rotate
This article is from the "Lone Nine Swords" blog, please be sure to keep this source http://130030.blog.51cto.com/120030/1922805
Use Logrotate to perform a day rotation backup of MySQL specified log files