共計 4712 個字符,預(yù)計需要花費 12 分鐘才能閱讀完成。
這篇文章主要講解了“Innodb 關(guān)鍵特性之怎么實現(xiàn)啟動、關(guān)閉和恢復(fù)”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“Innodb 關(guān)鍵特性之怎么實現(xiàn)啟動、關(guān)閉和恢復(fù)”吧!
參數(shù) innodb_fast_shutdown 影響著表的存儲引擎為 Innodb 的行為,該參數(shù)可以取值為 0、1、2,默認值為 1,支持全動態(tài)局設(shè)置。
設(shè)置為 1:關(guān)閉 MySQL 的時候不會做清除臟頁和插入緩沖區(qū)的合并操作,也不會將臟頁刷新到磁盤。
設(shè)置為 0:會做清除臟頁和插入緩沖區(qū)的合并操作,也會將臟頁全部刷新到磁盤上面去,但是這個時候關(guān)閉的速度也是最慢的。
設(shè)置為 2:不會做清除臟頁和插入緩沖區(qū)的合并操作,也不會將臟頁刷新到磁盤,但是會刷新到 redo log 里面,再下次啟動
mysql 時候恢復(fù)。
參數(shù) innodb_force_recovery 影響了整個 Innodb 存儲引擎的恢復(fù)狀況。該值默認為 0,表示當需要恢復(fù)時執(zhí)行所有的恢復(fù)操作。當不能進行有效恢復(fù)時,如數(shù)據(jù)頁發(fā)生了 corruption,Mysql 數(shù)據(jù)庫可能會宕機,并把錯誤寫入錯誤日志中。
但在某些情況下,可能不需要執(zhí)行完整的恢復(fù)操作。例如在進行 alter table 操作時,這時發(fā)生意外,數(shù)據(jù)庫重啟時會對 Innodb 表執(zhí)行回滾操作。對于一個大表,這需要很長時間,甚至可能是幾個小時。這時可以自行恢復(fù),例如將表刪除,從備份中重新將數(shù)據(jù)導(dǎo)入表中,這些操作可能要快于回滾操作。
innodb_force_recovery 可以設(shè)置 6 個非零值:
1(SRV_FORCE_IGNORE_CORRUPT): 忽略檢查到的 corrupt 頁。
2(SRV_FORCE_NO_BACKGROUND): 阻止主線程的運行,如主線程需要執(zhí)行 full purge 操作,會導(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 存儲引擎會將未提交的事務(wù)視為已提交。
6(SRV_FORCE_NO_LOG_REDO): 不執(zhí)行前滾的操作。
備注:當設(shè)置 innodb_force_recovery 大于 0 后,可以對表進行 select、create、drop 操作,但 insert、update 或者 delete 這類操作是不允許的。
下面做一個實驗,來模擬故障的發(fā)生。
查看相關(guān)參數(shù):
mysql show variables like innodb_force%
+-----------------------------+-------+
| Variable_name | Value |
+-----------------------------+-------+
| innodb_force_load_corrupted | OFF |
| innodb_force_recovery | 0 |
+-----------------------------+-------+
2 rows in set (0.01 sec)
mysql show variables like innodb_fast%
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| innodb_fast_shutdown | 1 |
+----------------------+-------+
1 row in set (0.01 sec)
手動開啟事務(wù),防止 auto commit,并更新 1000000 條記錄,不提交。
mysql start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql update rank_item set city=5000 limit 1000000;
Query OK, 1000000 rows affected (4 min 34.50 sec)
Rows matched: 1000000 Changed: 1000000 Warnings: 0
然后 kill 掉 mysql 進程,啟動 mysql,查看錯誤日志輸出。
2020-08-05T07:07:20.271772Z 0 [Note] InnoDB: Starting in background the rollback of uncommitted transactions
2020-08-05T07:07:20.271806Z 0 [Note] InnoDB: Removed temporary tablespace data file: ibtmp1
2020-08-05T07:07:20.271826Z 0 [Note] InnoDB: Rolling back trx with id 393530, 901725 rows to undo
2020-08-05T07:07:20.271841Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
InnoDB: Progress in percents: 12020-08-05T07:07:20.272012Z 0 [Note] InnoDB: Setting file ./ibtmp1 size to 12 MB. Physically writing the file full; Please wait ...
2020-08-05T07:07:20.344665Z 0 [Note] InnoDB: File ./ibtmp1 size is now 12 MB.
2020-08-05T07:07:20.346201Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2020-08-05T07:07:20.346228Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2020-08-05T07:07:20.346492Z 0 [Note] InnoDB: Waiting for purge to start
2020-08-05T07:07:20.396629Z 0 [Note] InnoDB: 5.7.26 started; log sequence number 248770956866
2020-08-05T07:07:20.396654Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 30517ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
2020-08-05T07:07:20.397332Z 0 [Note] InnoDB: Loading buffer pool(s) from /data/mysql/data/ib_buffer_pool
2020-08-05T07:07:20.398205Z 0 [Note] Plugin FEDERATED is disabled.
2020-08-05T07:07:20.405796Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key
2020-08-05T07:07:20.405835Z 0 [Note] Server hostname (bind-address): * port: 16008
2020-08-05T07:07:20.405919Z 0 [Note] IPv6 is available.
2020-08-05T07:07:20.405943Z 0 [Note] - :: resolves to ::
2020-08-05T07:07:20.406018Z 0 [Note] Server socket created on IP: :: .
2020-08-05T07:07:20.471375Z 0 [Note] Event Scheduler: Loaded 0 events
2020-08-05T07:07:20.471641Z 0 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: 5.7.26 socket: /tmp/mysqld.sock port: 16008 Source distribution
2020-08-05T07:07:31.464532Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 10068ms. The settings might not be optimal. (flushed=5784 and evicted=0, during the time.)
2 3 4 5 6 7 82020-08-05T07:08:13.440447Z 0 [Note] InnoDB: Buffer pool(s) load completed at 200805 15:08:13
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 1002020-08-05T07:12:12.083704Z 0 [Note] InnoDB: Rollback of trx with id 393530 completed
2020-08-05T07:12:12.083776Z 0 [Note] InnoDB: Rollback of non-prepared transactions completed
可以看到 rollback 過程用了很久。
感謝各位的閱讀,以上就是“Innodb 關(guān)鍵特性之怎么實現(xiàn)啟動、關(guān)閉和恢復(fù)”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對 Innodb 關(guān)鍵特性之怎么實現(xiàn)啟動、關(guān)閉和恢復(fù)這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!