日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
MySQL斷電恢復(fù)的一點(diǎn)簡單分析

今天有個(gè)網(wǎng)友問我一個(gè)MySQL的恢復(fù)問題。提供的截圖如下。

對于這個(gè)問題,在一些斷電的場景下還是可能出現(xiàn)的。我首先是要確認(rèn)是否為線上業(yè)務(wù)還是測試環(huán)境,線上業(yè)務(wù)來說這個(gè)影響還是很大的。如果數(shù)據(jù)庫無法啟動(dòng),首要任務(wù)還是把數(shù)據(jù)庫啟動(dòng),然后在這個(gè)基礎(chǔ)上查看丟失的數(shù)據(jù)程度,安排數(shù)據(jù)修復(fù)的事宜。

當(dāng)然從我的角度來說,怎么去快速復(fù)現(xiàn)這個(gè)問題呢。我用自己寫的快速搭建測試主從環(huán)境的腳本(https://github.com/jeanron100/mysql_slaves,后期有一位大牛建議用Python來做,最近在考慮),分分鐘即可搞定。

我們創(chuàng)建一個(gè)表test,指定id,name兩個(gè)字段。然后開啟顯式事務(wù)。

 
 
 
 
  1. create table test(id int primary key,name varchar(30) not null); 

顯式開啟一個(gè)事務(wù):

 
 
 
 
  1. begin; 
  2. insert into test values(1,'a'); 
  3. insert into test values(2,'b'); 
  4. insert into test values(3,'c');  

不提交,我們直接查看mysql的服務(wù)進(jìn)程,直接Kill掉。默認(rèn)情況下雙1指標(biāo)是開啟的,我們直接模擬斷電重啟,看看后臺(tái)的處理情況:

 
 
 
 
  1. 2017-09-13 15:05:11 35556 [Note] InnoDB: Highest supported file format is Barracuda. 
  2.  
  3. 2017-09-13 15:05:11 35556 [Note] InnoDB: The log sequence numbers 1625987 and 1625987 in ibdata files do not match the log sequence number 1640654 in the ib_logfiles! 
  4.  
  5. 2017-09-13 15:05:11 35556 [Note] InnoDB: Database was not shutdown normally! 
  6.  
  7. 2017-09-13 15:05:11 35556 [Note] InnoDB: Starting crash recovery. 
  8.  
  9. 2017-09-13 15:05:11 35556 [Note] InnoDB: Reading tablespace information from the .ibd files... 
  10.  
  11. 2017-09-13 15:05:11 35556 [Note] InnoDB: Restoring possible half-written data pages 
  12.  
  13. 2017-09-13 15:05:11 35556 [Note] InnoDB: from the doublewrite buffer... 
  14.  
  15. InnoDB: 1 transaction(s) which must be rolled back or cleaned up 
  16.  
  17. InnoDB: in total 3 row operations to undo 
  18.  
  19. InnoDB: Trx id counter is 2304 
  20.  
  21. 2017-09-13 15:05:11 35556 [Note] InnoDB: 128 rollback segment(s) are active. 
  22.  
  23. InnoDB: Starting in background the rollback of uncommitted transactions 
  24.  
  25. 2017-09-13 15:05:11 7f5ccc3d1700 InnoDB: Rolling back trx with id 1806, 3 rows to undo 
  26.  
  27. 2017-09-13 15:05:11 35556 [Note] InnoDB: Rollback of trx with id 1806 completed 
  28.  
  29. 2017-09-13 15:05:11 7f5ccc3d1700 InnoDB: Rollback of non-prepared transactions completed 
  30.  
  31. 2017-09-13 15:05:11 35556 [Note] InnoDB: Waiting for purge to start 
  32.  
  33. 2017-09-13 15:05:11 35556 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.14-rel62.0 started; log sequence number 1640654 
  34.  
  35. 2017-09-13 15:05:11 35556 [Note] Recovering after a crash using binlog 
  36.  
  37. 2017-09-13 15:05:11 35556 [Note] Starting crash recovery... 
  38.  
  39. 2017-09-13 15:05:11 35556 [Note] Crash recovery finished.  

可以看到后臺(tái)檢測到了上次的異常宕機(jī),然后開啟崩潰恢復(fù),InnoDB檢測到日志LSN是1625987 而系統(tǒng)數(shù)據(jù)文件ibd的LSN為1625987 ,和ib_logfiles里面的LSN不匹配。后面就是一系列的恢復(fù),前滾,恢復(fù),回滾。***表里的數(shù)據(jù)為空,證明之前的事務(wù)都已經(jīng)回滾了。

所以基于上面的情況,我們明白開啟了事務(wù),基本情況下這個(gè)問題是不會(huì)出現(xiàn)的,什么時(shí)候會(huì)拋出開始的錯(cuò)誤呢。

我們繼續(xù)測試,開啟一個(gè)顯式事務(wù),不提交。

 
 
 
 
  1. begin; 
  2.  
  3. insert into test values(1,'a'); 
  4.  
  5. insert into test values(2,'b'); 
  6.  
  7. insert into test values(3,'c');  

然后殺掉mysql的服務(wù)進(jìn)程,找到mysql的數(shù)據(jù)目錄下,刪除redo文件。完成后我們重啟數(shù)據(jù)庫。

這個(gè)時(shí)候就拋出了和截圖類似的錯(cuò)誤。

 
 
 
 
  1. 2017-09-13 16:05:14 36896 [Note] InnoDB: Highest supported file format is Barracuda. 
  2.  
  3. 2017-09-13 16:05:14 7f73450a97e0 InnoDB: Error: page 7 log sequence number 1627722 
  4.  
  5. InnoDB: is in the future! Current system log sequence number 1626124. 
  6.  
  7. InnoDB: Your database may be corrupt or you may have copied the InnoDB 
  8.  
  9. InnoDB: tablespace but not the InnoDB log files. See 
  10.  
  11. InnoDB: http://dev.mysql.com/doc/refman/5.6/en/forcing-innodb-recovery.html 
  12.  
  13. InnoDB: for more information.  

這個(gè)問題目前的影響范圍其實(shí)還不明顯,因?yàn)楸M管如此,我們還是能夠?qū)懭霐?shù)據(jù)的。 

 
 
 
 
  1. mysql> insert into test values(1,'a'); 
  2.  
  3. Query OK, 1 row affected (0.04 sec) 
  4.  
  5. mysql> select *from test; 
  6.  
  7. +----+------+ 
  8.  
  9. | id | name | 
  10.  
  11. +----+------+ 
  12.  
  13. | 1 | a | 
  14.  
  15. +----+------+ 
  16.  
  17. 1 row in set (0.00 sec)  

