切割mysql慢日志_logrotate介绍-并用logrotate切割mysql慢查询日志

本文介绍了logrotate工具,这是一个日志文件管理工具,用于按日期转储和管理日志。通过logrotate配置,我们可以设置按天切割MySQL的慢查询日志,以保持日志的整洁和易管理。文章还详细解释了logrotate配置文件的各个参数及其作用。
摘要由CSDN通过智能技术生成

一,logrotate是什么

logrotate 程序是一个日志文件管理工具。用来把旧的日志文件的内容转存到新的文件中,并清空或者是删除旧的日志文件。

二,用logrotate做什么

我们把日志文件,按照日期来转储,以方便管理和查看,这个过程一般通过 cron 来执行。

三,查看logrotate的配置

cat  /etc/logrotate.conf

# see "man

logrotate" for details

# rotate log files weekly

weekly  #指定所有的日志文件每周转储一次

# keep 4 weeks worth of

backlogs

rotate 4  #指定转储文件的保留 4份

# create new (empty) log files

after rotating old ones

create  # create 指定 logrotate 自动建立新的日志文件,新的日志文件具有和原来的文件一样的权限

# use date as a suffix of the

rotated file

dateext  #轮换的日志后缀为-YYYYMMDD格式

# uncomment this if you want

your log files compressed

#compress  # 指定不压缩转储文件

# RPM packages drop log

rotation information into this directory

include /etc/logrotate.d  #读取/etc/logrotate.d目录中的其他配置文件

# no packages own wtmp and

btmp -- we'll rotate them here

/var/log/wtmp {

monthly

#指定所有的日志文件每月转储一次

create 0664 root utmp   #创建一个新的文件,权限为0664,属主为root,日志文件名为utmp

minsize 1M  #最小为1M

rotate 1

#指定转储文件的保留1份

}

/var/log/btmp {

missingok

#如果日志不存在, 则忽略该警告信息

monthly

#指定所有的日志文件每月转储一次

create 0600 root utmp  #创建一个新的文件,权限为0664,属主为root,日志文件名为utmp

rotate 1

#指定转储文件的保留 1份

}

# system-specific logs may be

also be configured here.

下面看一个典型的脚本 /etc/logrotate.d/syslog

cat syslog

/var/log/cron  #针对/var/log/cron有效

/var/log/maillog  #针对/var/log/maillog有效

/var/log/messages  #同上

/var/log/secure   #同上

/var/log/spooler   #同上

{

sharedscripts  #只为整个日志组运行一次的脚本

postrotate

#引入在轮换过日志之后要运行的脚本

#用来重新初始化系统日志守护程序 syslogd

/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null`

2> /dev/null || true

endscript

#结束postrotate脚本

}

下面是logrotate的执行时间,可以自己修改/etc/crontab:

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

logrotate 其他的参数如下表:

参数

功能

compress

通过gzip 压缩转储以后的日志

nocompress

不需要压缩时,用这个参数

copytruncate

用于还在打开中的日志文件,把当前日志备份并截断

nocopytruncate

备份日志文件但是不截断

create mode owner group      转储文件,使用指定的文件模式创建新的日志文件

nocreate

不建立新的日志文件

delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩

nodelaycompress

覆盖

delaycompress 选项,转储同时压缩。

errors

address

专储时的错误信息发送到指定的Email 地址

ifempty

即使是空文件也转储,这个是 logrotate 的缺省选项。

notifempty

如果是空文件的话,不转储

mail

address

把转储的日志文件发送到指定的E-mail 地址

nomail

转储时不发送日志文件

olddir

directory

转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统

noolddir

转储后的日志文件和当前日志文件放在同一个目录下

prerotate/endscript          在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行

postrotate/endscript         在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行

daily

指定转储周期为每天

weekly

指定转储周期为每周

monthly

指定转储周期为每月

rotate

count

指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份

tabootext [+]

list           让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~

size

size

当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB

(sizek)或者MB

(sizem).

四,用logrotate按日期切割mysql慢查询日志

vi

/etc/logrotate.d/mysqld

# 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=/var/log/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 "" is the

password.

#

# ATTENTION: This /root/.my.cnf should be

readable ONLY

# for root !

# Then, un-comment the following lines to

enable rotation of mysql's log file:

/var/log/mysqld.log

/var/run/mysqld/*.log{

# create 640 mysql mysql

notifempty

copytruncate

#if you use copytruncate,then postrotate you don't use again!

daily

dateext

rotate 7

#

compress

#

postrotate

#     just if mysqld is really running

#

if test -x /usr/bin/mysqladmin && \

#

/usr/bin/mysqladmin ping &>/dev/null

#

then

#

/usr/bin/mysqladmin flush-logs

#

fi

#

endscript

}

注:前提是mysql的slowlog我已经开启,并且路径配置在在/var/run/mysqld下面。copytruncate是转储后清空旧的日志文件;如果不想用copytruncate可以把后面的postrotate脚本打开,用mysqladmin来flushlogs。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值