MySQLINNODB表空间分类简单介绍
从功能上看
INNODB表空间共以下几种:
从共享与否角度看
上边提到的五种表空间可以这么归类。
- 共享表空间
- 独立表空间
官档中有明确提到“共享表空间”这个说法:
https://dev.mysql.com/doc/refman/8.0/en/general-tablespaces.html
或者比如将分区表更改为共享表空间则会提示报错:
(root@localhost 16:57:48) [zkm](10)> alter table zkm.employees tablespace tbs01; ERROR 1478 (HY000): InnoDB : A partitioned table is not allowed in a shared tablespace.
至于独立表空间,暂时没找到官档的说法,是网络上大家的说法,相对于共享表空间来说很准确。
共享角度和功能两者的关系
共享表空间包含(官档来源):防偷防爬
- 系统表空间
- 通用表空间
https://dev.mysql.com/doc/refman/8.0/en/innodb-file-per-table-tablespaces.html
而独立表空间:
- File-Per-Table表空间
简单说下系统表空间、File-Per-Table表空间、通用表空间的创建
系统表空间:
通过这两个参数控制:innodb_data_file_path、innodb_data_home_dir
(root@localhost 18:08:11) [information_schema](13)> show variables like 'innodb_data%'; +-----------------------+------------------------+ | Variable_name | Value | +-----------------------+------------------------+ | innodb_data_file_path | ibdata1:12M:autoextend | | innodb_data_home_dir | | +-----------------------+------------------------+ 2 rows in set (0.00 sec)
innodb_data_file_path是控制系统表空间文件的数量,绝对路径文件位置,名称,大小,是否自动扩展,最大值等,eg:innodb_data_file_path=ibdata1:76M;ibdata2:64M;/sys_tbs2/ibdata3:12M;ibdata4:12M:autoextend:max:256M
innodb_data_home_dir是控制系统表空间文件位置的参数,默认值为datadir的值。
(root@localhost 18:43:56) [information_schema](8)> select * from information_schema.INNODB_TABLESPACES_BRIEF where name='innodb_system'; +--------------+---------------+-------------------+--------------+------------+ | SPACE | NAME | PATH | FLAG | SPACE_TYPE | +--------------+---------------+-------------------+--------------+------------+ | 0x30 | innodb_system | ibdata1 | 0x3138343332 | System | | 0x30 | innodb_system | /sys_tbs/ibdata2 | 0x3138343332 | System | | 0x30 | innodb_system | ibdata3 | 0x3138343332 | System | | 0x30 | innodb_system | ibdata2 | 0x3138343332 | System | | 0x30 | innodb_system | /sys_tbs2/ibdata3 | 0x3138343332 | System | | 0x30 | innodb_system | ibdata4 | 0x3138343332 | System | +--------------+---------------+-------------------+--------------+------------+ 6 rows in set (0.00 sec)
File-Per-Table表空间:
开启File-Per-Table表空间属性,即启动innodb_file_per_table变量(默认启用)的情况下,创建表的时候自动在变量datadir下创建库名文件夹/表名.ibd
(root@localhost 18:47:21) [information_schema](8)> select @@innodb_file_per_table; +-------------------------+ | @@innodb_file_per_table | +-------------------------+ | 1 | +-------------------------+ 1 row in set (0.00 sec) (root@localhost 18:49:01) [information_schema](8)> create database zkm; Query OK, 1 row affected (0.00 sec) (root@localhost 18:49:04) [information_schema](8)> CREATE TABLE zkm.t1 (c1 INT PRIMARY KEY) engine=innodb; Query OK, 0 rows affected (0.02 sec) [root@dev-app80 data]# ll /data/mysql3306/data/zkm/t1.ibd -rw-r----- 1 mysql mysql 114688 Jun 30 18:49 /data/mysql3306/data/zkm/t1.ibd
也可以不需要开启File-Per-Table表空间属性,但是要结合create table ... tablespace = innodb_file_per_table(可以结合data directory子句将t2.ibd放到其他位置,详见MySQL如何创建外部表)来创建。
(root@localhost 18:51:31) [information_schema](8)> set global innodb_file_per_table = off; Query OK, 0 rows affected (0.00 sec) (root@localhost 18:51:41) [information_schema](8)> select @@innodb_file_per_table; +-------------------------+ | @@innodb_file_per_table | +-------------------------+ | 0 | +-------------------------+ 1 row in set (0.00 sec) (root@localhost 18:51:44) [information_schema](8)> CREATE TABLE zkm.t2 (c1 INT PRIMARY KEY) tablespace=innodb_file_per_table engine=innodb; Query OK, 0 rows affected (0.01 sec) [root@dev-app80 data]# ll /data/mysql3306/data/zkm/ total 224 -rw-r----- 1 mysql mysql 114688 Jun 30 18:49 t1.ibd -rw-r----- 1 mysql mysql 114688 Jun 30 18:52 t2.ibd
根据观察到的规律,File-Per-Table表空间的名称都带有'/',并且*.ibd都是伴随着表创建产生的。
(root@localhost 18:55:22) [(none)](9)> select * from information_schema.INNODB_TABLESPACES_BRIEF where name like '%/%'; +--------------+----------------+----------------------+--------------+------------+ | SPACE | NAME | PATH | FLAG | SPACE_TYPE | +--------------+----------------+----------------------+--------------+------------+ | 0x31 | sys/sys_config | ./sys/sys_config.ibd | 0x3136343137 | Single | | 0x3334 | zkm/t1 | ./zkm/t1.ibd | 0x3136343137 | Single | | 0x3335 | zkm/t2 | ./zkm/t2.ibd | 0x3136343137 | Single | +--------------+----------------+----------------------+--------------+------------+ 3 rows in set (0.00 sec)
如果是外部表创建,要结合innodb_directories,不然无法将表创建到变量datadir意外的目录。
通用表空间:
设置innodb_directories,可以将通用表空间创建到innodb_directories所指定的目录下,注意设置innodb_directories需要重启生效。
(root@localhost 19:03:46) [(none)](9)> create tablespace common_tbs add datafile '/mysqldata_tmp/common_tbs01.ibd' engine=innodb; Query OK, 0 rows affected (0.01 sec) [root@dev-app80 mysqldata_tmp]# ll /mysqldata_tmp/ total 112 -rw-r----- 1 mysql mysql 114688 Jun 30 19:04 common_tbs01.ibd
表空间名称就是你指定时候的名称。
(root@localhost 19:05:34) [(none)](9)> select * from information_schema.INNODB_TABLESPACES_BRIEF WHERE name='common_tbs'; +--------------+------------+---------------------------------+--------------+------------+ | SPACE | NAME | PATH | FLAG | SPACE_TYPE | +--------------+------------+---------------------------------+--------------+------------+ | 0x3336 | common_tbs | /mysqldata_tmp/common_tbs01.ibd | 0x3138343332 | General | +--------------+------------+---------------------------------+--------------+------------+ 1 row in set (0.00 sec)
PS:如果将一个普通的表从独立表空间变为共享表空间,则独立表空间对应的.ibd文件会被删除回收。
如果将一个普通的表从共享表空间变为另外一个共享表空间,则原来的共享表空间对应的.ibd文件不会有任何变化。
alter table t tablespace new_tablespace;
标签:MySQLINNODB,root,分类,ibd,innodb,简单,空间,table,data From: https://www.cnblogs.com/PiscesCanon/p/17517656.html