加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_镇江站长网 (https://www.0511zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Linux中使用logrotate对MySQL日志进行轮转 - Linux系统

发布时间:2014-06-01 23:05:52 所属栏目:Linux 来源:站长网
导读:日志轮转特别适用于具有固定文件名的日志文件,比如MySQL的出错日志、常规查询日志、慢查询日志 等。Linux系统有一个非常好用的根据logratate可以实现自动轮转
日志轮转特别适用于具有固定文件名的日志文件,比如MySQL的出错日志、常规查询日志、慢查询日志 等。Linux系统有一个非常好用的根据logratate可以实现自动轮转,本文介绍它的原理和用法。

默认情况下,logrotate部署为每天运行的cron job,你可以在目录/etc/cron.daily里找到名为 logrotate的配置文件。那么它是在每天的上面时候运行的呢?打开文件/etc/crontab就知道了,下面是 我机器上的情况:

SHELL=/bin/bash  
PATH=/sbin:/bin:/usr/sbin:/usr/bin  
MAILTO=root  
HOME=/  
     
# run-parts  
01 * * * * root run-parts /etc/cron.hourly  
02 4 * * * root run-parts /etc/cron.daily  
22 4 * * 0 root run-parts /etc/cron.weekly  
42 4 1 * * root run-parts /etc/cron.monthly

从上面的配置我们可以知道,/etc/cron.daily是在每天凌晨4:02执行。也就是说,每天4:02 分/etc/cron.daily/logrotate将会自动执行,下面是它的内容:

#!/bin/sh  
     
/usr/sbin/logrotate /etc/logrotate.conf  
EXITVALUE=$?  
if [ $EXITVALUE != 0 ]; then  
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"  
fi  
exit 0

从上面我们可以知道,logratate默认的配置文件是/etc/logratate.conf,下面是它的内容:

EXITVALUE=$?  
if [ $EXITVALUE != 0 ]; then  
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"  
fi  
exit 0  
[root@lx202 /etc/cron.daily ]# cat /etc/logrotate.conf  
# see "man logrotate" for details  
# rotate log files weekly  
weekly  
     
# keep 4 weeks worth of backlogs  
rotate 4  
     
# create new (empty) log files after rotating old ones  
create  
     
# uncomment this if you want your log files compressed  
#compress  
     
# RPM packages drop log rotation information into this directory  
include /etc/logrotate.d  
     
# no packages own wtmp -- we'll rotate them here  
/var/log/wtmp {  
    monthly  
    minsize 1M  
    create 0664 root utmp  
    rotate 1  
}  
     
/var/log/btmp {  
    missingok  
    monthly  
    minsize 1M  
    create 0600 root utmp  
    rotate 1  
}

(编辑:应用网_镇江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!