首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mysql日志轮转不起作用

Mysql日志轮转不起作用
EN

Ask Ubuntu用户
提问于 2021-04-27 14:43:45
回答 1查看 737关注 0票数 1

我很难让Mysql日志旋转工作。我不知道为什么这件事这么复杂,尽管我听从了文档和互联网的建议,但仍然无法让它发挥作用。我在Mysql上启用了所有3个日志:一般日志、日志错误和慢速查询,在3个文件下:error.log, mysql-slow.log , mysql.log。所有3条日志都正常工作。其中一个文件( mysql.log )每天生成10个Gbs,我希望每2天对该文件进行日志轮转,这意味着我希望始终保存过去2天的数据,并一直循环数据,使其不超过30 Gbs的数据,因此它看起来会像这样(我修改了输出,给出了我想要的示例):

代码语言:javascript
复制
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,如下所示:

代码语言:javascript
复制
# 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天:

代码语言:javascript
复制
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个脚本:

代码语言:javascript
复制
#!/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

代码语言:javascript
复制
#!/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

这两样都不管用。你能帮我提出一些建议/指导方针吗?很抱歉这么长时间,谢谢。

EN

回答 1

Ask Ubuntu用户

发布于 2021-05-19 13:34:07

解决了。在这种情况下,必须检查所有相关脚本: /etc/logrotate.conf、/etc/cron.day/log旋转和/etc/logrotate.d/mysql。

对我来说,问题是在/etc/logrotate.d/mysql中,对于刷新日志,用户不允许运行该命令,

票数 0
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1334354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档