mysql binlog占用大量磁盘空间的解决方法

来自:网络
时间:2024-08-28
阅读:
免费资源网 - https://freexyz.cn/

查看当前日志保存天数:

mysql> show variables like '%expire_logs_days%';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 0     |
+------------------+-------+
1 row in set (0.08 sec)

默认是0,也就是logs不过期。

也可以使用 show variables like '%expire%' 查看是否有开启日志到期自动删除

mysql> show variables like '%expire%';
+--------------------------------+--------+
| Variable_name                  | Value  |
+--------------------------------+--------+
| binlog_expire_logs_auto_purge  | ON     |
| binlog_expire_logs_seconds     | 432000 |
| disconnect_on_expired_password | ON     |
| expire_logs_days               | 0      |
+--------------------------------+--------+
4 rows in set (0.00 sec)

设置BINLOG到期时间

临时生效,无需重起mysql

如果数据库此时不便重启,可以设置全局的参数,使他临时生效:

mysql> set global expire_logs_days=5;

设置了只保留5天BINLOG, 下次重启mysql这个参数默认会失败。

如果上面指令报错,则改为set global binlog_expire_logs_seconds=432000

mysql> set global expire_logs_days=7;
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=432000;
Query OK, 0 rows affected (0.00 sec)
 
mysql> 
mysql> show variables like '%expire%';
+--------------------------------+--------+
| Variable_name                  | Value  |
+--------------------------------+--------+
| binlog_expire_logs_auto_purge  | ON     |
| binlog_expire_logs_seconds     | 432000 |
| disconnect_on_expired_password | ON     |
| expire_logs_days               | 0      |
+--------------------------------+--------+
4 rows in set (0.00 sec)

写入配置文件,mysql重启依旧生效

如果要永久生效,需要在my.cnf中设置
vi /etc/my.cnf
追加内容:

binlog_expire_logs_seconds=432000

mysql binlog占用大量磁盘空间的解决方法

手动删除BINLOG

也可以先手动删除BINLOG (purge binary logs)

删除指定的日志

mysql> PURGE {MASTER | BINARY} LOGS TO 'log_name'

例:删除binlog.1000

mysql> PURGE MASTER LOGS TO 'binlog.1000';

或删除指定日期之前的日志索引中的所有二进制日志。

mysql> PURGE {MASTER | BINARY} LOGS BEFORE 'date'

例:删除2023-06-03 12:00:00前的文件

mysql> PURGE MASTER LOGS BEFORE '2023-06-03 12:00:00';

例:删除3天前的日志

mysql> PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 3 DAY);
免费资源网 - https://freexyz.cn/
返回顶部
顶部