Use logrotate to rotate MySQL logs in Linux

Source: Internet
Author: User
Tags exit

Log rotation is especially useful for log files with fixed file names, such as MySQL error log, regular query log, slow query log, and so on. Linux system has a very easy to use according to Logratate can achieve automatic rotation, this article describes its principle and usage.

By default, Logrotate is deployed as a daily cron job, and you can find a configuration file named Logrotate in the directory/etc/cron.daily. So it runs at the top of the day? Open the file/etc/crontab will know, the following is my machine on the situation:

Shell=/bin/bash  
path=/sbin:/bin:/usr/sbin:/usr/bin  
mailto=root  
home=/  
     
# run-parts  
01 * * * * * * Root run-parts/etc/cron.hourly  
4 * * * * Root run-parts/etc/cron.daily  
4 * * 0 root Run-parts/etc/cron.week LY  
4 1 * * Root run-parts/etc/cron.monthly

From the above configuration we can know that the/etc/cron.daily is executed at 4:02 every day. That is, every day 4:02/etc/cron.daily/logrotate will be automatically executed, the following is its content:

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

From the above we can see that the default profile for Logratate is/etc/logratate.conf, and here's what it does:

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  
# "man logrotate" for details  
# rotate log files weekly  
9/># Keep 4 weeks worth of backlogs  
rotate 4  
     
# Create new (empty) log files after rotating old ones  
create< c13/># Uncomment this if you want your log files compressed  
#compress  
     
# RPM Packages drop log Rotation informati On to this directory  
INCLUDE/ETC/LOGROTATE.D  
     
# no packages own wtmp--we'll rotate them here  
/var/log/w TMP {  
    monthly  
    minsize 1M  
    Create 0664 root utmp  
    rotate 1  
}  
     
/var/log/btmp {  
    Missingok  
    monthly  
    minsize 1M  
    Create 0600 root utmp  
    rotate 1  
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.