MySQL的logrotate脚本

# This logname can be set in /etc/my.cnf
# by setting the variable "err-log"
# in the [safe_mysqld] section as follows:
#
# [safe_mysqld]
# err-log=/usr/local/mysql/data/mysqld.log
#
# If the root user has a password you have to create a
# /root/.my.cnf configuration file with the following
# content:
#
# [mysqladmin]
# password = <secret>
# user= root
#
# where "<secret>" is the password.
#
# ATTENTION: This /root/.my.cnf should be readable ONLY
# for root !
 
#这里日志文件为datadir下的*.log和*.err文件
/data/mysqldata/*.log /data/mysqldata/*.err {
        create 600 mysql mysql
        notifempty#如果日志为空logrotate不会进行
      daily
        rotate 3
        missingok#日志logrotate期间任何错误都忽略
        compress
    postrotate#在logrotate之前要判断mysql是否启动,如果没有启动就flush-logs
      # just if mysqld is really running
      if test -x/usr/local/mysql/bin/mysqladmin && \
          /usr/local/mysql/bin/mysqladmin ping &>/dev/null
      then
          /usr/local/mysql/bin/mysqladmin flush-logs
      fi
    endscript
}
[root@localhost support-files]# cp mysql-log-rotate /etc/logrotate.d/mysqld

把logrotate文件复制在logrotate.d文件夹里,这里主要一点是如果你的mysql的root用户有加密码,那么你要在root的家目录中新建.my.cnf添加以下内容,使得mysqladmin能用:

[root@localhost ~]# vim .my.cnf
[mysqladmin]
user = root
host = <youhost>
password = <youpassword>

相关推荐