我很难让Mysql日志旋转工作。我不知道为什么这件事这么复杂,尽管我听从了文档和互联网的建议,但仍然无法让它发挥作用。我在Mysql上启用了所有3个日志:一般日志、日志错误和慢速查询,在3个文件下:error.log, mysql-slow.log , mysql.log。所有3条日志都正常工作。其中一个文件( mysql.log )每天生成10个Gbs,我希望每2天对该文件进行日志轮转,这意味着我希望始终保存过去2天的数据,并一直循环数据,使其不超过30 Gbs的数据,因此它看起来会像这样(我修改了输出,给出了我想要的示例):
ls -lh
-rw-r----- 1 mysql mysql 9.1G Apr 18 06:30 mysql.log
-rw-r----- 1 mysql mysql 9.3G Apr 17 06:28 mysql.log-20210417.backup
-rw-r----- 1 mysql mysql 9.0G Apr 16 06:28 mysql.log-20210416.backup首先,我尝试修改文件/etc/logrotate.conf,如下所示:
# see "man logrotate" for details
# rotate log files weekly
weekly
# use the syslog group by default, since this is the owning group
# of /var/log/syslog.
su root syslog
# 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
# packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
missingok
monthly
create 0664 root utmp
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0660 root utmp
rotate 1
}
# system-specific logs may be configured here
/var/log/mysql/mysql.log {
su mysql mysql
create 600 mysql mysql
notifempty
daily
rotate 2
missingok
compress
copytruncate
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
}我只是在文件的末尾添加了以/var/log/mysql/mysql.log {开头的部分,然后在/etc/logrotate.d/目录中创建了文件mysql,并用以/var/log/mysql/mysql.log {开头的部分更新了文件。
这两个方法都失败(使用/etc/logrotation.conf和/etc/logrotate.d/mysql文件),文件日志不断堆积超过2天:
total 52G
-rw-r----- 1 mysql mysql 13K Apr 16 08:41 error.log
-rw-r----- 1 mysql mysql 140 Apr 1 06:25 error.log.1
-rw-r----- 1 mysql mysql 5.7G Apr 18 09:37 mysql-slow.log
-rw-r----- 1 mysql mysql 114K Apr 18 19:24 mysql.log
-rw-r----- 1 mysql mysql 9.1G Apr 18 06:30 mysql.log.1
-rw-r----- 1 mysql mysql 9.3G Apr 14 06:28 mysql.log.1-2021041506.backup
-rw-r----- 1 mysql mysql 9.0G Apr 15 06:28 mysql.log.1-2021041606.backup
-rw-r----- 1 mysql mysql 9.2G Apr 16 06:28 mysql.log.1-2021041706.backup
-rw-r----- 1 mysql mysql 9.1G Apr 17 06:28 mysql.log.1-2021041806.backup此外,我注意到另一个文件的名字后面有"1“:mysql.log.1。真奇怪。
我尝试修改的另一件事是/etc/cron.daily/logrotate文件:我尝试了2个脚本:
#!/bin/sh
# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
[ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.d/mysql和
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.d/mysql
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0这两样都不管用。你能帮我提出一些建议/指导方针吗?很抱歉这么长时间,谢谢。
发布于 2021-05-19 13:34:07
解决了。在这种情况下,必须检查所有相关脚本: /etc/logrotate.conf、/etc/cron.day/log旋转和/etc/logrotate.d/mysql。
对我来说,问题是在/etc/logrotate.d/mysql中,对于刷新日志,用户不允许运行该命令,
https://askubuntu.com/questions/1334354
复制相似问题