Logrotate Mysql - 没有旋转发生

1 mysql centos logrotate

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

mysql Ver 14.12 Distrib 5.0.77, for redhat-linux-gnu (i686) using 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.
Run Code Online (Sandbox Code Playgroud)

/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

}
Run Code Online (Sandbox Code Playgroud)

这是我注意到的关于 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
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的帮助 !

西尔万

sum*_*mar 7

没有轮换,因为您正在使用-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
}
Run Code Online (Sandbox Code Playgroud)