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

來源:互聯網
上載者:User

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

一、二進位日誌簡介

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

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

log-bin=F:\mysqllog\logbin

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

MySQL 二進位日誌刪除與恢複 

二、資料恢複執行個體

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 @OLD_COMPLETION_TYPE=@@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/SVA8BAAAAZwAAAGsAAAABAAQANS41LjI1LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA==
'/*!*/;
# at 107
#150204 20:25:40 server id 1  end_log_pos 180    Query    thread_id=8    exec_time=0    error_code=0
SET 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=0
use 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=0
SET 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 = 391
COMMIT/*!*/;
# at 584
#150204 20:45:31 server id 1  end_log_pos 657    Query    thread_id=8    exec_time=0    error_code=0
SET 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=0
SET 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=0
SET TIMESTAMP=1423053931/*!*/;
DELETE FROM `bookstore`.`authors` WHERE `author_id`='5'
/*!*/;
# at 903
#150204 20:45:31 server id 1  end_log_pos 930    Xid = 407
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

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

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

--------------------------------------分割線 --------------------------------------

Ubuntu 14.04下安裝MySQL

《MySQL權威指南(原書第2版)》清晰中文掃描版 PDF

Ubuntu 14.04 LTS 安裝 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主從伺服器

Ubuntu 12.04 LTS 構建高可用分布式 MySQL 叢集

Ubuntu 12.04下原始碼安裝MySQL5.6以及Python-MySQLdb

MySQL-5.5.38通用二進位安裝

--------------------------------------分割線 --------------------------------------

本文永久更新連結地址:

相關文章

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.