[Linux] 利用logrotate对MySQL日志进行轮转_MySQL
bitsCN.com
[Linux] 利用logrotate对MySQL日志进行轮转
日志轮转特别适用于具有固定文件名的日志文件,比如MySQL的出错日志、常规查询日志、慢查询日志等。Linux系统有一个非常好用的根据logratate可以实现自动轮转,本文介绍它的原理和用法。
默认情况下,logratate部署为每天运行的cron job,你可以在目录/etc/cron.daily里找到名为logratate的配置文件。那么它是在每天的上面时候运行的呢?打开文件/etc/crontab就知道了,下面是我机器上的情况:
[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
从上面的配置我们可以知道,/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 -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
从上面我们可以知道,logratate默认的配置文件是/etc/logratate.conf,下面是它的内容:
[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 1M
create 0664 root utmp
rotate 1
}
/var/log/btmp {
missingok
monthly
minsize 1M
create 0600 root utmp
rotate 1
}
从上面我们可以知道,这个默认的配置文件将读取目录/etc/logrotate.d,所以我们只要把自己写的配置文件放到该目录下即可。
MySQL本省提供了一个rotate的参考配置文件,在support-files目录下,文件名为mysql-log-rotate,内容如下:
[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 a
# /root/.my.cnf configuration file with the following
# content:
#
# [mysqladmin]
# password =
# user= root
#
# where "
#
# ATTENTION: This /root/.my.cnf should be readable ONLY
# for root !
/opt/mysql/data/mysqld.log {
# create 600 mysql 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
}
logrotate常见选项:
选项 含义
compress 压缩日志文件的所有非当前版本
copy 复制当前的日志文件,忽略create参数
copytruncate 复制当前的日志文件,并置空当前文件
daily 每天轮日志文件i
dateext 轮换的日志后缀为-YYYYMMDD格式
delaycompress 压缩除了当前和最近之外的所有其他版本
missingok 如果日志不存在,不会报错
notifempty 如果日志为空,则不轮换
rotate n 在轮换方案中包含n个版本的日志
size=logsize 如果日志文件大于logsize才轮换
我们只要根据自己的需要,修改相应配置即可,下面是一个例子:
1)创建MySQL root密码文件
vi /root/.my.cnf
[plain]
[mysqladmin]
password = ***
user= root
chmod 600 /root/.my.cnf
2)把mysql-log-rotate拷贝至/etc/logrotate.d目录下,修改其内容为:
[plain]
/data/mysql/log/slow.log
/data/mysql/log/alert.log {
create 600 mysql 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)执行以下命令测试
[plain]
/usr/sbin/logrotate -f /etc/logrotate.d/mysql-log-rotate
bitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...
