SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000795 | 123456 | | | +------------------+----------+--------------+------------------+
SHOW SLAVE STATUS\G Master_Log_File: mysql-bin.000790 Read_Master_Log_Pos: 98765 Exec_Master_Log_Pos: 98765
確保所有 Slave 的 Exec_Master_Log_Pos 已超過將要刪除的檔案位置。
mysql-bin.000795 之前的所有 binlog)
PURGE BINARY LOGS TO 'mysql-bin.000795';
SHOW BINARY LOGS; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000795 | 12345678 | | mysql-bin.000796 | 5678901 | +------------------+-----------+
[mysqld] expire_logs_days = 7 -- 自動清理 7 天前的 binlog
動態生效:SET GLOBAL expire_logs_days = 7;
/var/lib/mysql/mysql-bin.* 檔案,會導致 mysql-bin.index 索引不一致,引發啟動失誤。PURGE BINARY LOGS TO 會同步更新 index 檔案,保證一致性。gtid_purged 不會遺漏 Slave 所需事務。