使用mysqlbinlog恢複指定表

來源:互聯網
上載者:User

標籤:

一、登入資料庫重新整理binlog
1.1)查看當前的binlog
MySQL> show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| test-150-bin.000003 |  2895377 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

1.2)重新整理binlog
mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)

1.3)確認重新整理binlog成功
mysql> show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| test-150-bin.000004 |      120 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
用show master status 命令查看當前的binlog已經由test-150-bin.000003變為test-150-bin.000004,
證明binlog已經重新整理成功。

二、查詢二進位日誌位置
mysql> show variables like‘log_bin%‘;
+---------------------------------+------------------------------------------+
| Variable_name                   | Value                                    |
+---------------------------------+------------------------------------------+
| log_bin                             | ON                                       |
| log_bin_basename             | /db/mysql5.6/data/test-150-bin       |
| log_bin_index                    | /db/mysql5.6/data/test-150-bin.index |
| log_bin_trust_function_creators  | ON                                       |
| log_bin_use_v1_row_events       | OFF                                      |
+---------------------------------+------------------------------------------+

三、從二進位日誌中擷取表被刪除的時間
$ mysqlbinlog test-150-bin.000003 | grep -i DROP  -A3 -B4
COMMIT/*!*/;
# at 122869
#140126 17:03:35 server id 150  end_log_pos 122989 CRC32 0x8707c4c4     Query   thread_id=12519 exec_time=0     error_code=0
SET TIMESTAMP=1390727015/*!*/;
DROP TABLE `test` /* generated by server */
/*!*/;
# at 122989
#140126 17:04:06 server id 150  end_log_pos 123039 CRC32 0x43476aad     Rotate to test-150-bin.000004  pos: 4

drop語句的前兩行表名drop語句的執行時間是在 17:03:35
mysql> SELECT from_unixtime(‘1390727015‘);
+-----------------------------+
| from_unixtime(‘1390727015‘)  |
+-----------------------------+
| 2014-01-26 17:03:35.000000 |
+-----------------------------+
1 row in set

 

四、從binlog中擷取指定資料庫的改變資料
用mysqlbinlog 恢複ivr資料庫在二進位日誌test-150-bin.000003中的資料
這裡假設從上一次mysql備份後,只有一個binlog產生,即登入資料庫時查詢到binlog test-150-bin.000003
mysqlbinlog -d ivr --stop-datetime=‘2014-01-26 17:03:35‘ test-150-bin.000003 >recover_ivr.sql

如果從上次備份重新整理binlog,到發現表被刪掉的過程中產生了多個binlog。則要按照binlog產生的順序進行恢複,那
麼恢複的次序應該是按照binglog的產生的序號,從小到大依次恢複。
假如從上次備份,到發現表被刪除,共有兩個binlog檔案,分別是test-150-bin.000002,test-150-bin.000003 ,
則按照binlog序號從小到大的排列,恢複的順序應該是:

mysqlbinlog -d ivr  test-150-bin.000002  > recover_ivr.sql

mysqlbinlog -d ivr --stop-datetime=‘2014-01-26 17:03:35‘ test-150-bin.000003  >> recover_ivr.sql

由於恢複的檔案recover_ivr.sql中包含了整個ivr資料庫的所有表,我們只要恢複指定的表testtuo,還要對恢複出來的sql進行過濾。

[[email protected] data]$ more recover_ivr.sql | grep  -i -E ‘insert|update|delete‘   -A2 -B2 | grep testtuo
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
--
# at 122721
#140126 17:03:28 server id 150  end_log_pos 122721 CRC32 0xe0f851bb     Intvar
SET INSERT_ID=4/*!*/;
#140126 17:03:28 server id 150  end_log_pos 122838 CRC32 0x9efcc7b2     Query   thread_id=12578 exec_time=0     error_code=0
use `ivr`/*!*/;
SET TIMESTAMP=1390727008/*!*/;
INSERT INTO `testtuo` (`name`) VALUES (‘d‘)
/*!*/;
# at 122838

將過濾後的結果儲存為sql指令碼,恢複到資料庫即可。


恢複之前為了避免產生沒有用的二進位日誌,可以關閉二進位日誌的記錄
SET SESSION sql_log_bin=0;

恢複完成,啟用記錄二進位日誌
SET SESSION sql_log_bin=1;

 

 

 

  

使用mysqlbinlog恢複指定表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.