本位主要講述如何正確的清理 MySQL的binlog,里面有哪些坑,注意點有什么。
一、 為什么要清理binlog
如果沒有設置MySQL的binlog過期時間或者設置的時間過長, 會導致磁盤容量報警,當磁盤100%時時非常危險,服務會不可用,所以監控一定要做好,
磁盤容量不足時候可以清理之前的binlog來釋放磁盤空間。
二、 如何清理binlog
清理binlog有兩個維度進行清理, 一個是根據時間的維度,一個是清理某個binlog文件之前的,具體指令如下:
可以通過 help purge 查看此指令的介紹:
1 mysql> help purge 2 Name: 'PURGE BINARY LOGS' 3 Description: 4 Syntax: 5 PURGE { BINARY | MASTER } LOGS 6 { TO 'log_name' | BEFORE datetime_expr } 7 8 9 The binary log is a set of files that contain information about data 10 modifications made by the MySQL server. The log consists of a set of 11 binary log files, plus an index file (see 12 http://dev.mysql.com/doc/refman/5.7/en/binary-log.html). 13 14 15 The PURGE BINARY LOGS statement deletes all the binary log files listed 16 in the log index file prior to the specified log file name or date. 17 BINARY and MASTER are synonyms. Deleted log files also are removed from 18 the list recorded in the index file, so that the given log file becomes 19 the first in the list. 20 21 22 This statement has no effect if the server was not started with the 23 --log-bin option to enable binary logging. 24 25 26 URL: http://dev.mysql.com/doc/refman/5.7/en/purge-binary-logs.html 27 28 29 Examples: 30 PURGE BINARY LOGS TO 'mysql-bin.010'; 31 PURGE BINARY LOGS BEFORE '2008-04-02 22:46:26';
三、如何查看binlog
查看binlog可以查看日志目錄里面, 也可以通過mysql 指令來查看:
SHOW BINARY LOGS;
四、如何才算正確的清理(⚠️避坑)
- 首先這個節點最好不是主從結構中的主庫角色,如果是主庫角色,則建議一定要保證清理的是從庫已經讀取完畢的binlog文件,否則從庫異常。
- 錯誤的清理binlog方式: reset master; 危險,絕對不要在生產環境里面使用,特別是主庫,否則所有從庫全部廢掉
- 建議搭建單獨的binlog server 來存儲binlog
- 設置參數讓mysql 自動的清理過期的binlog,一般binlog 保存七天時間,可以根據自己的環境要求自己定義:
-