MySQL清理binlog的正確方式

本位主要講述如何正確的清理 MySQL的binlog,裏面有哪些坑,注意點有什麼。

 

一、 爲什麼要清理binlog 

     如果沒有設置MySQL的binlog過期時間或者設置的時間過長, 會導致磁盤容量報警,當磁盤100%時時非常危險,服務會不可用,所以監控一定要做好,

磁盤容量不足時候可以清理之前的binlog來釋放磁盤空間。

 

二、 如何清理binlog

     清理binlog有兩個維度進行清理, 一個是根據時間的維度,一個是清理某個binlog文件之前的,具體指令如下:

mysql> help purge
Name: 'PURGE BINARY LOGS'
Description:
Syntax:
PURGE { BINARY | MASTER } LOGS
    { TO 'log_name' | BEFORE datetime_expr }


The binary log is a set of files that contain information about data
modifications made by the MySQL server. The log consists of a set of
binary log files, plus an index file (see
http://dev.mysql.com/doc/refman/5.7/en/binary-log.html).


The PURGE BINARY LOGS statement deletes all the binary log files listed
in the log index file prior to the specified log file name or date.
BINARY and MASTER are synonyms. Deleted log files also are removed from
the list recorded in the index file, so that the given log file becomes
the first in the list.


This statement has no effect if the server was not started with the
--log-bin option to enable binary logging.


URL: http://dev.mysql.com/doc/refman/5.7/en/purge-binary-logs.html


Examples:
PURGE BINARY LOGS TO 'mysql-bin.010';
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 保存七天時間,可以根據自己的環境要求自己定義:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章