說明:
當開啓mysql數據庫主從時,會產生大量如mysql-bin.00000* log的文件,這會大量耗費您的硬盤空間。
mysql-bin.000001
mysql-bin.000002
mysql-bin.000003
mysql-bin.000004
mysql-bin.000005
…
有三種解決方法:1.關閉mysql主從,關閉binlog;2.開啓mysql主從,設置expire_logs_days;3.手動清除binlog文件,> PURGE MASTER LOGS TO ‘MySQL-bin.010′;
實現:
1.關閉mysql主從,關閉binlog
# vim /etc/my.cnf //註釋掉log-bin,binlog_format# Replication Master Server (default)# binary logging is required for replication# log-bin=mysql-bin# binary logging format - mixed recommended# binlog_format=mixed而後重啓數據庫
2.重啓mysql,開啓mysql主從,設置expire_logs_days
# vim /etc/my.cnf //修改expire_logs_days,x是自動刪除的天數,通常將x設置爲短點,如10expire_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;
3.手動清除binlog文件
# /usr/local/mysql/bin/mysql -u root -p> PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY); //刪除10天前的MySQL binlog日誌,附錄2有關於PURGE MASTER LOGS手動刪除用法及示例> show master logs;
也能夠重置master,刪除全部binlog文件:
# /usr/local/mysql/bin/mysql -u root -p> reset master; //附錄3有清除binlog時,對從mysql的影響說明mysql
附錄:
1.expire_logs_days英文說明
Where X is the number of days you’d like to keep them around. I would recommend 10, but this depends on how busy your MySQL server is and how fast these log files grow. Just make sure it is longer than the slowest slave takes to replicate the data from your master.
Just a side note: You know that you should do this anyway, but make sure you back up your mysql database. The binary log can be used to recover the database in certain situations; so having a backup ensures that if your database server does crash, you will be able to recover the data.
2.PURGE MASTER LOGS手動刪除用法及示例,MASTER和BINARY是同義詞
> PURGE {MASTER | BINARY} LOGS TO 'log_name'> PURGE {MASTER | BINARY} LOGS BEFORE 'date'刪除指定的日誌或日期以前的日誌索引中的全部二進制日誌。這些日誌也會從記錄在日誌索引文件中的清單中被刪除MySQL BIN-LOG 日誌,這樣被給定的日誌成爲第一個。
實例:
> PURGE MASTER LOGS TO 'MySQL-bin.010'; //清除MySQL-bin.010日誌> PURGE MASTER LOGS BEFORE '2008-06-22 13:00:00'; //清除2008-06-22 13:00:00前binlog日誌> PURGE MASTER LOGS BEFORE DATE_SUB( NOW, INTERVAL 3 DAY); //清除3天前binlog日誌BEFORE,變量的date自變量能夠爲'YYYY-MM-DD hh:mm:ss'格式。
3.清除binlog時,對從mysql的影響
若是您有一個活性的從屬服務器,該服務器當前正在讀取您正在試圖刪除的日誌之一,則本語句不會起做用,而是會失敗,並伴隨一個錯誤。不過,若是從屬服務器是休止的,而且您碰巧清理了其想要讀取的日誌之一,則從屬服務器啓動後不能複製。當從屬服務器正在複製時,本語句能夠安全運行。您不須要中止它們。sql