Let me help you solve the problem of MySQL binlog files taking up too much space. Here are a few options to deal with it:
-
Clean up old binlog files
-- 查看当前的 binlog 文件列表 SHOW BINARY LOGS; -- 手动删除指定日期之前的 binlog PURGE BINARY LOGS BEFORE DATE_SUB(NOW(), INTERVAL 7 DAY); -- 或者删除指定文件之前的所有日志 PURGE BINARY LOGS TO 'mysql-bin.000123';
-
Setting the binlog auto cleanup policy
-- 设置 binlog 保留天数(比如保留7天) SET GLOBAL expire_logs_days = 7; -- MySQL 8.0之后使用以下命令 SET GLOBAL binlog_expire_logs_seconds = 604800; -- 7天 = 7*24*60*60秒
-
Modify configuration file optimization
在my.cnf
或my.ini
Add in:# 设置 binlog 过期时间(天) expire_logs_days = 7 # MySQL 8.0+ 使用 binlog_expire_logs_seconds = 604800 # 设置单个 binlog 文件的最大值(默认1G) max_binlog_size = 100M
Recommended steps:
- Back up important data first
- Check if binlog is really needed (if you don’t need the replication feature, consider turning it off)
- Set a reasonable expiration time
- Regularly monitor binlog space usage
Caveats:
- Ensure that the relevant slave libraries have been synchronized before cleaning up
- Do not delete the binlog file in use
- Recommended for cleanup during low peak business periods
- Consider storing the binlog on a separate high-capacity disk
If you only need to clear space temporarily, use thePURGE BINARY LOGS
is the fastest way. However, in the long run, it is recommended to set up an automatic expiration policy at the same time to avoid excessive space usage again.