利用logrotate对MySQL日志进行轮转

日志轮转特别适用于具有固定文件名的日志文件,比如MySQL的出错日志、常规查询日志、慢查询日志等。Linux系统有一个非常好用的根据logratate可以实现自动轮转,本文介绍它的原理和用法。

默认情况下,logrotate部署为每天运行的cron job,你可以在目录/etc/cron.daily里找到名为logrotate的配置文件。那么它是在每天的上面时候运行的呢?打开文件/etc/crontab就知道了,下面是我机器上的情况:

双击代码全选

1

2

3

4

5

6

7

8

9

10

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/logrotate将会自动执行,下面是它的内容:

双击代码全选

1

2

3

4

5

6

7

8

#!/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,下面是它的内容:

双击代码全选

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

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,内容如下:

双击代码全选

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

# 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 = <secret>

# user= root

#

# where "<secret>" is the password.

#

# 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常见选项:



我们只要根据自己的需要,修改相应配置即可,下面是一个例子:

1)创建MySQL root密码文件

vi /root/.my.cnf

双击代码全选

1

2

3

[mysqladmin]

password = ***

user= root

chmod 600 /root/.my.cnf

2)把mysql-log-rotate拷贝至/etc/logrotate.d目录下,修改其内容为:

双击代码全选

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

/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)执行以下命令测试

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

(0)

相关推荐

  • 在Linux系统中使用logrotate来管理日志文件的方法

    日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到.对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题.除此之外,处理一个单个的庞大日 ...

  • 使用logrotate管理VPS日志文件的方法

    我们可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过 crontab程序来执行。 logrotate 程序还可以用于压缩日志文件,以及发送日志到指定的E-mail 。 logrotat ...

  • 浅谈12 条用于 Linux 的 MySQL/MariaDB 安全最佳实践

    MySQL 是世界上最流行的开源数据库系统,MariaDB(一个 MySQL 分支)是世界上增长最快的开源数据库系统.在安装 MySQL 服务器之后,在默认配置下是不安全的,确保数据库安全通常是通用数 ...

  • Navicat for mysql如何创建表

    Navicat for mysql 是mysql数据库的一个可视化的管理工具.下面说下如何利用navicat for mysql 创建表的一个简单例子. 操作方法 01 首先创建一个数据库,就叫use ...

  • 阿里云服务器配置与性能优化

    阿里云我问过很多站长都说非常的不错了,不管是网速还是从性能都比其它主机要好,但阿里云就是贵那么一点了,下面小编来给各位介绍阿里云服务器配置与性能优化 更换阿里云服务器有一阵了,相对于我以前的虚拟主机不 ...

  • Linux下Varnish缓存服务器的安装与配置

    Varnish是一款高性能且开源的反向代理服务器和http加速器.与传统的Squid相比,Varnish具有性能更高.速度更快.管理更方便等诸多优点.作者Poul-Henning Kamp是FreeB ...

  • Linux下Varnish缓存服务器的安装与配置教程

    Varnish是一款高性能且开源的反向代理服务器和http加速器.与传统的Squid相比,Varnish具有性能更高.速度更快.管理更方便等诸多优点.作者Poul-Henning Kamp是FreeB ...

  • nginx 502 Bad Gateway 错误解决办法

    一些运行在Nginx上的网站有时候会出现"502 Bad Gateway"错误,有些时候甚至频繁的出现.以下是小编搜集整理的一些Nginx 502错误的排查方法,供参考: Ngin ...

  • 502 Bad Gateway 错误解决办法

    nginx 502 Bad Gateway 错误解决办法 操作方法 01 一些运行在Nginx上的网站有时候会出现"502 Bad Gateway"错误,有些时候甚至频繁的出现.以 ...