Innodb关键特性之怎么实现启动、关闭和恢复

56次阅读
没有评论

共计 4712 个字符,预计需要花费 12 分钟才能阅读完成。

这篇文章主要讲解了“Innodb 关键特性之怎么实现启动、关闭和恢复”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着丸趣 TV 小编的思路慢慢深入,一起来研究和学习“Innodb 关键特性之怎么实现启动、关闭和恢复”吧!

        参数 innodb_fast_shutdown 影响着表的存储引擎为 Innodb 的行为,该参数可以取值为 0、1、2,默认值为 1,支持全动态局设置。

设置为 1:关闭 MySQL 的时候不会做清除脏页和插入缓冲区的合并操作,也不会将脏页刷新到磁盘。

设置为 0:会做清除脏页和插入缓冲区的合并操作,也会将脏页全部刷新到磁盘上面去,但是这个时候关闭的速度也是最慢的。

设置为 2:不会做清除脏页和插入缓冲区的合并操作,也不会将脏页刷新到磁盘,但是会刷新到 redo log 里面,再下次启动

mysql 时候恢复。

        参数 innodb_force_recovery 影响了整个 Innodb 存储引擎的恢复状况。该值默认为 0,表示当需要恢复时执行所有的恢复操作。当不能进行有效恢复时,如数据页发生了 corruption,Mysql 数据库可能会宕机,并把错误写入错误日志中。

        但在某些情况下,可能不需要执行完整的恢复操作。例如在进行 alter table 操作时,这时发生意外,数据库重启时会对 Innodb 表执行回滚操作。对于一个大表,这需要很长时间,甚至可能是几个小时。这时可以自行恢复,例如将表删除,从备份中重新将数据导入表中,这些操作可能要快于回滚操作。

 innodb_force_recovery 可以设置 6 个非零值:

1(SRV_FORCE_IGNORE_CORRUPT): 忽略检查到的 corrupt 页。

2(SRV_FORCE_NO_BACKGROUND): 阻止主线程的运行,如主线程需要执行 full purge 操作,会导致 crash。

3(SRV_FORCE_NO_TRX_UNDO): 不执行事务回滚操作。

4(SRV_FORCE_NO_IBUF_MERGE): 不执行插入缓冲的合并操作。

5(SRV_FORCE_NO_UNDO_LOG_SCAN): 不查看重做日志,InnoDB 存储引擎会将未提交的事务视为已提交。

6(SRV_FORCE_NO_LOG_REDO): 不执行前滚的操作。

备注:当设置 innodb_force_recovery 大于 0 后,可以对表进行 select、create、drop 操作,但 insert、update 或者 delete 这类操作是不允许的。

      下面做一个实验,来模拟故障的发生。

查看相关参数:

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)

手动开启事务,防止 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 关键特性之怎么实现启动、关闭和恢复”的内容了,经过本文的学习后,相信大家对 Innodb 关键特性之怎么实现启动、关闭和恢复这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是丸趣 TV,丸趣 TV 小编将为大家推送更多相关知识点的文章,欢迎关注!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-27发表,共计4712字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)