MySQL使用二進位日誌恢複資料庫

來源:互聯網
上載者:User

標籤:

一、二進位日誌簡介

MySQL有不同類型的日誌,其中二進位檔案記錄了所有對資料庫的修改,如果資料庫因為操作不當或其他原因丟失了資料,可以通過二進位檔案恢複。

在my.ini檔案中設定了log-bin,重新啟動MySQL後就開啟了二進位日誌。資料庫每次重新啟動(或執行flush logs命令)後,都會產生一個新的二進位日誌,如在在my.ini檔案中設定了

log-bin=F:\mysqllog\logbin

則資料庫第一次啟動會產生logbin.000001,第二次啟動會產生logbin.000002,第三次啟動會產生logbin.000003,......,以此類推。

二、資料恢複執行個體

1. 為了便於說明,執行flush logs命令,產生一個新的二進位檔案;

2. 開啟bookstore資料庫中的authors表,裡面已經有3條資料,在裡面新插入兩條資料,即第4條和第5條資料;

3. 假設由於操作失誤,將第4條和第5條資料刪除了;

4. 這時就考慮用二進位檔案恢複第4條和第5條資料了,開啟二進位檔案的路徑,發現有很多二進位記錄檔,

其中編號最大的000030是最新的,記錄了前面所述的記錄插入和刪除操作;

5. 首先查看一下這個檔案,運行以下命令將其轉換成文字檔,

開啟文字檔,可以看見記錄下了記錄的插入和刪除操作。

/*!40019 SET @@session.max_insert_delayed_threads=0*/;/*!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/;DELIMITER /*!*/;# at 4#150204 20:24:10 server id 1  end_log_pos 107     Start: binlog v 4, server v 5.5.25-log created 150204 20:24:10# Warning: this binlog is either in use or was not closed properly.BINLOG ‘ag/SVA8BAAAAZwAAAGsAAAABAAQANS41LjI1LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA==‘/*!*/;# at 107#150204 20:25:40 server id 1  end_log_pos 180     Query    thread_id=8    exec_time=0    error_code=0SET TIMESTAMP=1423052740/*!*/;SET @@session.pseudo_thread_id=8/*!*/;SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;SET @@session.sql_mode=1344274432/*!*/;SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;/*!\C utf8 *//*!*/;SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;SET @@session.lc_time_names=0/*!*/;SET @@session.collation_database=DEFAULT/*!*/;BEGIN/*!*/;# at 180#150204 20:25:40 server id 1  end_log_pos 369     Query    thread_id=8    exec_time=0    error_code=0use bookstore/*!*/;SET TIMESTAMP=1423052740/*!*/;INSERT INTO `bookstore`.`authors` (`author_id`, `author_last`, `author_first`, `country`) VALUES (4, ‘Li‘, ‘Si‘, ‘China‘)/*!*/;# at 369#150204 20:25:40 server id 1  end_log_pos 557     Query    thread_id=8    exec_time=0    error_code=0SET TIMESTAMP=1423052740/*!*/;INSERT INTO `bookstore`.`authors` (`author_id`, `author_last`, `author_first`, `country`) VALUES (5, ‘Wang‘, ‘Wu‘, ‘US‘)/*!*/;# at 557#150204 20:25:40 server id 1  end_log_pos 584     Xid = 391COMMIT/*!*/;# at 584#150204 20:45:31 server id 1  end_log_pos 657     Query    thread_id=8    exec_time=0    error_code=0SET TIMESTAMP=1423053931/*!*/;BEGIN/*!*/;# at 657#150204 20:45:31 server id 1  end_log_pos 780     Query    thread_id=8    exec_time=0    error_code=0SET TIMESTAMP=1423053931/*!*/;DELETE FROM `bookstore`.`authors` WHERE `author_id`=‘4‘/*!*/;# at 780#150204 20:45:31 server id 1  end_log_pos 903     Query    thread_id=8    exec_time=0    error_code=0SET TIMESTAMP=1423053931/*!*/;DELETE FROM `bookstore`.`authors` WHERE `author_id`=‘5‘/*!*/;# at 903#150204 20:45:31 server id 1  end_log_pos 930     Xid = 407COMMIT/*!*/;DELIMITER ;# End of log fileROLLBACK /* added by mysqlbinlog */;/*!50003 SET [email protected]_COMPLETION_TYPE*/;

6. 開始恢複資料,執行以下命令,

之所以要有一個--stop-pos=584參數,是因為從584開始,就在刪除記錄了,所以在584就應該停止。執行命令以後,再次開啟authors表,可以看見第4、5條資料被恢複了!

MySQL使用二進位日誌恢複資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.