参数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过程用了很久。