關(guān)于崩潰恢復(fù),有一個(gè)數(shù)據(jù)參數(shù)尤其需要注意,那就是innodb_force_recovery,這個(gè)參數(shù)默認(rèn)值為0,如果為非0的值(范圍為1-6),會(huì)有下面的影響范圍。

1 (SRV_FORCE_IGNORE_CORRUPT): 忽略檢查到的corrupt頁。

2 (SRV_FORCE_NO_BACKGROUND): 阻止主線程的運(yùn)行,如主線程需要執(zhí)行full purge操作,會(huì)導(dǎo)致crash。

3 (SRV_FORCE_NO_TRX_UNDO): 不執(zhí)行事務(wù)回滾操作。

4 (SRV_FORCE_NO_IBUF_MERGE): 不執(zhí)行插入緩沖的合并操作。

5 (SRV_FORCE_NO_UNDO_LOG_SCAN):不查看重做日志,InnoDB存儲(chǔ)引擎會(huì)將未提交的事務(wù)視為已提交。

6 (SRV_FORCE_NO_LOG_REDO): 不執(zhí)行前滾的操作。

當(dāng)然這個(gè)參數(shù)的設(shè)置修改是需要重啟MySQL服務(wù)的。

 
 
 
 
  1. mysql> set global innodb_force_recovery=2; 
  2.  
  3. ERROR 1238 (HY000): Variable 'innodb_force_recovery' is a read only variable  

在此假設(shè)我們設(shè)置為2,再次復(fù)現(xiàn)這個(gè)問題問題,你就會(huì)發(fā)現(xiàn),數(shù)據(jù)庫暫時(shí)是可以啟動(dòng)的,但是數(shù)據(jù)只能查詢,DML操作都會(huì)拋錯(cuò)。

 
 
 
 
  1. mysql> select *from test; 
  2.  
  3. Empty set (0.00 sec) 
  4.  
  5. mysql> 
  6.  
  7. mysql> insert into test values(1,'a'); 
  8.  
  9. ERROR 1030 (HY000): Got error -1 from storage engine  

按照這個(gè)影響的范圍來評(píng)估force_recovery的值,我們就可以做相應(yīng)的取舍了。如果MySQL服務(wù)無法正常啟動(dòng),就可以修改這個(gè)參數(shù)值來調(diào)整,先滿足服務(wù)可持續(xù)性的基本問題。然后評(píng)估后導(dǎo)出重要的數(shù)據(jù)來。 


文章名稱:MySQL斷電恢復(fù)的一點(diǎn)簡單分析
分享URL:http://www.dlmjj.cn/article/dpjegde.html