萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> 刪除MySQL二進制日志命令與例子詳解

刪除MySQL二進制日志命令與例子詳解

下面我總結了四種刪除mysql二進制日志的方法,分別為reset master PURGE 設置日志過期expire_log_day及linux中的rm命令來刪除,具體操作方法都有例子說明有興趣的可參考一下。

方法一,刪除全部二進制日志:

在mysql上執行reset master命令,那麼就清除了所有的mysql-bin.*日志,並且以後日志文件名從mysql-bin.000001開始

reset master

刪除部分日志:

 代碼如下 復制代碼 PURGE MASTER LOGS TO & PURGE MASTER LOGS BEFORE
PURGE MASTER LOGS TO 'mysql-bin.******'命令,是將'******'編號之前的所有日志

進行刪除

PURGE MASTER LOGS BEFORE 'yyyy-mm-dd hh:mm:ss'命令,是將在'yyyy-mm-dd hh:mm:ss'時間之前的所有日志進行刪除


方法二,或者使用PURGE

PURGE {MASTER | BINARY} LOGS TO ‘log_name’
   PURGE {MASTER | BINARY} LOG

查看日志

 代碼如下 復制代碼

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |   1357315 |
| mysql-bin.000002 |       117 |
| mysql-bin.000003 |    404002 |
| mysql-bin.000004 |   2050722 |
| mysql-bin.000005 |    139103 |
| mysql-bin.000006 |     46702 |
| mysql-bin.000007 |       117 |
| mysql-bin.000008 |        98 |
| mysql-bin.000009 |       117 |
| mysql-bin.000010 |      1254 |
| mysql-bin.000011 |       117 |
| mysql-bin.000012 |  29394942 |
| mysql-bin.000013 |    422100 |
| mysql-bin.000014 |       117 |
| mysql-bin.000015 |       117 |
| mysql-bin.000016 |        98 |
| mysql-bin.000017 |       117 |
| mysql-bin.000018 |       117 |
| mysql-bin.000019 |    285300 |
| mysql-bin.000020 |    181229 |
| mysql-bin.000021 |        98 |
+------------------+-----------+
21 rows in set (0.03 sec)

刪除bin-log(刪除mysql-bin.000018之前的所有二進制日志文件)

 

 代碼如下 復制代碼

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

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000018 |       117 |
| mysql-bin.000019 |    285300 |
| mysql-bin.000020 |    181229 |
| mysql-bin.000021 |        98 |
+------------------+-----------+
4 rows in set (0.00 sec)

查看日志

 代碼如下 復制代碼

mysql> show binlog events;
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                  |
+------------------+-----+-------------+-----------+- www.111cn.net ------------+---------------------------------------+
| mysql-bin.000018 |   4 | Format_desc |         1 |          98 | Server ver: 5.0.45-log, Binlog ver: 4 |
| mysql-bin.000018 |  98 | Stop        |         1 |         117 |                                       |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
2 rows in set (0.01 sec)


 

方法三,設置日志過期時間:

修改my.cnf

 代碼如下 復制代碼

expire_log_day=5

這裡設置保存5天的日志,超過5天的日志會被自動刪除


方法四,如果是linux系統可以使用rm刪除

刪除二進制日志,啟動服務

 代碼如下 復制代碼 # c -rf /mybinlog/*
# netstat -tnlp 查看啟動的mysql進程號
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2523/mysql
# kill 2523 (注意,如果在備份之前mysql服務是開啟的,重啟是不成功的,需要把這個進程先kill掉)
# service mysqld restart
copyright © 萬盛學電腦網 all rights reserved