mysql恢複資料學習筆記

來源:互聯網
上載者:User

說不定某個程式員來一個del,某個備份時間定後面的資料想找也找不回來了,查了一下資料利用mysql日誌可以很好解決前面問題,優點:是想恢複到某個時間點,或某個操作sql語句,缺點:就產生龐大的記錄檔.下面是我在自己window服務下操作過程:
1.開啟mysql日誌
在my.ini 檔案裡找到[mysqld],在其下面增加一行log-bin

Ruby Code複製內容到剪貼簿01.[mysqld] 
02.# The TCP/IP Port the MySQL Server will listen on 
03.port=3306 
04.log-bin 
預設記錄檔名字是以主機命名名字,如果想改為自己定義的名字

01.[mysqld] 
02.# The TCP/IP Port the MySQL Server will listen on 
03.port=3306 
04.#mysql-bin為自訂名字 
05.log-bin = mysql-bin 
2.定義記錄檔路徑

Ruby Code複製內容到剪貼簿01.#Path to the database root存放日誌的路徑 
02.datadir="D:/web/mysql/Data/" 
3.重啟mysql
在上面的路徑下會產生兩個檔案

4.用命令進入mysql
mysql> show binlog events ;
你會看到類似這樣的介面:

上面pos就是開始位置end_log_pos的結束位置
相關命令:
1.查看當前日誌開啟情況
mysql>show variables like 'log%';
2.查看當前日誌情況
mysql>show master status;
3.顯示當前二進位
mysql>show binary logs;
5.恢複有兩種:
1.是把記錄檔儲存成sql檔案,再用source命令
定位儲存用法:在mysql安裝bin的目下:mysqlbinlog --start-position=4 --stop-position=239 d:/web/mysql/data/ mysqlbin-log.000001 >test1.txt
定時間儲存用法:在mysql安裝bin的目下mysqlbinlog --start-datetime="2013-03-16 13:00:00" --stop-datetime="201
3-03-16 14:00:00" d:/web/mysql/data/ mysqlbin-log.000001 >test1.txt
再source test1.txt
2.直接把日誌恢複到某個點上
定時間還原法:在mysql安裝bin的目下mysqlbinlog --start-datetime="2013-03-16 13:00:00" --stop-datetime="201
3-03-16 14:00:00" d:/web/mysql/data/ mysqlbin-log.000001 | mysql -uroot -p
定位還原法:在mysql安裝bin的目下 D:/web/mysql/bin>mysqlbinlog --start-position=3696 --stop-position=4241 d:/web/
ysql/data/mysql-bin.000001 | mysql -hlocalhost -uroot -p
PS:如果在建立表和動作表的過程有錯誤,在還原的時候也是會有錯誤的,那得部分定位來還原。(下面是我操作過程中一個小報錯,Duplicate entry '1' for key 1
去掉auto_increment,或者不要給有auto_increment的欄位賦值便可解決),有興趣的朋友可以簡單建立一個表,增加資料,然後刪除資料,利用上面步驟看看

聯繫我們

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