随着mysql服务运行时间的增加会大量积累二进制文件binlog(记录数据库执行的sql语句),占用大量磁盘空间,所以需要清除binlog文件释放可使用磁盘空间。
SHOW VARIABLES LIKE "%expire_logs%"; 查看当前数据库binlog过期时间设置
SET GLOBAL expire_logs_days=3; 设置当前数据库binlog过期时间为3天
FLUSH LOGS; 刷新日志立刻生效(执行后之前积累点binlog文件会立马删除,同时binlog.index文件会更新记录最新binlog文件名称)
查看当前binlog的信息
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000006 | 201 |
| mysql-bin.000007 | 201 |
| mysql-bin.000008 | 201 |
| mysql-bin.000009 | 201 |
| mysql-bin.000010 | 201 |
| mysql-bin.000011 | 201 |
| mysql-bin.000012 | 201 |
| mysql-bin.000013 | 201 |
| mysql-bin.000014 | 201 |
| mysql-bin.000015 | 201 |
| mysql-bin.000016 | 201 |
| mysql-bin.000017 | 201 |
| mysql-bin.000018 | 201 |
| mysql-bin.000019 | 201 |
| mysql-bin.000020 | 201 |
| mysql-bin.000021 | 154 |
+------------------+-----------+
16 rows in set (0.00 sec)
purge删除binlog
mysql> purge binary logs to 'mysql-bin.000017';
Query OK, 0 rows affected (0.01 sec)
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000017 | 201 |
| mysql-bin.000018 | 201 |
| mysql-bin.000019 | 201 |
| mysql-bin.000020 | 201 |
| mysql-bin.000021 | 154 |
+------------------+-----------+
5 rows in set (0.00 sec)
备注:通过查询结果可以知道,在17之前的日志都被清除了.不包括17本身!
如果在系统手动删除了binlog日志文件,mysql-bin.index不会自动更新binlog索引,则需要使用上述方法进行mysql-bin.index文件的恢复否则数据库重启会报错。
内容参考自:https://www.cnblogs.com/chuanzhang053/p/9336386.html