溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL怎么使用二進制日志恢復數(shù)據(jù)庫

發(fā)布時間:2021-08-09 14:43:22 來源:億速云 閱讀:512 作者:chen 欄目:MySQL數(shù)據(jù)庫

本篇內(nèi)容主要講解“MySQL怎么使用二進制日志恢復數(shù)據(jù)庫”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“MySQL怎么使用二進制日志恢復數(shù)據(jù)庫”吧!

一、二進制日志簡介

   MySQL有不同類型的日志,其中二進制文件記錄了所有對數(shù)據(jù)庫的修改,如果數(shù)據(jù)庫因為操作不當或其他原因丟失了數(shù)據(jù),可以通過二進制文件恢復。在my.ini文件中設置了log-bin,重新啟動MySQL后就開啟了二進制日志。數(shù)據(jù)庫每次重新啟動(或執(zhí)行flush logs命令)后,都會生成一個新的二進制日志,如在在my.ini文件中設置了

log-bin=F:\mysqllog\logbin

則數(shù)據(jù)庫第一次啟動會生成logbin.000001,第二次啟動會生成logbin.000002,第三次啟動會生成logbin.000003,......,以此類推。

MySQL二進制日志刪除與恢復  http://www.linuxidc.com/Linux/2010-11/29576p2.htm

二、數(shù)據(jù)恢復實例

1. 為了便于說明,執(zhí)行flush logs命令,生成一個新的二進制文件;

2. 打開bookstore數(shù)據(jù)庫中的authors表,里面已經(jīng)有3條數(shù)據(jù),在里面新插入兩條數(shù)據(jù),即第4條和第5條數(shù)據(jù);

MySQL怎么使用二進制日志恢復數(shù)據(jù)庫

3. 假設由于操作失誤,將第4條和第5條數(shù)據(jù)刪除了;

MySQL怎么使用二進制日志恢復數(shù)據(jù)庫

4. 這時就考慮用二進制文件恢復第4條和第5條數(shù)據(jù)了,打開二進制文件的路徑,發(fā)現(xiàn)有很多二進制日志文件,

MySQL怎么使用二進制日志恢復數(shù)據(jù)庫

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

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

MySQL怎么使用二進制日志恢復數(shù)據(jù)庫

打開文本文件,可以看見記錄下了記錄的插入和刪除操作。

/*!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. 開始恢復數(shù)據(jù),執(zhí)行以下命令,

MySQL怎么使用二進制日志恢復數(shù)據(jù)庫

之所以要有一個--stop-pos=584參數(shù),是因為從584開始,就在刪除記錄了,所以在584就應該停止。執(zhí)行命令以后,再次打開authors表,可以看見第4、5條數(shù)據(jù)被恢復了!

MySQL怎么使用二進制日志恢復數(shù)據(jù)庫

到此,相信大家對“MySQL怎么使用二進制日志恢復數(shù)據(jù)庫”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI