Mariadb的binlog就是无法自动清理(>=11.4)


注意:本文描述的问题(以及解决方法)只会出现在mariadb >= 11.4的版本里

以前就发现了这个问题,但以前的服务器硬盘比较大,所以一直没怎么管这个问题

总之,可以描述如下:

按我的设置来说mariadb应该只会保留1天的binlog,但检查硬盘会发现那些过期了很多天的binlog还保存着,而且它们的最近修改时间都是几天前(说明mariadb确实没有在用这些binlog)。但这些binlog就是无法通过日期/自动删除的机制删掉,只能通过指定binlog名字的方法一个一个手动删

MariaDB [(none)]> SHOW VARIABLES LIKE 'expire_logs_days';
+------------------+----------+
| Variable_name    | Value    |
+------------------+----------+
| expire_logs_days | 1.000000 |
+------------------+----------+



MariaDB [(none)]> show binary logs;
+------------------+------------+
| Log_name         | File_size  |
+------------------+------------+
| mysql-bin.000001 |        351 |
| mysql-bin.000002 |        351 |
| mysql-bin.000003 |        351 |
| mysql-bin.000004 | 1073911886 |
| mysql-bin.000005 | 1074748360 |
| mysql-bin.000006 |  202306200 |
| mysql-bin.000007 |   58956397 |
| mysql-bin.000008 |  191344926 |
| mysql-bin.000009 |       6016 |
| mysql-bin.000010 |       6426 |
+------------------+------------+
10 rows in set (0.000 sec)

# mysql-bin.000007之前的日志应该都是过期的,按理来说就不应该还留在硬盘上




MariaDB [(none)]> SHOW VARIABLES LIKE 'binlog_expire_logs_seconds';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| binlog_expire_logs_seconds | 86400 |
+----------------------------+-------+

# 再次确认过期时间确实是1天



MariaDB [(none)]> PURGE BINARY LOGS BEFORE '2025-11-05';
Query OK, 0 rows affected, 1 warning (0.002 sec)

# 这一步按理来说可以清理掉mysql-bin.000007之前的所有bin log,然而无事发生




MariaDB [(none)]> SHOW WARNINGS;
+-------+------+-------------------------------------------------------------------------------------+
| Level | Code | Message                                                                             |
+-------+------+-------------------------------------------------------------------------------------+
| Note  | 1375 | Binary log 'mysql-bin.000001' is not purged because it is the current active binlog |
+-------+------+-------------------------------------------------------------------------------------+




MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000010 |     7538 |              |                  |
+------------------+----------+--------------+------------------+

# 我明明在用mysql-bin.000010,为什么之前的binlog不让我删?

不应该呀,明明我早就不用mysql-bin.000001~mysql-bin.000007了,而且这些日志文件在硬盘上也过期很多天了...

和gpt/gemini大战了很久很久,无果

最后还是reddit好用:🔗 [After the latest update MariaDB (11.4.2-1) does not purge binary logs : r/archlinux] https://www.reddit.com/r/archlinux/comments/1e1sa1b/after_the_latest_update_mariadb_11421_does_not/ 

reddit

实际上如果查看mariadb.err日志也可以发现这一点:

[Note] Binary log 'mysql-bin.000001' is not purged because less than 'slave_connections_needed_for_purge' slaves have processed it

但systemctl restart mariadb.service的时候这行日志不会影响重启也不会显示,所以一直没发现

所以解决问题就很简单了(我的mariadb只在一台服务器上独立运行,没有主从复制)

直接在my.cnf里面加入

slave_connections_needed_for_purge = 0

然后重启mariadb,就好了

Leave a Comment Anonymous comment is allowed / 允许匿名评论