新特性解读 | 趋近完美的 Undo 空间
作者:杨涛涛
资深数据库专家,专研 MySQL 十余年。擅长 MySQL、PostgreSQL、MongoDB 等开源数据库相关的备份恢复、SQL 调优、监控运维、高可用架构设计等。目前任职于爱可生,为各大运营商及银行金融企业提供 MySQL 相关技术支持、MySQL 相关课程培训等工作。
本文来源:原创投稿
*爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。
在说 Undo 表空间前,先来简单说下 Undo Log 的概念。
undo log segment(undo segment) Undo Logs 合集。undo segment 可以被重复使用,但是一次只能由一个事务占用。 rollback segment 也就是 Undo Logs 的物理存储区域。 undo tablespace rollback segment 被从系统表空间里分离出来后的实际磁盘文件表现形式。
MySQL 5.6
MySQL 5.7
MySQL 8.0
root@ytt-pc:/var/lib/mysql/3304# ls -sihl undo*
919027 14M -rw-r----- 1 mysql mysql 14M 3月 20 11:00 undo_001
918943 12M -rw-r----- 1 mysql mysql 12M 3月 20 11:00 undo_002
mysql> drop undo tablespace innodb_undo_001;
ERROR 3119 (42000): InnoDB: Tablespace names starting with `innodb_` are reserved.
mysql> show errors;
+-------+------+----------------------------------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------------------------------+
| Error | 3119 | InnoDB: Tablespace names starting with `innodb_` are reserved. |
| Error | 3119 | Incorrect tablespace name `innodb_undo_001` |
+-------+------+----------------------------------------------------------------+
2 rows in set (0.00 sec)
2、可设置回滚段数量的参数
3、自动收缩参数
4、废弃的参数
CREATE [UNDO] TABLESPACE tablespace_name
InnoDB and NDB:
[ADD DATAFILE 'file_name']
InnoDB only:
[FILE_BLOCK_SIZE = value]
[ENCRYPTION [=] {'Y' | 'N'}]
NDB only:
USE LOGFILE GROUP logfile_group
[EXTENT_SIZE [=] extent_size]
[INITIAL_SIZE [=] initial_size]
[AUTOEXTEND_SIZE [=] autoextend_size]
[MAX_SIZE [=] max_size]
[NODEGROUP [=] nodegroup_id]
[WAIT]
[COMMENT [=] 'string']
InnoDB and NDB:
[ENGINE [=] engine_name]
1、创建表空间
语法方面和 MySQL 5.7 创建通用表空间类似,并且是从 MySQL NDB 引擎上借鉴过来的。
mysql> create undo tablespace undo_ts1 add datafile 'undo_ts1.ibu';
Query OK, 0 rows affected (0.47 sec)
2、查看表空间
mysql> select * from information_schema.innodb_tablespaces where SPACE_TYPE='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: 0
FILE_SIZE: 0
ALLOCATED_SIZE: 0
SERVER_VERSION: 8.0.18
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: 0
FILE_SIZE: 0
ALLOCATED_SIZE: 0
SERVER_VERSION: 8.0.18
SPACE_VERSION: 1
ENCRYPTION: N
STATE: active
*************************** 3. row ***************************
SPACE: 4294967277
NAME: undo_ts1
FLAG: 0
ROW_FORMAT: Undo
PAGE_SIZE: 16384
ZIP_PAGE_SIZE: 0
SPACE_TYPE: Undo
FS_BLOCK_SIZE: 0
FILE_SIZE: 0
ALLOCATED_SIZE: 0
SERVER_VERSION: 8.0.19
SPACE_VERSION: 1
ENCRYPTION: N
STATE: active
3 rows in set (0.00 sec)
# 默认在当前数据目录
mysql> select @@innodb_undo_directory;
+-------------------------+
| @@innodb_undo_directory |
+-------------------------+
| ./ |
+-------------------------+
1 row in set (0.00 sec)
# 刚建立的 undo_ts1.ibu.
root@ytt-pc:/var/lib/mysql/3304# ls -sihl undo_ts1.ibu
918978 10M -rw-r----- 1 mysql mysql 10M 3月 20 11:33 undo_ts1.ibu
# 指定undo 表空间目录/var/lib/mysql-files/,新建立一个undo_ts2.ibu.
mysql> create undo tablespace undo_ts2 add datafile '/var/lib/mysql-files/undo_ts2.ibu';
Query OK, 0 rows affected (0.30 sec)
# 创建一个 undo 表空间 undo_ts3,没有带后缀,MySQL 拒绝创建。
mysql> create undo tablespace undo_ts3 add datafile 'undo_ts3';
ERROR 3121 (HY000): The ADD DATAFILE filepath must end with '.ibu'.
mysql> show errors;
+-------+------+-------------------------------------------------+
| Level | Code | Message |
+-------+------+-------------------------------------------------+
| Error | 3121 | The ADD DATAFILE filepath must end with '.ibu'. |
| Error | 1528 | Failed to create UNDO TABLESPACE undo_ts3 |
| Error | 3121 | Incorrect File Name 'undo_ts3'. |
+-------+------+-------------------------------------------------+
3 rows in set (0.00 sec)
4、删除表空间
mysql> alter undo tablespace undo_ts2 set inactive;
Query OK, 0 rows affected (0.01 sec)
mysql> drop undo tablespace undo_ts2;
Query OK, 0 rows affected (0.01 sec)
5、移动表空间
mysql> select @@innodb_undo_directory;
+-------------------------+
| @@innodb_undo_directory |
+-------------------------+
| /var/lib/mysql-files |
+-------------------------+
1 row in set (0.00 sec)
mysql> select file_name,file_type,tablespace_name,status from files where file_type = 'undo log';
+-------------------------------+-----------+-----------------+--------+
| FILE_NAME | FILE_TYPE | TABLESPACE_NAME | STATUS |
+-------------------------------+-----------+-----------------+--------+
| /var/lib/mysql-files/undo_001 | UNDO LOG | innodb_undo_001 | NORMAL |
| /var/lib/mysql-files/undo_002 | UNDO LOG | innodb_undo_002 | NORMAL |
+-------------------------------+-----------+-----------------+--------+
2 rows in set (0.00 sec)
mysql> create undo tablespace undo_ts_new add datafile 'undo_ts_new.ibu';
Query OK, 0 rows affected (0.51 sec)
root@ytt-pc:/var/lib/mysql-files# ls -l undo*
-rw-r----- 1 mysql mysql 10485760 3月 20 12:00 undo_001
-rw-r----- 1 mysql mysql 10485760 3月 20 12:00 undo_002
-rw-r----- 1 mysql mysql 10485760 3月 20 12:00 undo_ts_new.ibu
社区近期动态