mysql通過binlog日誌來恢複資料

來源:互聯網
上載者:User

標籤:key   結束時間   log   pos   class   word   備份   top   執行   

簡介

在生產的過程中有這麼一個業務情境:比如我在2016-11-19 09:30:00 通過mysqldump的方式備份了資料庫,但是在2016-11-19 10:30:00的時候資料庫崩潰了,如果通過我們的備份檔案來恢複,也只能恢複到 2016-11-19 09:30:00 這個時間點,而9.30分到10:30分這一個小時的資料恢複不了,這時候我們可以通過mysql的binlog日誌來恢複沒有備份到的這一小時的資料。

1、開啟mysql的binlog日誌(預設情況下沒有開啟)

#vim /etc/my.cnf下添加一行(也可以另外指定binlog日誌的路徑)
log-bin = mysql-bin

#mysql5.7預設情況下,binlog記錄檔預設放在/usr/local/mysql/data/目錄下

2、用mysqlbinlog可以查看binlog日誌裡的資訊
./mysqlbinlog --no-defaults /usr/local/mysql/data/mysql-bin.000003
3、根據位置恢複#mysqlbinlog --start-position=134 --stop-position=330 mysql-binglog.000001 > /root/test1.txt
進入MYSQL匯入
mysql> source /root/test1.txt
--start-position  起始位置--stop-position  結束位置   4、根據日期#./mysqlbinlog --start-date="2016-11-10 17:30:05"--stop-date="2016-11-10 18:00:00"mysql-binlog.000002  >/root/test2.sql
mysql> source /root/test2.txt--start-date 起始時間--stop-date 結束時間
或者
#./mysqlbinlog --start-date="2016-11-10 17:30:05"--stop-date="2016-11-10 18:00:00" mysql-binlog.000002 | ./mysql -uroot -padmin
將mysql-bin.000002記錄檔中從2016-11-10 17:30:05到2016-11-10 18:00:00截止的sql語句匯入到mysql中test
----如果有多個binlog檔案,中間用空格隔開,打上完全路徑5、補充 mysqldump備份[[email protected] tmp]# /usr/local/mysql/bin/mysqldump -uroot -padmin test -l -F > /tmp/test.sql   #備份 資料庫test到tmpl目錄下的test.sql檔案中-l 表示讀鎖,在備份的過程中mysql不能寫入資料,從而保證資料的完整性-f 表示 flust logs,重建新的記錄檔,當然也包括bin-log日誌./mysql -uroot -padmin test  -v -f < /tmp/test.sql   #通過/tmp/test.sql檔案恢複test資料庫-v 查看匯入的詳細資料-f 匯入處理程序中遇到錯誤,跳過,並繼續往下執行

mysql通過binlog日誌來恢複資料

聯繫我們

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