爛泥:通過binlog恢複mysql備份之前的資料

來源:互聯網
上載者:User

標籤:爛泥   通過   binlog   恢複   mysql   

本文首發於爛泥行天下。

上一篇文章,我們講解了如何通過mysql的binlog日誌恢複mysql資料庫,文章串連為《爛泥:通過binlog恢複mysql資料庫》。其中我們提到了的備份資料庫要晚與要恢複的資料庫時間,即要恢複的資料庫在前,而備份的資料庫在後。

當時我提到說會單獨寫一篇文章講解這個情況,本篇文章我就來介紹如何通過binlog日誌恢複比備份資料庫早時的資料庫狀態。我們還是以上篇文章的資料及備份檔案為基礎,來進行本篇文章的講解。

ailanni資料庫完整備份時,其資料庫中有1、2、3、4,這四條資料。而此時mysql總共有三個binlog記錄檔:mysql-bin.000001、mysql-bin.000002、mysql-bin.000003。其中在mysql-bin.000001中,我們向ilannitable表中插入兩個資料1、2。在mysql-bin.000002中我們向ilannitable表中插入兩個資料3、4。

現在我們要求把ailanni資料庫恢複到插入資料3後,未插入資料4時的狀態。即資料庫ailanni中只有資料1、2、3,這三條資料。

分析:要達到上述的要求,我們需要使用到mysql-bin.000002,這個binlog記錄檔。然後根據binlog日誌恢複資料庫使,可以指定pos位置節點的方法,來恢複ailanni資料庫。

除此非常重要的是,我們還要先把ailanni資料庫先恢複到完整備份時的狀態,如果不先把ailanni資料庫先恢複到完整備份時的狀態,我們在使用binlog日誌進行恢複時,系統會報錯。

我們需要分以下幾個步驟,可以恢複題目要求的ailanni資料庫狀態。如下:

1、 完整恢複ailanni資料庫

2、 刪除恢複後ailanni資料庫中的全部資料

3、 通過binlog日誌恢複ailanni資料庫

一、完整恢複ailanni資料庫

要完整恢複ailanni資料庫,我們可以通過mysql命令來實現。但是在這之前,我們還要就有關ailanni資料庫是否存在的情況要說明下。

如果目前mysql資料庫伺服器上有ailanni這個資料庫,那麼我們在恢複資料時,就不需要重新建立ailanni資料庫。如果目前mysql資料庫伺服器上沒有ailanni這個資料庫,那麼我們就需要重新建立一個空的ailanni資料庫。即該資料庫只是一個空的庫,裡面沒有任何錶或者其他的元素。

如果不建立ailanni資料庫的話,我們在通過mysql命令恢複資料庫時,系統就會報錯,如下:

650) this.width=650;" title="clip_image001" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image001" src="http://s3.51cto.com/wyfs02/M01/53/A8/wKioL1RtVO7ihkuCAADNgMmX6oc870.jpg" width="605" height="69" />

現在我們來執行mysql命令匯入備份的sql檔案,如下:

create database ailanni;

/usr/local/mysql/bin/mysql -uroot -p123456 ailanni</root/ailanni.sql

mysql -uroot -p123456

use ailanni;

select id from ilannitable;

650) this.width=650;" title="clip_image002" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image002" src="http://s3.51cto.com/wyfs02/M01/53/A8/wKioL1RtVPuDgnzoAAIf4IQbzOA983.jpg" width="610" height="428" />

通過,我們可以看到ailanni資料庫已經完全恢複。

二、刪除恢複後ailanni資料庫中的全部資料

在第一步中我們已經把ailanni資料庫全部恢複,現在我們需要清空ailanni資料庫。如下:

delete from ilannitable;

select id from ilannitable;

650) this.width=650;" title="clip_image003" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image003" src="http://s3.51cto.com/wyfs02/M02/53/AA/wKiom1RtVIOQv8wCAAC23PxX3D0573.jpg" width="338" height="136" />

通過,我們可以看到目前ailanni資料庫中已經沒有資料,是一個空的資料庫。

三、通過binlog日誌恢複ailanni資料庫

第二步已經清空ailanni資料庫,這樣我們就可以通過mysql的binlog日誌來恢複ailanni資料庫。

也許你會問我們為什麼要先完整恢複ailanni資料庫,然後再把ailanni資料庫清空?

這個是因為如果我們不先完整恢複ailanni資料庫的話,我們在使用binlog進行恢複ailanni資料庫時,系統會報錯的。如下:

650) this.width=650;" title="clip_image004" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image004" src="http://s3.51cto.com/wyfs02/M02/53/AA/wKiom1RtVe7yxgPVAADn5bNzWlw605.jpg" width="729" height="78" />

如果我們不清空ailanni資料庫的話,你會發下恢複後的ailanni資料庫,還是沒有達到我們的要求。如下:

650) this.width=650;" title="clip_image005" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image005" src="http://s3.51cto.com/wyfs02/M00/53/AA/wKiom1RtVfLyRvwiAAJ1OuzRp-M081.jpg" width="729" height="470" />

所以我們要進行第一、二步的操作。

現在我們來查看mysql-bin.000002檔案,以確定ailanni資料庫恢複的pos位置節點,如下:

/usr/local/mysql/bin/mysqlbinlog /usr/local/mysql/data/mysql-bin.000002

650) this.width=650;" title="clip_image006" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image006" src="http://s3.51cto.com/wyfs02/M01/53/AA/wKiom1RtVfaixHkaAAKsoCyZ5Os326.jpg" width="641" height="415" />

注意圖中標記出來的數字304,這個就是binlog的pos節點位置。我們可以看到在該pos節點後mysql才執行插入資料4的SQL語句。那麼我們只需要把資料恢複這個節點即可。

使用如下命令進行恢複資料,如下:

/usr/local/mysql/bin/mysqlbinlog --stop-position=304 /usr/local/mysql/data/mysql-bin.000002 |mysql -uroot -p123456

650) this.width=650;" title="clip_image007" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image007" src="http://s3.51cto.com/wyfs02/M02/53/AA/wKiom1RtVfmh1g0rAAJWaeqYysM302.jpg" width="698" height="468" />

通過,我們現在可以看到ailanni資料庫已經恢複到插入資料3之後,未插入4的狀態。現在ailanni資料庫中確實只有1、2、3,這三條資料,已經達到我們的要求。

其實我們還可以這樣說,目前只有mysql的所有binlog日誌,而且還有一個比較完整的Database Backup,那麼該如何恢複資料庫呢?

按照上述方法也是可以恢複的。

本文出自 “爛泥行天下” 部落格,請務必保留此出處http://ilanni.blog.51cto.com/526870/1579971

爛泥:通過binlog恢複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.