目录
一. 在线修改,即时生效
mysql> show variables like '%expire%';
+--------------------------------+---------+
| Variable_name | Value |
+--------------------------------+---------+
| binlog_expire_logs_auto_purge | ON |
| binlog_expire_logs_seconds | 2592000 |
| disconnect_on_expired_password | ON |
| expire_logs_days | 0 |
+--------------------------------+---------+
4 rows in set (0.02 sec)
mysql> set global expire_logs_days=1;
ERROR 3683 (HY000): The option expire_logs_days and binlog_expire_logs_seconds cannot be used together.
Please use binlog_expire_logs_seconds to set the expire time (expire_logs_days is deprecated)
mysql> set global binlog_expire_logs_seconds=86400;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%expire%';
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| binlog_expire_logs_auto_purge | ON |
| binlog_expire_logs_seconds | 86400 |
| disconnect_on_expired_password | ON |
| expire_logs_days | 0 |
+--------------------------------+-------+
4 rows in set (0.02 sec)
mysql> flush logs;
Query OK, 0 rows affected (2.77 sec)
mysql> show binary logs;
+---------------+------------+-----------+
| Log_name | File_size | Encrypted |
+---------------+------------+-----------+
| binlog.000034 | 1150850956 | No |
| binlog.000035 | 1073745962 | No |
| binlog.000036 | 1073765209 | No |
| binlog.000037 | 1036964608 | No |
| binlog.000038 | 1105127830 | No |
| binlog.000039 | 1136391108 | No |
| binlog.000040 | 1112828317 | No |
| binlog.000041 | 1109221590 | No |
| binlog.000042 | 165709537 | No |
| binlog.000043 | 955575 | No |
+---------------+------------+-----------+
10 rows in set (0.00 sec)
mysql> \q
查看binlog具体设置
查看binlog设置
mysql> show variables like '%expire%';
注意:mysql8.0以下版本binlog保存时效 以天为单位,参数为expire_logs_days,默认0为永不过期。
mysql8.0以上版本binlog保存时效 以秒为单位,参数为binlog_expire_logs_seconds,默认的binlog过期时间为2592000秒,也就是30天。
示例设置过期日期为1天(1d=86400s):
mysql> set global binlog_expire_logs_seconds=86400;
清除日志
1.手动刷新清理过期日志
mysql> flush logs;
2.清除某个日志:
mysql> purge binary logs to 'binlog.000011';
3.清除某日前的日志:
mysql> purge binary logs before '2020-12-20';
注意:不要轻易手动去删除binlog,会导致binlog.index和真实存在的binlog不匹配,而导致expire_logs_day失效。
二.修改配置,重启生效
在配置文件 my.cnf 中修改参数如下:
[mysqld]
expire_logs_days=1 #日志过期时间为1天,若expire_logs_days=0,则为永不过期哦
max_binlog_size=500M #日志最多存放500M,超过500M后会被清除
mysql服务重启后配置生效