Use Logrotate to perform a day rotation backup of MySQL specified log files

Source: Internet
Author: User

Depending on your needs, you can make a day-to-days dump of the MySQL-related log files (error logs, common query log files, and slow query log files) and keep only the backup files for a specified number of days, as an example:

1), install Crond service (if yum install logrotate crontabsis not installed)

Rpm-qa | grep crontabs

Yum Install Crontabs

Chkconfig Crond--list #如果未设置为自动重启这执行如下命令修改

Chkconfig Crond on

Service Crond Restart


2), create the MySQL root password file

Vi/root/.my.cnf

[Mysqladmin]

user = sysadmin

Password = MySQL

chmod 600/root/.my.cnf


3), copy the mysql-log-rotate to the/ETC/LOGROTATE.D directory and modify its contents as follows:

# Mysql_error.log Error log file

# Mysql_general.log General Query log file

# mysql_slow.log Slow Query log file

/app/mysql/mysql3306/logs/mysql_error.log

/app/mysql/mysql3306/logs/mysql_general.log

/app/mysql/mysql3306/logs/mysql_slow.log {

# create MySQL MySQL

Notifempty

Daily

Rotate 5

Copytruncate

Nocreate

Missingok

Compress

Dateext

Postrotate

# just if Mysqld is really running

If Test-x/app/mysql/mysql3306/bin/mysqladmin && \

/app/mysql/mysql3306/bin/mysqladmin Ping &>/dev/null

Then

/app/mysql/mysql3306/bin/mysqladmin Flush-logs

Fi

Endscript

}


4) Execute the following command to test whether the configuration is correct

/usr/sbin/logrotate-f/etc/logrotate.d/mysql-log-rotate

Note: You need to view the backed up compressed files This execution Gunzip file_name.gz can


5) timed execution (early morning execution)

# Vim/etc/crontab

* * * * root/usr/sbin/logrotate-f/etc/logrotate.d/mysql-log-rotate


This article is from the "Lone Nine Swords" blog, please be sure to keep this source http://130030.blog.51cto.com/120030/1922805

Use Logrotate to perform a day rotation backup of MySQL specified log files

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.