在 logrotate 之后不写 MariaDB 慢日志
Posted
技术标签:
【中文标题】在 logrotate 之后不写 MariaDB 慢日志【英文标题】:MariaDB slow log is not written after logrotate 【发布时间】:2018-04-28 19:58:35 【问题描述】:我们的一台 MariaDB 服务器的 logrotate 出现问题。 Logrotate 正在运行而没有(显然)抛出任何错误。旋转 mariadb-slow.log 文件后,MariaDB 不会将任何消息写入新的日志文件。 该问题仅影响 mariadb-slow.log。
当我从 logrotate 配置文件的 postrotate 部分运行 mysqladmin 命令时,maraidb-slow.log 再次被写入。
mysqladmin --defaults-file="/etc/mysql/debian.cnf" --local flush-error-log flush-engine-log flush-general-log flush-slow-log
手动运行 logrotate 也会导致问题。
logrotate /etc/logrotate.d/mysql-server
添加 -v 参数没有什么明显的影响
logrotate -v /etc/logrotate.d/mysql-server
reading config file /etc/logrotate.d/mysql-server
Reading state from file: /var/lib/logrotate/status
Allocating hash table for state file, size 64 entries
Creating new state
...
Creating new state
Handling 1 logs
rotating pattern: /var/log/mysql/mysql.log /var/log/mysql/mysql-slow.log /var/log/mysql/mariadb-slow.log /var/log/mysql/error.log 1024 bytes (7 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/mysql/mysql.log
log /var/log/mysql/mysql.log does not exist -- skipping
considering log /var/log/mysql/mysql-slow.log
log /var/log/mysql/mysql-slow.log does not exist -- skipping
considering log /var/log/mysql/mariadb-slow.log
Now: 2017-11-15 15:02
Last rotated at 2017-11-15 13:54
log needs rotating
considering log /var/log/mysql/error.log
Now: 2017-11-15 15:02
Last rotated at 2017-11-15 13:54
log needs rotating
rotating log /var/log/mysql/mariadb-slow.log, log->rotateCount is 7
dateext suffix '-20171115'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/mysql/mariadb-slow.log.7.gz to /var/log/mysql/mariadb-slow.log.8.gz (rotatecount 7, logstart 1, i 7),
renaming /var/log/mysql/mariadb-slow.log.6.gz to /var/log/mysql/mariadb-slow.log.7.gz (rotatecount 7, logstart 1, i 6),
old log /var/log/mysql/mariadb-slow.log.6.gz does not exist
renaming /var/log/mysql/mariadb-slow.log.5.gz to /var/log/mysql/mariadb-slow.log.6.gz (rotatecount 7, logstart 1, i 5),
renaming /var/log/mysql/mariadb-slow.log.4.gz to /var/log/mysql/mariadb-slow.log.5.gz (rotatecount 7, logstart 1, i 4),
renaming /var/log/mysql/mariadb-slow.log.3.gz to /var/log/mysql/mariadb-slow.log.4.gz (rotatecount 7, logstart 1, i 3),
renaming /var/log/mysql/mariadb-slow.log.2.gz to /var/log/mysql/mariadb-slow.log.3.gz (rotatecount 7, logstart 1, i 2),
renaming /var/log/mysql/mariadb-slow.log.1.gz to /var/log/mysql/mariadb-slow.log.2.gz (rotatecount 7, logstart 1, i 1),
renaming /var/log/mysql/mariadb-slow.log.0.gz to /var/log/mysql/mariadb-slow.log.1.gz (rotatecount 7, logstart 1, i 0),
old log /var/log/mysql/mariadb-slow.log.0.gz does not exist
rotating log /var/log/mysql/error.log, log->rotateCount is 7
dateext suffix '-20171115'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/mysql/error.log.7.gz to /var/log/mysql/error.log.8.gz (rotatecount 7, logstart 1, i 7),
renaming /var/log/mysql/error.log.6.gz to /var/log/mysql/error.log.7.gz (rotatecount 7, logstart 1, i 6),
renaming /var/log/mysql/error.log.5.gz to /var/log/mysql/error.log.6.gz (rotatecount 7, logstart 1, i 5),
renaming /var/log/mysql/error.log.4.gz to /var/log/mysql/error.log.5.gz (rotatecount 7, logstart 1, i 4),
renaming /var/log/mysql/error.log.3.gz to /var/log/mysql/error.log.4.gz (rotatecount 7, logstart 1, i 3),
renaming /var/log/mysql/error.log.2.gz to /var/log/mysql/error.log.3.gz (rotatecount 7, logstart 1, i 2),
renaming /var/log/mysql/error.log.1.gz to /var/log/mysql/error.log.2.gz (rotatecount 7, logstart 1, i 1),
renaming /var/log/mysql/error.log.0.gz to /var/log/mysql/error.log.1.gz (rotatecount 7, logstart 1, i 0),
old log /var/log/mysql/error.log.0.gz does not exist
renaming /var/log/mysql/mariadb-slow.log to /var/log/mysql/mariadb-slow.log.1
creating new /var/log/mysql/mariadb-slow.log mode = 0640 uid = 110 gid = 4
renaming /var/log/mysql/error.log to /var/log/mysql/error.log.1
creating new /var/log/mysql/error.log mode = 0640 uid = 110 gid = 4
running postrotate script
compressing log with: /bin/gzip
removing old log /var/log/mysql/mariadb-slow.log.8.gz
compressing log with: /bin/gzip
removing old log /var/log/mysql/error.log.8.gz
logrotate 配置文件来自软件包维护者。我没有做任何修改。我还尝试了一些其他的东西。
/var/log/mysql/mysql.log /var/log/mysql/mysql-slow.log /var/log/mysql/mariadb-slow.log /var/log/mysql/error.log
# daily
size 1k
rotate 7
missingok
create 640 mysql adm
compress
sharedscripts
postrotate
test -x /usr/bin/mysqladmin || exit 0
if [ -f `my_print_defaults --mysqld | grep -m 1 -oP "pid-file=\K.+$"` ]; then
# If this fails, check debian.conf!
mysqladmin --defaults-file=/etc/mysql/debian.cnf -v --local flush-error-log \
flush-engine-log flush-general-log flush-slow-log >> /var/log/mysql-logrotate.log 2>&1
fi
endscript
了解了这个问题后,我刚刚添加了
>> /var/log/mysql-logrotate.log 2>&1
但是 mysql-logrotate.log 保持为空。
使用的软件: 操作系统:Debian 9.1 数据库:MariaDB 10.1 日志旋转:3.11.0
【问题讨论】:
【参考方案1】:好的,当我在 mysqladmin 命令之前将以下内容放入 postrotate 时
test -x /usr/bin/mysql || exit 0
mysql --defaults-file=/etc/mysql/debian.cnf -e "FLUSH SLOW LOGS;"
mariadb-slow.log 被轮换并且可以在轮换后写入。
【讨论】:
以上是关于在 logrotate 之后不写 MariaDB 慢日志的主要内容,如果未能解决你的问题,请参考以下文章
debian apach2 wsgi 自定义log logrotate 之后无权限访问