標籤:設定檔 purge mysql
1.手動清除binlog檔案
理論上,應該在設定檔/etc/my.cnf中加上binlog到期時間的配置項,expire_logs_days = 10.但是如果沒有加這一項,隨著產生越來越多的binlog,磁碟被吃掉了不少。可以直接刪除binlog檔案,但是可以通過mysql提供的工具來刪除更安全。因為purge會更新mysql-bin.index中的條目,而直接刪除的話,mysql-bin.index檔案不會更新。mysql-bin.index的作用是加快尋找binlog檔案的速度。
先help一下吧650) this.width=650;" src="http://img.baidu.com/hi/jx2/j_0020.gif" alt="j_0020.gif" />:
mysql> help purge
Name: ‘PURGE MASTER LOGS‘
Description:
Syntax:
PURGE {MASTER | BINARY} LOGS TO ‘log_name‘
PURGE {MASTER | BINARY} LOGS BEFORE ‘date‘
Deletes all the binary logs listed in the log index prior to the
specified log or date. The logs also are removed from the list recorded
in the log index file, so that the given log becomes the first.
This statement has no effect if the --log-bin option has not been
enabled.
URL: http://dev.mysql.com/doc/refman/5.0/en/purge-master-logs.html
Examples:
PURGE MASTER LOGS TO ‘mysql-bin.010‘;
PURGE MASTER LOGS BEFORE ‘2003-04-02 22:46:26‘;
兩種方法都可用。第一個是刪除至某一個檔案為止,第二個是刪除到某個日期為止。
比如我們刪除2017-05-31之前的log,可以這樣
mysql>PURGE MASTER LOGS BEFORE ‘2017-05-31 00:00:00‘;
2.設定expire_logs_days
# vim /etc/my.cnf //修改expire_logs_days,x是自動刪除的天數,一般將x設定為短點,如10
expire_logs_days = x //二進位日誌自動刪除的天數。預設值為0,表示“沒有自動刪除”
此方法需要重啟mysql,附錄有關於expire_logs_days的英文說明
當然也可以不重啟mysql,開啟mysql主從,直接在mysql裡設定expire_logs_days
> show binary logs;
> show variables like ‘%log%‘;
> set global expire_logs_days = 10;
需要注意的是:最好到slave上面去看下當前同步到那個binlog檔案了,用show slave status查看。否則,master上刪多了的話,就造成slave缺失記錄檔而導致資料不一致了。
本文出自 “夢Dream” 部落格,請務必保留此出處http://dreamy.blog.51cto.com/12471447/1936959
mysql的binlog安全刪除