[Linux] using logrotate to rotate MySQL logs _ MySQL

Source: Internet
Author: User
[Linux] using logrotate to rotate MySQL logs bitsCN.com

[Linux] using logrotate to rotate MySQL logs

Log rotation is especially suitable for log files with fixed file names, such as MySQL error logs, General query logs, and slow query logs. In Linux, logratate can be used for automatic rotation. This article introduces the principle and usage of logratate.

By default, logratate is deployed as a daily running cron job. you can find the configuration file named logratate in the/etc/cron. daily directory. So it runs on the top of every day? Open the/etc/crontab file and you will be able to see it. The following figure shows my situation on the machine:

[Plain]

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

From the above configuration, we can know that/etc/cron. daily is executed at every day. That is to say,/etc/cron. daily/logratate will be automatically executed every day. The following is its content:

[Plain]

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

As we can see from the above, the default configuration file of logratate is/etc/logratate. conf, and the following is its content:

[Plain]

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 1 M

Create 0664 root utmp

Rotate 1

}

/Var/log/btmp {

Missingok

Monthly

Minsize 1 M

Create 0600 root utmp

Rotate 1

}

As we can see from the above, this default configuration file will read the/etc/logrotate. d directory, so we just need to put the configuration file we have written in this directory.

MySQL provides a reference configuration file for rotate in this province. in the support-files directory, the file name is mysql-log-rotate. the content is as follows:

[Plain]

# This logname can be set in/etc/my. cnf

# By setting the variable "err-log"

# In the [safe_mysqld] section as follows:

#

# [Safe_mysqld]

# Err-log =/opt/mysql/data/mysqld. log

#

# If the root user has a password you have to create

#/Root/. my. cnf configuration file with the following

# Content:

#

# [Mysqladmin]

# Password =

# User = root

#

# Where" "Is the password.

#

# ATTENTION: This/root/. my. cnf shocould be readable ONLY

# For root!

/Opt/mysql/data/mysqld. log {

# Create 600 mysql

Notifempty

Daily

Rotate 3

Missingok

Compress

Postrotate

# Just if mysqld is really running

If test-x/opt/mysql/bin/mysqladmin &&/

/Opt/mysql/bin/mysqladmin ping &>/dev/null

Then

/Opt/mysql/bin/mysqladmin flush-logs

Fi

Endscript

}

Common logrotate options:

Option description

Compress compresses all non-current versions of log files

Copy the current log file and ignore the create parameter.

Copytruncate: copy the current log file and leave the current file empty.

Daily log file I

The log suffix of dateext rotation is-YYYYMMDD format.

Delaycompress compresses all versions except current and recent versions

Missingok: If the log does not exist, no error is reported.

Notifempty if the log is empty, it is not rotated

Rotate n contains n versions of logs in the rotation scheme

Size = logsize rotate only when the log file is larger than logsize

You only need to modify the corresponding configuration as needed. The following is an example:

1) create a MySQL root password file

Vi/root/. my. cnf

[Plain]

[Mysqladmin]

Password = ***

User = root

Chmod 600/root/. my. cnf

2) copy mysql-log-rotate to the/etc/logrotate. d Directory and modify its content:

[Plain]

/Data/mysql/log/slow. log

/Data/mysql/log/alert. log {

Create 600 mysql

Notifempty

Daily

Rotate 7

Missingok

# Compress

Postrotate

# Just if mysqld is really running

If test-x/opt/mysql/bin/mysqladmin &&/

/Opt/mysql/bin/mysqladmin ping &>/dev/null

Then

/Opt/mysql/bin/mysqladmin flush-logs

Fi

Endscript

}

3) execute the following command to test

[Plain]

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

BitsCN.com

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.