系统大全为您提供[Linux]利用logrotate对MysqL日志进行轮转 日志轮转特别适用于具有固定文件名的日志文件,比如MysqL的出错日志、常规查询日志、慢查询日志等。Linux系统有一个非常好用的根据logratate可以实现自动轮转,本文介绍它的原理和用法。默认情况下,logratate部署为每天运行的cronjob,你可以在目录/etc/cron.daily里找到名为logratate的配置文件。那么它是在每天的上面时候运行的呢?打开文件/etc/crontab就知道了,下面是我机器上的情况:[plain] SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ #run-parts 01****rootrun-parts/etc/cron.hourly 024***rootrun-parts/etc/cron.daily 224**0rootrun-parts/etc/cron.weekly 4241**rootrun-parts/etc/cron.monthly 从上面的配置我们可以知道,/etc/cron.daily是在每天凌晨4:02执行。也就是说,每天4:02分/etc/cron.daily/logratate将会自动执行,下面是它的内容:[plain] #!/bin/sh /usr/sbin/logrotate/etc/logrotate.conf EXITVALUE=$? if[$EXITVALUE!=0];then /usr/bin/logger-tlogrotate"ALERTexitedabnormallywith[$EXITVALUE]" fi exit0 从上面我们可以知道,logratate默认的配置文件是/etc/logratate.conf,下面是它的内容:[plain] EXITVALUE=$? if[$EXITVALUE!=0];then /usr/bin/logger-tlogrotate"ALERTexitedabnormallywith[$EXITVALUE]" fi exit0 [root@lx202/etc/cron.daily]#cat/etc/logrotate.conf #see"manlogrotate"fordetails #rotatelogfilesweekly weekly #keep4weeksworthofbacklogs rotate4 #createnew(empty)logfilesafterrotatingoldones create #uncommentthisifyouwantyourlogfilescompressed #compress #RPMpackagesdroplogrotationinformationintothisdirectory include/etc/logrotate.d #nopackagesownwtmp--we'llrotatethemhere /var/log/wtmp{ monthly minsize1M create0664rootutmp rotate1 } /var/log/btmp{ missingok monthly minsize1M create0600rootutmp rotate1 } 从上面我们可以知道,这个默认的配置文件将读取目录/etc/logrotate.d,所以我们只要把自己写的配置文件放到该目录下即可。MysqL本省提供了一个rotate的参考配置文件,在support-files目录下,文件名为MysqL-log-rotate,内容如下:[plain] #Thislognamecanbesetin/etc/my.cnf #bysettingthevariable"err-log" #inthe[safe_MysqLd]sectionasfollows: # #[safe_MysqLd] #err-log=/opt/MysqL/data/MysqLd.log # #Iftherootuserhasapasswordyouhavetocreatea #/root/.my.cnfconfigurationfilewiththefollowing #content: # #[MysqLadmin] #password=<secret> #user=root # #where"<secret>"isthepassword. # #ATTENTION:This/root/.my.cnfshouldbereadableONLY #forroot! /opt/MysqL/data/MysqLd.log{ #create600MysqLMysqL notifempty daily rotate3 missingok compress postrotate #justifMysqLdisreallyrunning iftest-x/opt/MysqL/bin/MysqLadmin&& /opt/MysqL/bin/MysqLadminping&>/dev/null then /opt/MysqL/bin/MysqLadminflush-logs fi endscript } logrotate常见选项:选项含义compress压缩日志文件的所有非当前版本copy复制当前的日志文件,忽略create参数copytruncate复制当前的日志文件,并置空当前文件daily每天轮日志文件idateext轮换的日志后缀为-YYYYMMDD格式delaycompress压缩除了当前和最近之外的所有其他版本missingok如果日志不存在,不会报错notifempty如果日志为空,则不轮换rotaten在轮换方案中包含n个版本的日志size=logsize如果日志文件大于logsize才轮换我们只要根据自己的需要,修改相应配置即可,下面是一个例子:1)创建MysqLroot密码文件vi/root/.my.cnf[plain] [MysqLadmin] password=*** user=root chmod600/root/.my.cnf2)把MysqL-log-rotate拷贝至/etc/logrotate.d目录下,修改其内容为:[plain] /data/MysqL/log/slow.log /data/MysqL/log/alert.log{ create600MysqLMysqL notifempty daily rotate7 missingok #compress postrotate #justifMysqLdisreallyrunning iftest-x/opt/MysqL/bin/MysqLadmin&& /opt/MysqL/bin/MysqLadminping&>/dev/null then /opt/MysqL/bin/MysqLadminflush-logs fi endscript } 3)执行以下命令测试[plain] /usr/sbin/logrotate-f/etc/logrotate.d/MysqL-log-rotate 以上就是系统大全给大家介绍的如何使的方法都有一定的了解了吧,好了,如果大家还想了解更多的资讯,那就赶紧点击系统大全官网吧。本文来自系统大全http:///如需转载请注明!推荐:win7纯净版