找出服务器硬盘空间被大量占用的真凶

1、mysql-bin文件

有一台服务器,今天登录上去一看,发现硬盘空间突然被大量占用了。之前只用了不到8G,今天发现陡增到38G,足足多了30G。

先用以下命令,依次查看硬盘占用前5的文件夹:

du -hm / --max-depth=1 | sort -nr | head -5

du -hm /xxxdir --max-depth=1 | sort -nr | head -5

最后发现是/www/server/data目录下,有很多mysql-bin-000000 数字文件,每个文件大小1G,合在一起占用了产不多30G空间。

在MySQL数据库中,mysql-bin.000001、mysql- bin.000002等文件是数据库的操作日志,例如UPDATE一个表,或者DELETE一些数据,即使该语句没有匹配的数据,这个命令也会存储到日志文件中,还包括每个语句执行的时间,也会记录进去的。

这样做的目的是为了数据库恢复,或者主从服务器之间同步数据。

我不需要,所以直接注释掉数据库配置文件my.cnf中以下一行即可:

# log-bin=mysql-bin

删除所有 mysql-bin.00000 文件,重启mysql。

updtae:最好不要直接删除mysql-bin.文件,找到英文的解释。

简单来说是:不要直接删除,用mysqld来删除,可以通过修改/etc/my.cnf来设置数据库日志保存时间。

Please do not just delete them in the OS.

You need to let mysqld do that for you. Here is how mysqld manages it:

The file mysql-bin.[index] keeps a list of all binary logs mysqld has generated and auto-rotated. The mechanisms for cleaning out the binlogs in conjunction with mysql-bin.[index] are:

PURGE BINARY LOGS TO 'binlogname';
PURGE BINARY LOGS BEFORE 'datetimestamp';

These will clear all binary logs before the binlog or timestamp you just specified.

For example, if you run

PURGE BINARY LOGS TO 'mysql-bin.000223';

this will erase all binary logs before mysql-bin.000223.

If you run

PURGE BINARY LOGS BEFORE DATE(NOW() - INTERVAL 3 DAY) + INTERVAL 0 SECOND;

this will erase all binary logs before midnight 3 days ago.

If you want to have binlog rotated away automatically and keep 3 days woth, simply set this:

mysql> SET GLOBAL expire_logs_days = 3;

then add this to /etc/my.cnf

[mysqld]
expire_logs_days=3

and mysqld will delete them logs for you

2、var/log/journal日志文件

系统日志文件,也会占用越来越多的系统空间。

清理所有日志,但是保留最近3天的:

sudo journalctl --vacuum-time=3d

设定journal日志文件不超过100M:

sudo journalctl --vacuum-size=100M

这样就不需要经常清理journal日志了。

update: 早上起来,发现硬盘空间又多了4G,真头疼,继续找。。。

这次发现是/www/server/data/168itw.com目录下wp_wpr_rucss_resources.ibd和 wp_wpr_rucss_unused.ibd两个文件占用了几个G,而且 wp_wpr_rucss_unused.ibd还在一直增大。

Google看到这篇文章:

https://stackoverflow.com/questions/68347966/wp-wpr-rucss-resources-table-with-huge-size

原来是wordpress插件wp rocket惹的祸,不要勾选插件中Optimize CSS delivery选项,保存。

删除上述两个文件,删除数据库中的这两个table。搞定!

文章参考:https://blog.51cto.com/6226001001/1597205

感谢hostloc大佬提供查看命令帮助。

本文链接: https://www.168itw.com/tech/mysql-bin/
转载请注明转载自:168itw

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注