mysql日志切割

2016/02/13 mysql

#mysql日志切割

利用系统自带的lograte进行日志切割

步骤如下

1.在/root/目录下创建.my.cnf文件,以供mysqladmin使用

vim /root/.my.cnf
输入以下内容,其中<secret>是mysql帐号的root密码
[mysqladmin]
password = <secret> 
user= root

设置root属主可读写,其他用户无权查看
chmod 600 /root/.my.cnf

2.在/etc/lograte.d/目录下建立日志切割配置文件

vim /etc/logrotate.d/mysqld 

输入如下内容

# 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=/data/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 !

/home/mysql_log/*general_log.log {
        # create 600 mysql mysql
        notifempty
        daily
        rotate 60
        missingok
        compress
        dateext
    postrotate
        # just if mysqld is really running
        if test -x /usr/local/mysql-5.5.38/bin/mysqladmin && /usr/local/mysql-5.5.38/bin/mysqladmin ping &>/dev/null
        then
           /usr/local/mysql-5.5.38/bin/mysqladmin flush-logs
        fi
    endscript
}

3.执行命令,立即生效

logrotate -vf /etc/logrotate.d/mysqld 

Search

    Table of Contents