Logrotate Mysql - 没有发生旋转

Logrotate Mysql - 没有发生旋转

我使用的是 CentOS5,并且拥有以下版本的 mysql:

mysql Ver 14.12 Distrib 5.0.77,适用于 redhat-linux-gnu (i686),使用 readline 5.1

我想使用 logrotate 轮换 mysql 的日志。

假设我只想旋转文件 /var/lib/mysql/mysql-log.log 作为示例。下面我将描述我所遵循的过程、我所注意到的 logrotate 行为,然后给出我的配置文件的内容。

我遵循以下步骤:

  1. 我在/etc/logrotate.d中添加了一个名为mysql的文件(见下文内容),权限为660(该文件属于root)
  2. 我尝试使用命令“logrotate -dv --force /etc/logrotate.conf”强制旋转

我注意到的行为:

  • logrotate 命令生成的消息似乎显示 /var/lib/mysql/mysql-log.log 已旋转
  • 其实,/var/lib/mysql/中根本没有“mysql-log.*”,只有原始文件/var/lib/mysql/mysql-log.log,和执行命令前一样大……

我现在给你我的 onfig 文件,以及与 mysql 有关的命令 logrotate 生成的日志的摘录。

/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

}

# system-specific logs may be also be configured here.

/etc/logrotate.d/mysql

/var/lib/mysql/mysql-log.log {

    rotate 4

    daily

    nocompress

    missingok

    create 660 mysql mysql

    postrotate

      if ls /proc/`cat /var/run/mysqld/mysqld.pid` > /dev/null

      then

        /usr/bin/mysqladmin -pMotDePasseSqlRoot flush-logs

      fi

    endscript

}

下面是我记录的有关 mysql 的 logrotate 日志:

reading config file mysql
reading config info for /var/lib/mysql/mysql-log.log 
...
rotating pattern: /var/lib/mysql/mysql-log.log  forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/lib/mysql/mysql-log.log
  log needs rotating
rotating log /var/lib/mysql/mysql-log.log, log->rotateCount is 4
renaming /var/lib/mysql/mysql-log.log.4 to /var/lib/mysql/mysql-log.log.5 (rotatecount 4, logstart 1, i 4), 
renaming /var/lib/mysql/mysql-log.log.3 to /var/lib/mysql/mysql-log.log.4 (rotatecount 4, logstart 1, i 3), 
renaming /var/lib/mysql/mysql-log.log.2 to /var/lib/mysql/mysql-log.log.3 (rotatecount 4, logstart 1, i 2), 
renaming /var/lib/mysql/mysql-log.log.1 to /var/lib/mysql/mysql-log.log.2 (rotatecount 4, logstart 1, i 1), 
renaming /var/lib/mysql/mysql-log.log.0 to /var/lib/mysql/mysql-log.log.1 (rotatecount 4, logstart 1, i 0), 
renaming /var/lib/mysql/mysql-log.log to /var/lib/mysql/mysql-log.log.1
creating new log mode = 0660 uid = 27 gid = 27
running postrotate script
running script with arg /var/lib/mysql/mysql-log.log : "
      if ls /proc/`cat /var/run/mysqld/mysqld.pid` > /dev/null
      then
        /usr/bin/mysqladmin -pMotDePasseSqlRoot flush-logs
      fi
"
removing old log /var/lib/mysql/mysql-log.log.5

你知道为什么 logrotates 会报告它实际上没有执行的操作吗?我在定义文件 /etc/logrotated.d/mysql 之前或之后是否错过了某个步骤?

感谢您的帮助 !

西尔万

答案1

没有旋转,因为你正在使用-d选项。来自手册:

-d 打开调试模式并隐含 -v。在调试模式下,不会做出任何改变到日志或 logrotate 状态文件。

我有两点建议:

  • 将操作系统升级到 x86_64如果可能的话,因为每个进程的内存限制为 2.5GB
  • 出于安全原因,不要-pPassword在 mysql 中使用参数,最好使用--defaults-extra-file=path_to_extra,它只能由 root 读取。当有权访问服务器的人执行 时ps -ef | grep mysql,它将看到数据库的 root 密码。

这是我/etc/logrotate.d/mysql在 CentOS 5 上针对 mysql 的 logrotate 脚本(mysql Ver 14.12 Distrib 5.0.77,适用于 redhat-linux-gnu (x86_64),使用 readline 5.1):

# If the root user has a password you have to create a
# /.my.cnf configuration file with the following
# content:
#
# [mysqladmin]
# password = <secret>
# user = <username>
#
# where "<secret>" is the password and <username> is user.
#
# ATTENTION: This /.my.cnf should be readable ONLY
# for root !
/var/log/mysqld.log {
        create 640 mysql mysql
        notifempty
        missingok
        postrotate
                # just if mysqld is really running
                if test -x /usr/bin/mysqladmin && \
                   /usr/bin/mysqladmin --defaults-extra-file=/root/mysql/logrotate.cnf ping &>/dev/null
                then
                   /usr/bin/mysqladmin --defaults-extra-file=/root/mysql/logrotate.cnf flush-logs
                fi
                /usr/bin/chcon -u system_u -r object_r -t mysqld_log_t /var/log/mysqld.log
        endscript
}

答案2

此选项工作正常--defaults-extra-file=/root/mysql/logrotate.cnf,我这样做:

[client]
user     = root
password = TheRootPassword

从我的初始命令中删除-dv选项后,我可以测试旋转。

答案3

我无法确定守护进程的 -HUP 是否会导致它像应该的那样刷新并重新打开其日志。

下面是另一个参考(我试图澄清,另一个指向建议简单 HUP 的页面的 URL 可以起作用): http://www.mysqlperformanceblog.com/2007/12/09/be-careful-rotating-mysql-logs/comment-page-1/#comment-1043963

YMMV,因为我不是一个 mysql 专家,但如果它是正确的,你的 logrotate 可以像对所有其他礼貌应用程序一样 HUP 该过程。

相关内容