http://blog.csdn.net/dbanote/article/details/12966605

来源:互联网 发布:欧洲文明概论网络课 编辑:程序博客网 时间:2024/06/06 06:46
 

[Linux] 利用logrotate对MySQL日志进行轮转

标签: Linuxmysql日志轮换logratate
 4316人阅读 评论(5) 收藏 举报
 分类:
 

日志轮转特别适用于具有固定文件名的日志文件,比如MySQL的出错日志、常规查询日志、慢查询日志等。Linux系统有一个非常好用的根据logratate可以实现自动轮转,本文介绍它的原理和用法。

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

[plain] view plain copy
 print?
  1. SHELL=/bin/bash  
  2. PATH=/sbin:/bin:/usr/sbin:/usr/bin  
  3. MAILTO=root  
  4. HOME=/  
  5.   
  6. # run-parts  
  7. 01 * * * * root run-parts /etc/cron.hourly  
  8. 02 4 * * * root run-parts /etc/cron.daily  
  9. 22 4 * * 0 root run-parts /etc/cron.weekly  
  10. 42 4 1 * * root run-parts /etc/cron.monthly  
从上面的配置我们可以知道,/etc/cron.daily是在每天凌晨4:02执行。也就是说,每天4:02分/etc/cron.daily/logrotate将会自动执行,下面是它的内容:

[plain] view plain copy
 print?
  1. #!/bin/sh  
  2.   
  3. /usr/sbin/logrotate /etc/logrotate.conf  
  4. EXITVALUE=$?  
  5. if [ $EXITVALUE != 0 ]; then  
  6.     /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"  
  7. fi  
  8. exit 0  
从上面我们可以知道,logratate默认的配置文件是/etc/logratate.conf,下面是它的内容:

[plain] view plain copy
 print?
  1. EXITVALUE=$?  
  2. if [ $EXITVALUE != 0 ]; then  
  3.     /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"  
  4. fi  
  5. exit 0  
  6. [root@lx202 /etc/cron.daily ]# cat /etc/logrotate.conf  
  7. # see "man logrotate" for details  
  8. # rotate log files weekly  
  9. weekly  
  10.   
  11. # keep 4 weeks worth of backlogs  
  12. rotate 4  
  13.   
  14. # create new (empty) log files after rotating old ones  
  15. create  
  16.   
  17. # uncomment this if you want your log files compressed  
  18. #compress  
  19.   
  20. # RPM packages drop log rotation information into this directory  
  21. include /etc/logrotate.d  
  22.   
  23. # no packages own wtmp -- we'll rotate them here  
  24. /var/log/wtmp {  
  25.     monthly  
  26.     minsize 1M  
  27.     create 0664 root utmp  
  28.     rotate 1  
  29. }  
  30.   
  31. /var/log/btmp {  
  32.     missingok  
  33.     monthly  
  34.     minsize 1M  
  35.     create 0600 root utmp  
  36.     rotate 1  
  37. }  
从上面我们可以知道,这个默认的配置文件将读取目录/etc/logrotate.d,所以我们只要把自己写的配置文件放到该目录下即可。

MySQL本省提供了一个rotate的参考配置文件,在support-files目录下,文件名为mysql-log-rotate,内容如下:

[plain] view plain copy
 print?
  1. # This logname can be set in /etc/my.cnf  
  2. # by setting the variable "err-log"  
  3. # in the [safe_mysqld] section as follows:  
  4. #  
  5. # [safe_mysqld]  
  6. # err-log=/opt/mysql/data/mysqld.log  
  7. #  
  8. # If the root user has a password you have to create a  
  9. # /root/.my.cnf configuration file with the following  
  10. # content:  
  11. #  
  12. # [mysqladmin]  
  13. # password = <secret>  
  14. # user= root  
  15. #  
  16. # where "<secret>" is the password.  
  17. #  
  18. # ATTENTION: This /root/.my.cnf should be readable ONLY  
  19. # for root !  
  20.   
  21. /opt/mysql/data/mysqld.log {  
  22.         # create 600 mysql mysql  
  23.         notifempty  
  24.         daily  
  25.         rotate 3  
  26.         missingok  
  27.         compress  
  28.     postrotate  
  29.         # just if mysqld is really running  
  30.         if test -x /opt/mysql/bin/mysqladmin && \  
  31.            /opt/mysql/bin/mysqladmin ping &>/dev/null  
  32.         then  
  33.            /opt/mysql/bin/mysqladmin flush-logs  
  34.         fi  
  35.     endscript  
  36. }  

logrotate常见选项:

选项含义compress压缩日志文件的所有非当前版本copy复制当前的日志文件,忽略create参数
copytruncate复制当前的日志文件,并置空当前文件daily每天轮日志文件idateext轮换的日志后缀为-YYYYMMDD格式delaycompress压缩除了当前和最近之外的所有其他版本missingok如果日志不存在,不会报错notifempty如果日志为空,则不轮换rotate n在轮换方案中包含n个版本的日志size=logsize如果日志文件大于logsize才轮换
我们只要根据自己的需要,修改相应配置即可,下面是一个例子:

1)创建MySQL root密码文件

vi /root/.my.cnf

[plain] view plain copy
 print?
  1. [mysqladmin]  
  2. password = ***  
  3. user= root  

chmod 600 /root/.my.cnf
2)把mysql-log-rotate拷贝至/etc/logrotate.d目录下,修改其内容为:

[plain] view plain copy
 print?
  1. /data/mysql/log/slow.log  
  2. /data/mysql/log/alert.log {  
  3.         create 600 mysql mysql  
  4.         notifempty  
  5.         daily  
  6.         rotate 7  
  7.         missingok  
  8.         # compress  
  9.     postrotate  
  10.         # just if mysqld is really running  
  11.         if test -x /opt/mysql/bin/mysqladmin && \  
  12.            /opt/mysql/bin/mysqladmin ping &>/dev/null  
  13.         then  
  14.            /opt/mysql/bin/mysqladmin flush-logs  
  15.         fi  
  16.     endscript  
  17. }  

3)执行以下命令测试

[plain] view plain copy
 print?
  1. /usr/sbin/logrotate -f /etc/logrotate.d/mysql-log-rotate  

阅读全文
0 0
原创粉丝点击