在MySQL 8.0中,默认的二进制日志的保存时间参数binlog_expire_logs_seconds和expire_logs_days的设置如下
mysql> show variables like '%expire_log%'; +----------------------------+---------+ | Variable_name | Value | +----------------------------+---------+ | binlog_expire_logs_seconds | 2592000 | | expire_logs_days | 0 | +----------------------------+---------+ 2 rows in set (0.01 sec) mysql> select @@version; +-----------+ | @@version | +-----------+ | 8.0.26 | +-----------+ 1 row in set (0.00 sec)
2592000是30天。binlog_expire_logs_seconds是8.0里面新增的,优先级比expire_logs_days高,5.7里的默认设置如下:
Server version: 5.7.33-0ubuntu0.16.04.1-log (Ubuntu) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show variables like '%expire%'; +--------------------------------+-------+ | Variable_name | Value | +--------------------------------+-------+ | disconnect_on_expired_password | ON | | expire_logs_days | 0 | +--------------------------------+-------+ 2 rows in set (0.02 sec)
在5.7里面需要设置一个过期时间,不然会一直涨下去,例如可以使用下面的命令进行设置:
mysql> set global expire_logs_days=30; Query OK, 0 rows affected (0.00 sec)
这样设置后,过期的binlog并不会马上被删除,在下面三种情况下会被删除:
MySQL启动
发出flush logs的命令
binlog达到最大值(通常是1G),切换时。