2019年3月11日 星期一

【MYSQL】刪除binlog的方法





binlog會因為數據庫的不斷運行而越來越大 ,當硬碟空間被占據越大時,可能會導致訪問速度變慢,若是完全被占滿時,就會導致服務卡死,就遇過服務狀態正常,但確無法登入,連重啟也無法!因為磁碟撐爆了~
binlog文件不是直接rm刪除就解決了 !也千萬不要用rm的方式刪除 ,因為刪除後,即使空間釋放了,但 mysql-bin.index 檔案中,仍有記錄(如下)

錯誤方法:查詢mysql data下的binlog檔案記錄有哪些
ll mysql-bin.*

-rw-r—– 1 mysql mysql 2011 Feb 11 10:12 mysql-bin.000017
-rw-r—– 1 mysql mysql 2301 Feb 11 10:42 mysql-bin.000018
-rw-r—– 1 mysql mysql 1201 Feb 12 11:42 mysql-bin.000019
-rw-r—– 1 mysql mysql 2122 Feb 12 12:42 mysql-bin.000020
-rw-r—– 1 mysql mysql 4231 Feb 13 13:45 mysql-bin.000021
-rw-r—– 1 mysql mysql 5401 Feb 13 16:45 mysql-bin.000022
-rw-r—– 1 mysql mysql 2567 Feb 14 19:45 mysql-bin.000023
-rw-r—– 1 mysql mysql 9011 Feb 15 21:45 mysql-bin.000024
-rw-r—– 1 mysql mysql 19 Feb 15 21:46 mysql-bin.index
*******若用rm方式,直接刪除binlog檔案會有什麼問題************
rm -f mysql-bin.000017 mysql-bin.000018 mysql-bin.000019 mysql-bin.000020
ll mysql-bin.*
-rw-r—– 1 mysql mysql 4231 Feb 13 13:45 mysql-bin.000021
-rw-r—– 1 mysql mysql 5401 Feb 13 16:45 mysql-bin.000022
-rw-r—– 1 mysql mysql 2567 Feb 14 19:45 mysql-bin.000023
-rw-r—– 1 mysql mysql 9011 Feb 15 21:45 mysql-bin.000024
-rw-r—– 1 mysql mysql 19 Feb 15 21:46 mysql-bin.index
雖然資料已刪除了 ,但進入MYSQL查詢binlog的記錄檔會發現有誤
mysql> show binary logs;
+———————+—————-+
| Log_name | File_size        |
+———————+—————-+
| mysql-bin.000017 | 0         |
| mysql-bin.000018 | 0         |
| mysql-bin.000019 | 0         |
| mysql-bin.000020 | 0         |
| mysql-bin.000021 | 4231         |
| mysql-bin.000022 | 5401            |
| mysql-bin.000023 | 2567            |
| mysql-bin.000024 | 9011             |
+———————+—————–+
8 rows in set (0.00 sec)
在DB的記錄內,binlog還是檔案名還是存在 ,但size變0,因為在檔案index中記錄中binlog 的信息,但一旦重啟(仍可正常啟動),error.log會報錯
/usr/local/mysql/bin/mysqld: File ‘./mysql-bin.000020’ not found (Errcode: 2 – No such file or directory)2019-02-15 16:12:00 20593 [ERROR] Failed to open log (file ‘./mysql-bin.000020’, errno 2)
2019-02-15 16:12:00 20593 [ERROR] Could not open log file
/usr/local/mysql/bin/mysqld: File ‘./mysql-bin.000019’ not found (Errcode: 2 – No such file or directory)
2019-02-15 16:12:00 20593 [ERROR] Failed to open log (file ‘./mysql-bin.000019’, errno 2)
2019-02-15 16:12:00 20593 [ERROR] Could not open log file
/usr/local/mysql/bin/mysqld: File ‘./mysql-bin.000018’ not found (Errcode: 2 – No such file or directory)
2019-02-15 16:12:00 20593 [ERROR] Failed to open log (file ‘./mysql-bin.000018’, errno 2)
2019-02-15 16:12:00 20593 [ERROR] Could not open log file
/usr/local/mysql/bin/mysqld: File ‘./mysql-bin.000017’ not found (Errcode: 2 – No such file or directory)
2019-02-15 16:12:00 20593 [ERROR] Failed to open log (file ‘./mysql-bin.000017’, errno 2)
2019-02-15 16:12:00 20593 [ERROR] Could not open log file
萬一真的不小心直接刪除了 ,解決error.log報錯的方法就如下
vi mysql-bin.index
編輯mysql-bin.index,然後將刪除的檔案號碼
mysql-bin.000017 到 mysql-bin.000020的記錄刪除!(再重啟即可)

**當然以上的方法是不正確的做法,接下來要教正確的步驟!!
正確方法1.先查看my.cnf的binlog存放天數
cat /etc/my.cnf
查找(原設定)↓expire_logs_days這個參數
expire_logs_days=3     默認值是0,表示不自動刪除
(假設原本設定存放3天,要更改縮短成1天)
接著進入mysql更改天數,因為大部份DB不是說重啟就能重啟,
所以無法直接更改my.cnf的設定,需登入DB內更改
mysql>show variables like 'expire_logs_days';
+————————–+————-+
| Variable_name        | Value      |
+————————–+————-+
| expire_logs_days    | 3             |
+————————–+————-+
1 row in set (0.00 sec)
輸入指令確認保留天數
mysql>set global expire_logs_days=1;
mysql>flush logs;
輸入指令更改成1天,然後保存更新。
完成後可以查binlog檔案,檔案就會刪除只保留一天!!

正確方法2.進去mysql裡面binlog放置檔案的位置
ll mysql-bin.*
-rw-r—– 1 mysql mysql 2011 Feb 11 10:12 mysql-bin.000017
-rw-r—– 1 mysql mysql 2301 Feb 11 10:42 mysql-bin.000018
-rw-r—– 1 mysql mysql 1201 Feb 12 11:42 mysql-bin.000019
-rw-r—– 1 mysql mysql 2122 Feb 12 12:42 mysql-bin.000020
-rw-r—– 1 mysql mysql 4231 Feb 13 13:45 mysql-bin.000021
-rw-r—– 1 mysql mysql 5401 Feb 13 16:45 mysql-bin.000022
-rw-r—– 1 mysql mysql 2567 Feb 14 19:45 mysql-bin.000023
-rw-r—– 1 mysql mysql 9011 Feb 15 21:45 mysql-bin.000024
-rw-r—– 1 mysql mysql 19 Feb 15 21:46 mysql-bin.index
或是進入mysql 執行
mysql> show binary logs;
+———————+—————-+
| Log_name   | File_size   |
+———————+—————-+
| mysql-bin.000017 |  2011  |
| mysql-bin.000018 |  2301  |
| mysql-bin.000019 |  1201  |
| mysql-bin.000020 |  2122  |
| mysql-bin.000021 |   4231 |
| mysql-bin.000022 |   5401 |
| mysql-bin.000023 |   2567 |
| mysql-bin.000024 |   9011 |
+———————+—————–+
8 rows in set (0.00 sec) 
若要刪除 mysql-bin.000022 以前的 ,可直接在mysql內下指令
mysql>purge binary logs to 'mysql-bin.000022';
執行後再次查詢show binary logs; ,即更新。

沒有留言:

張貼留言

【MYSQL】MYSQL的SYS表說明(版本8.0)

mysql> use sys Reading table information for completion of table and column names You can turn off this feature to get a quicker s...