說明:
開啟MySQL binlog日志的服務器,如果不設置自動清理日志,默認binlog日志一直保留著,時間一長,服務器磁盤空間被binlog日志占滿,導致MySQL數據庫出錯。
使用下面方法可以安全清理binlog日志
一、沒有主從同步的情況下清理日志
mysql -uroot -p123456 -e 'PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ),INTERVAL 5 DAY)';
#mysql 定時清理5天前的binlog
mysql -u root -p #進入mysql 控制臺
reset master; #重置binlog
二、MySQL主從同步下安全清理binlog日志
1、mysql -u root -p #進入從服務器mysql控制臺
show slave status/G; #檢查從服務器正在讀取哪個日志,有多個從服務器,選擇時間最早的一個做為目標日志。
2、進入主服務器mysql控制臺
show master log; #獲得主服務器上的一系列日志
PURGE MASTER LOGS TO 'binlog.000058'; #刪除binlog.000005之前的,不包括binlog.000058
PURGE MASTER LOGS BEFORE '2016-06-22 13:00:00'; #清除2016-06-22 13:00:00前binlog日志
PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 3 DAY); #清除3天前binlog日志
三、設置自動清理MySQL binlog日志
vi /etc/my.cnf #編輯配置
| expire_logs_days = 15 #自動刪除15天前的日志。默認值為0,表示從不刪除。log-bin=mysql-bin #注釋掉之后,會關閉binlog日志binlog_format=mixed #注釋掉之后,會關閉binlog日志 |
:wq! #保存退出
擴展閱讀:
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.5/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.5/en/purge-binary-logs.html
Examples:
PURGE BINARY LOGS TO 'mysql-bin.010';
PURGE BINARY LOGS BEFORE '2008-04-02 22:46:26';
下面是其它網友給出的方法,大家可以參考一下
MYSQL主從復制(replication)采用 RBR 模式后,binlog的格式為"ROW",能解決很多原先出現的主鍵重復問題。
在一個繁忙的master db server上,binlog日志文件增長速度很快,如果不定時清除,硬盤空間很快就會被充滿。
新聞熱點
疑難解答