文章目录
- MySQL 8.0 InnoDB Tablespaces之Undo Tablespaces(UNDO表空间)
- Undo Tablespaces(UNDO表空间)
- 默认UNDO表空间
- 添加 Undo 表空间
- 查看Undo 相关的信息
- 查看Undo 相关参数变量
- 查看Undo 状态信息
- 通过information_schema.innodb_tablespaces 查看undo表空间信息
- 通过information_schema.FILES查看undo表空间的数据文件信息
- undo表空间的状态
- 参考
【免责声明】文章仅供学习交流,观点代表个人,与任何公司无关。
编辑|SQL和数据库技术(ID:SQLplusDB)
MySQL 8.0 OCP (1Z0-908) 考点精析-安装与配置考点1:设置系统变量
【MySQL】控制MySQL优化器行为方法之optimizer_switch系统变量
【MySQL】MySQL系统变量(system variables)列表(mysqld --verbose --help的结果例)
【MySQL】MySQL系统变量(system variables)列表(SHOW VARIABLES 的结果例)
MySQL 8.0 OCP (1Z0-908) 考点精析-备份与恢复考点1:MySQL Enterprise Backup概要
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点1:sys.statement_analysis视图
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点2:系统变量的确认
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点3:EXPLAIN ANALYZE
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点4:慢速查询日志(slow query log)
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点5:表连接算法(join algorithm)
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点6:MySQL Enterprise Monitor之Query Analyzer
MySQL 8.0 OCP (1Z0-908) 考点精析-架构考点1:二进制日志文件(Binary log)
MySQL 8.0 OCP (1Z0-908) 考点精析-架构考点5:数据字典(Data Dictionary)
MySQL 8.0 OCP (1Z0-908) 考点精析-架构考点6:InnoDB Tablespaces之系统表空间(System Tablespace)
MySQL 8.0 InnoDB Tablespaces之File-per-table tablespaces(单独表空间)
MySQL 8.0 InnoDB Tablespaces之General Tablespaces(通用表空间/一般表空间)
MySQL 8.0 InnoDB Tablespaces之Undo Tablespaces(UNDO表空间)
InnoDB表空间是MySQL中用于存储InnoDB存储引擎表数据和索引的物理文件。
InnoDB表空间根据用途可以分成多种类型:
- 数据表空间:
- System tablespace(系统表空间)
- File-per-table tablespaces(单独表空间)
- General tablespaces(通用表空间/一般表空间)
- Undo 表空间
- 临时表空间(Temporary table tablespaces)
Undo Tablespaces(UNDO表空间)
UNDO日志(Undo Log)主要用于事务异常时的数据回滚,在磁盘上UNDO日志保存在UNDO表空间中。
默认UNDO表空间
MySQL实例初始化时会创建两个默认UNDO表空间,默认UNDO表空间会创建在innodb_undo_directory变量定义的位置。
默认UNDO表空间的数据文件命名为undo_001和undo_002,在数据字典中定义的对应UNDO表空间名称为innodb_undo_001和innodb_undo_002。
例:
mysql> show variables like 'datadir';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| datadir | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.00 sec)
mysql> show variables like 'innodb_undo_directory';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_undo_directory | ./ |
+-----------------------+-------+
1 row in set (0.00 sec)
mysql> SELECT TABLESPACE_NAME, FILE_NAME, FILE_TYPE,AUTOEXTEND_SIZE/1024/1024 size_m
-> FROM INFORMATION_SCHEMA.FILES
-> WHERE FILE_TYPE LIKE 'UNDO LOG';
+-----------------+------------+-----------+-------------+
| TABLESPACE_NAME | FILE_NAME | FILE_TYPE | size_m |
+-----------------+------------+-----------+-------------+
| innodb_undo_001 | ./undo_001 | UNDO LOG | 16.00000000 |
| innodb_undo_002 | ./undo_002 | UNDO LOG | 16.00000000 |
+-----------------+------------+-----------+-------------+
2 rows in set (0.01 sec)
mysql>
查看数据文件。
root@mysql-vm:/var/lib/mysql# ls -lh undo*
-rw-r----- 1 mysql mysql 16M Dec 28 17:09 undo_001
-rw-r----- 1 mysql mysql 16M Dec 28 17:09 undo_002
root@mysql-vm:/var/lib/mysql#
MySQL 8.0.23开始,初始UNDO表空间大小通常为16MB,UNDO表空间的扩展大小至少为16MB。
添加 Undo 表空间
由于 Undo 日志会在长时间运行的事务中变得很大,创建额外的 Undo 表空间可以避免单个表空间变得太大。MySQL 8.0.14 版本之后,可以在运行时使用CREATE UNDO TABLESPACE语法创建额外的UNDO 表空间。
CREATE UNDO TABLESPACE tablespace_name ADD DATAFILE 'file_name.ibu';
例:
mysql> CREATE UNDO TABLESPACE undo_tbs_1 ADD DATAFILE 'undo_tbs_1.ibu';
Query OK, 0 rows affected (0.26 sec)
mysql> SELECT TABLESPACE_NAME, FILE_NAME, FILE_TYPE,AUTOEXTEND_SIZE/1024/1024 size_m
-> FROM INFORMATION_SCHEMA.FILES
-> WHERE FILE_TYPE LIKE 'UNDO LOG';
+-----------------+------------------+-----------+-------------+
| TABLESPACE_NAME | FILE_NAME | FILE_TYPE | size_m |
+-----------------+------------------+-----------+-------------+
| innodb_undo_001 | ./undo_001 | UNDO LOG | 16.00000000 |
| innodb_undo_002 | ./undo_002 | UNDO LOG | 16.00000000 |
| undo_tbs_1 | ./undo_tbs_1.ibu | UNDO LOG | 16.00000000 |
+-----------------+------------------+-----------+-------------+
3 rows in set (0.00 sec)
mysql>
注意:创建创建额外的Undo 表空间的数据文件的扩展名必须使用 .ibu 。
查看Undo 相关的信息
查看Undo 相关参数变量
Undo 相关的参数变量主要包括如下内容。
例:
mysql> show variables like '%undo%';
+--------------------------+------------+
| Variable_name | Value |
+--------------------------+------------+
| innodb_max_undo_log_size | 1073741824 |
| innodb_undo_directory | ./ |
| innodb_undo_log_encrypt | OFF |
| innodb_undo_log_truncate | ON |
| innodb_undo_tablespaces | 2 |
+--------------------------+------------+
回滚段(rollback segment)相关参数。
mysql> show variables like '%rollback%'
-> ;
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| innodb_rollback_on_timeout | OFF |
| innodb_rollback_segments | 128 |
+----------------------------+-------+
2 rows in set (0.00 sec)
mysql>
mysql> show variables like 'innodb_purge_rseg_truncate_frequency';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| innodb_purge_rseg_truncate_frequency | 128 |
+--------------------------------------+-------+
1 row in set (0.01 sec)
查看Undo 状态信息
通过information_schema.innodb_tablespaces 查看undo表空间信息
mysql> select * from information_schema.innodb_tablespaces where name like '%undo%' \G
*************************** 1. row ***************************
SPACE: 4294967279
NAME: innodb_undo_001
FLAG: 0
ROW_FORMAT: Undo
PAGE_SIZE: 16384
ZIP_PAGE_SIZE: 0
SPACE_TYPE: Undo
FS_BLOCK_SIZE: 4096
FILE_SIZE: 16777216
ALLOCATED_SIZE: 16777216
AUTOEXTEND_SIZE: 0
SERVER_VERSION: 8.0.32
SPACE_VERSION: 1
ENCRYPTION: N
STATE: active
*************************** 2. row ***************************
SPACE: 4294967278
NAME: innodb_undo_002
FLAG: 0
ROW_FORMAT: Undo
PAGE_SIZE: 16384
ZIP_PAGE_SIZE: 0
SPACE_TYPE: Undo
FS_BLOCK_SIZE: 4096
FILE_SIZE: 16777216
ALLOCATED_SIZE: 16777216
AUTOEXTEND_SIZE: 0
SERVER_VERSION: 8.0.32
SPACE_VERSION: 1
ENCRYPTION: N
STATE: active
2 rows in set (0.00 sec)
mysql>
UNDO表空间的使用状态:
mysql> SELECT NAME, STATE FROM INFORMATION_SCHEMA.INNODB_TABLESPACES
-> WHERE NAME in ('innodb_undo_001','innodb_undo_002')\G
*************************** 1. row ***************************
NAME: innodb_undo_001
STATE: active
*************************** 2. row ***************************
NAME: innodb_undo_002
STATE: active
2 rows in set (0.00 sec)
mysql>
通过information_schema.FILES查看undo表空间的数据文件信息
mysql> SELECT TABLESPACE_NAME, FILE_NAME, FILE_TYPE FROM INFORMATION_SCHEMA.FILES
-> WHERE FILE_TYPE LIKE 'UNDO LOG';
+-----------------+------------+-----------+
| TABLESPACE_NAME | FILE_NAME | FILE_TYPE |
+-----------------+------------+-----------+
| innodb_undo_001 | ./undo_001 | UNDO LOG |
| innodb_undo_002 | ./undo_002 | UNDO LOG |
+-----------------+------------+-----------+
2 rows in set (0.00 sec)
mysql>
undo表空间的状态
undo表空间的状态相关的信息。
例:
mysql> SHOW STATUS LIKE '%undo%';
+----------------------------------+-------+
| Variable_name | Value |
+----------------------------------+-------+
| Innodb_undo_tablespaces_total | 2 |
| Innodb_undo_tablespaces_implicit | 2 |
| Innodb_undo_tablespaces_explicit | 0 |
| Innodb_undo_tablespaces_active | 2 |
+----------------------------------+-------+
4 rows in set (0.00 sec)
mysql>
Innodb_undo_tablespaces_total :总的UNDO表空间数量
Innodb_undo_tablespaces_implicit :隐式(InnoDB创建的)UNDO表空间数量
Innodb_undo_tablespaces_explicit :显式(用户创建的)UNDO表空间数量
Innodb_undo_tablespaces_active :活动UNDO表空间数量
参考
15.6.3.4 Undo Tablespaces
https://dev.mysql.com/doc/refman/8.0/en/innodb-undo-tablespaces.html
https://www.alibabacloud.com/blog/an-in-depth-analysis-of-undo-logs-in-innodb_598966
https://blog.csdn.net/wudi53433927/article/details/127206842
15.6.6 Undo Logs
https://dev.mysql.com/doc/refman/8.0/en/innodb-undo-logs.html