二進制日誌BINARY LOG清理

 

mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       126 |
| mysql-bin.000002 |       150 |
| mysql-bin.000003 |       150 |
| mysql-bin.000004 |       150 |
| mysql-bin.000005 |       107 |
+------------------+-----------+
5 rows in set (0.00 sec)

刪除列於指定日誌以前的全部日誌,但不包括指定的日誌:mysql

mysql> purge binary logs to 'mysql-bin.000002';
Query OK, 0 rows affected (0.10 sec)

mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000002 |       150 |
| mysql-bin.000003 |       150 |
| mysql-bin.000004 |       150 |
| mysql-bin.000005 |       107 |
+------------------+-----------+
4 rows in set (0.00 sec)

查看binlog是否開啓:sql

mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 |      368 | test,test2   |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

刪除2016-08-22 16:00:00時間點以前的日誌:安全

mysql> purge binary logs before '2016-08-22 16:00:00';
Query OK, 0 rows affected (0.16 sec)

mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000007 |       368 |
+------------------+-----------+
1 row in set (0.00 sec)

注意事項:架構

        在刪除binlog日誌同時,也會清理MySQL-bin.index的文件記錄,清理完後命令中指定的日誌文件成爲第一個。測試

[root@DB-S data]# cat mysql-bin.index 
./mysql-bin.000007

主從架構下,若是複製正在進行中,執行該命令是安全的,例如slave正在讀取咱們要刪除的log,該語句將什麼也不會作,並返回一個錯誤;若是複製是中止的,咱們刪除了一個slave還未讀取的日誌,則複製從新創建鏈接時將會失敗。網站

指定過時天數(expire_logs_days):該參數爲全局動態調整參數,默認值爲0,即關閉,取值範圍0-99;

mysql> SHOW VARIABLES LIKE 'expire_logs_days';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 0     |
+------------------+-------+
1 row in set (0.00 sec)

這裏我設置過時天數爲:一週spa

mysql> set global expire_logs_days = 7;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'expire_logs_days';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 7     |
+------------------+-------+
1 row in set (0.01 sec)

注意:在雙機複製環境下,應確保過時天數不該小於從機追趕主機binlog日誌的時間。.net

觸發過時刪除的條件:日誌

1重啓MYSQL;code

2BINLOG文件大小達到參數max_binlog_size限制;

3手動執行命令。

重置binlog(reset master)

mysql> reset master;
Query OK, 0 rows affected (0.26 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      107 | test,test2   |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> system cat mysql-bin.index
./mysql-bin.000001

該方法能夠刪除列於索引文件中的全部二進制日誌,把二進制日誌索引文件從新設置爲空,並建立一個以.000001爲後綴新的二進制日製文件,並刪除,過去生成的二進制日誌,以下:

mysql> system ls
ceshi      DB-S-relay-bin.000104  DB-S-relay-bin.000108    ib_logfile0  mysql-bin.000001     sx
chenlin   DB-S-relay-bin.000105  DB-S-relay-bin.000109    ib_logfile1  mysql-bin.index     test
DB-S.err  DB-S-relay-bin.000106  DB-S-relay-bin.index    master.info  performance_schema  test2
DB-S.pid  DB-S-relay-bin.000107  ibdata1        mysql         relay-log.info
mysql> flush logs;
Query OK, 0 rows affected (0.24 sec)

mysql> system ls
ceshi               DB-S-relay-bin.000107  ib_logfile1      relay-log.info
chenlin               DB-S-relay-bin.000108  master.info      sx
DB-S.err           DB-S-relay-bin.000109  mysql          test
DB-S.pid           DB-S-relay-bin.000110  mysql-bin.000001      test2
DB-S-relay-bin.000104  DB-S-relay-bin.index   mysql-bin.000002
DB-S-relay-bin.000105  ibdata1              mysql-bin.index
DB-S-relay-bin.000106  ib_logfile0          performance_schema
mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       150 |
| mysql-bin.000002 |       107 |
+------------------+-----------+
2 rows in set (0.00 sec)

mysql> reset master;
Query OK, 0 rows affected (0.19 sec)

mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       107 |
+------------------+-----------+
1 row in set (0.00 sec)

mysql> system ls
ceshi               DB-S-relay-bin.000106  ibdata1        mysql-bin.index
chenlin               DB-S-relay-bin.000107  ib_logfile0    performance_schema
DB-S.err           DB-S-relay-bin.000108  ib_logfile1    relay-log.info
DB-S.pid           DB-S-relay-bin.000109  master.info    sx
DB-S-relay-bin.000104  DB-S-relay-bin.000110  mysql        test
DB-S-relay-bin.000105  DB-S-relay-bin.index   mysql-bin.000001    test2

該語法通常只用在主從環境下初次創建複製時。     在主從複製進行過程當中,該語句是無效的。

主從環境下的配置步驟:     a. 啓動master和slave,開啓replication(即複製)     b. 在master上運行一些測試的語句,看數據是否可以複製到 slave上面     c. 當複製運行正常的話,就在從上stop slave 而後執行 reset slave,去掉不須要的數據      d. 在master上面執行reset master 清除測試產生的數據

相關文章
相關標籤/搜索