MySQL的logrotate脚本

随着业务量的增加和时间的推移MySQL的日志势必会越来越多、越来越大,那么就要用系统的logrotate轮替mysql的日志(logrotate可以参看: ),当然平时用的mysql都是二进制包部署安装的,其实mysql的二进制包中的support-files文件夹里就有提供mysql的logrotate脚本,文件名为:mysql-log-rotate,当然如果要使用的话还是要修改下才能使用,如下是我修改过的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>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/5a3b77bbadeee0e1c8bfdf14cb9128ce.html