aspx網站開發(fā) 案例網站代運營推廣
文章目錄
- MySQL 8.0 InnoDB Tablespaces之General Tablespaces(通用表空間/一般表空間)
- General tablespaces(通用表空間/一般表空間)
- 通用表空間的功能
- 通用表空間的限制
- 創(chuàng)建通用表空間(一般表空間)
- 創(chuàng)建語法
- 創(chuàng)建通用表空間
- 例1:不指定數(shù)據(jù)文件路徑
- 例2: 省略add datafile語句
- 例3:創(chuàng)建datadir路徑外的表空間
- innodb_directories變量參數(shù)
- 例:創(chuàng)建datadir路徑外的表空間
- 修改innodb_directories變量參數(shù)
- 創(chuàng)建datadir路徑外的表空間
- 例4:創(chuàng)建InnoDB通用表空間的限制
- 通用表空間中的表
- 例1:創(chuàng)建表指定通用表空間
- 例2:修改表的表空間
- 例2-1:修改到不同的通用表空間中
- 例2-2:修改表從獨立表空間或系統(tǒng)表空間到通用表空間
- 例2-3:修改表從通用表空間或系統(tǒng)表空間到獨立表空間
- 例2-4:修改表從通用表空間或獨立表空間到系統(tǒng)表空間
- 參考
【免責聲明】文章僅供學習交流,觀點代表個人,與任何公司無關。
編輯|SQL和數(shù)據(jù)庫技術(ID:SQLplusDB)
MySQL 8.0 OCP (1Z0-908) 考點概要
MySQL 8.0 OCP (1Z0-908) 考點精析-安裝與配置考點1:設置系統(tǒng)變量
【MySQL】控制MySQL優(yōu)化器行為方法之optimizer_switch系統(tǒng)變量
【MySQL】MySQL系統(tǒng)變量(system variables)列表(mysqld --verbose --help的結果例)
【MySQL】MySQL系統(tǒng)變量(system variables)列表(SHOW VARIABLES 的結果例)
MySQL 8.0 OCP (1Z0-908) 考點精析-備份與恢復考點1:MySQL Enterprise Backup概要
MySQL 8.0 OCP (1Z0-908) 考點精析-性能優(yōu)化考點1:sys.statement_analysis視圖
MySQL 8.0 OCP (1Z0-908) 考點精析-性能優(yōu)化考點2:系統(tǒng)變量的確認
MySQL 8.0 OCP (1Z0-908) 考點精析-性能優(yōu)化考點3:EXPLAIN ANALYZE
MySQL 8.0 OCP (1Z0-908) 考點精析-性能優(yōu)化考點4:慢速查詢日志(slow query log)
MySQL 8.0 OCP (1Z0-908) 考點精析-性能優(yōu)化考點5:表連接算法(join algorithm)
MySQL 8.0 OCP (1Z0-908) 考點精析-性能優(yōu)化考點6:MySQL Enterprise Monitor之Query Analyzer
MySQL 8.0 OCP (1Z0-908) 考點精析-架構考點1:二進制日志文件(Binary log)
MySQL 8.0 OCP (1Z0-908) 考點精析-架構考點5:數(shù)據(jù)字典(Data Dictionary)
MySQL 8.0 OCP (1Z0-908) 考點精析-架構考點6:InnoDB Tablespaces之系統(tǒng)表空間(System Tablespace)
MySQL 8.0 InnoDB Tablespaces之File-per-table tablespaces(單獨表空間)
MySQL 8.0 InnoDB Tablespaces之General Tablespaces(通用表空間/一般表空間)
InnoDB表空間是MySQL中用于存儲InnoDB存儲引擎表數(shù)據(jù)和索引的物理文件。
InnoDB表空間根據(jù)用途可以分成多種類型:
- 數(shù)據(jù)表空間:
- System tablespace(系統(tǒng)表空間)
- File-per-table tablespaces(單獨表空間)
- General tablespaces(通用表空間/一般表空間)
- Undo 表空間
- 臨時表空間(Temporary table tablespaces)
General tablespaces(通用表空間/一般表空間)
通用表空間具有如下功能和限制。
通用表空間的功能
通用表空間具有如下功能:
- 使用CREATE TABLESPACE語法創(chuàng)建的共享InnoDB表空間(與Oracle的表空間很相似)。
- 類似于系統(tǒng)表空間,通用表空間是共享表空間,可以存儲多個表的數(shù)據(jù)。 與獨立表空間相比,通用表空間具有潛在的內存優(yōu)勢。(通用表空間中多個表共享一個表空間,所以消耗更少的內存用于表空間元數(shù)據(jù),獨立表空間需要更多內存用于表空間元數(shù)據(jù)。)
- 數(shù)據(jù)文件可以放置在與MySQL數(shù)據(jù)目錄( MySQL data directory)相關或獨立的目錄中。
- 通用表空間支持所有的表行格式和相關功能(REDUNDANT, COMPACT, DYNAMIC, COMPRESSED)。 CREATE
- TABLE語句可以使用TABLESPACE選項在通用表空間、獨立表空間或系統(tǒng)表空間中創(chuàng)建表。 ALTER
- TABLE語句可以使用TABLESPACE選項在通用表空間、獨立表空間或系統(tǒng)表空間之間移動表。
- 創(chuàng)建通用表空間時候會生成相應的.ibd數(shù)據(jù)文件。 通用表空間不屬于特定的數(shù)據(jù)庫。
通用表空間的限制
通用表空間具有如下限制:
- 無法將現(xiàn)有的表空間更改為通用表空間。
- 不支持創(chuàng)建臨時通用表空間。
- 通用表空間不支持臨時表。
- 與系統(tǒng)表空間類似,truncate 或者drop通用表空間中的表,僅釋放系統(tǒng)表空間中的空間,不會返回給操作系統(tǒng)。
- 表復制的ALTER TABLE操作可能會增加表空間使用的空間量。
- 不支持表分區(qū)(table partitions)放置在通用表空間中
- 源和副本位于同一主機上的復制環(huán)境中,不支持使用ADD DATAFILE子句。
- MySQL 8.0.21開始,由datadir、innodb_data_home_dir和innodb_directories變量定義的目錄以外,無法在撤銷表空間目錄(innodb_undo_directory)中創(chuàng)建通用表空間。
創(chuàng)建通用表空間(一般表空間)
創(chuàng)建語法
通用表空間/一般表空間創(chuàng)建語法如下:
CREATE [UNDO] TABLESPACE tablespace_nameInnoDB and NDB:[ADD DATAFILE 'file_name'][AUTOEXTEND_SIZE [=] value]InnoDB only:[FILE_BLOCK_SIZE = value][ENCRYPTION [=] {'Y' | 'N'}]NDB only:USE LOGFILE GROUP logfile_group[EXTENT_SIZE [=] extent_size][INITIAL_SIZE [=] initial_size][MAX_SIZE [=] max_size][NODEGROUP [=] nodegroup_id][WAIT][COMMENT [=] 'string']InnoDB and NDB:[ENGINE [=] engine_name]Reserved for future use:[ENGINE_ATTRIBUTE [=] 'string']
創(chuàng)建通用表空間
例1:不指定數(shù)據(jù)文件路徑
可以不指定數(shù)據(jù)文件路徑創(chuàng)建通用表空間,這時候數(shù)據(jù)文件會默認創(chuàng)建在datadir路徑下。
創(chuàng)建表空間:
mysql> create tablespace test_tbs1 add datafile 'test_tbs1.ibd';
Query OK, 0 rows affected (0.01 sec)mysql> show variables like 'datadir';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| datadir | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.01 sec)mysql> select * from-> information_schema.innodb_tablespaces t-> join information_schema.innodb_datafiles d-> on t.SPACE=d.SPACE-> where t.NAME ='test_tbs1'\G
*************************** 1. row ***************************SPACE: 81NAME: test_tbs1FLAG: 18432ROW_FORMAT: AnyPAGE_SIZE: 16384ZIP_PAGE_SIZE: 0SPACE_TYPE: GeneralFS_BLOCK_SIZE: 4096FILE_SIZE: 114688ALLOCATED_SIZE: 114688
AUTOEXTEND_SIZE: 0SERVER_VERSION: 8.0.35SPACE_VERSION: 1ENCRYPTION: NSTATE: normalSPACE: 0x3831PATH: test_tbs1.ibd
1 row in set (0.00 sec)mysql>
查看數(shù)據(jù)文件:
root@mysql-vm:/var/lib/mysql# ls -l test_tbs*
-rw-r----- 1 mysql mysql 114688 Dec 20 20:32 test_tbs1.ibd
例2: 省略add datafile語句
MySQL 8.0.14版本之后,創(chuàng)建表空間時可以省略add datafile語句。這時MySQL會隱式創(chuàng)建具有唯一文件名的表空間數(shù)據(jù)文件。
- 文件名是一個128位的UUID,格式為五組用破折號分隔的十六進制數(shù)字(aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee)。
- 常規(guī)表空間數(shù)據(jù)文件包括一個.ibd文件擴展名。 在replication環(huán)境中,在源上創(chuàng)建的數(shù)據(jù)文件名與在復制副本上創(chuàng)建的文件名不同。
例:
mysql> create tablespace test_tbs2;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from-> information_schema.innodb_tablespaces t-> join information_schema.innodb_datafiles d-> on t.SPACE=d.SPACE-> where t.NAME ='test_tbs2'\G
*************************** 1. row ***************************SPACE: 82NAME: test_tbs2FLAG: 18432ROW_FORMAT: AnyPAGE_SIZE: 16384ZIP_PAGE_SIZE: 0SPACE_TYPE: GeneralFS_BLOCK_SIZE: 4096FILE_SIZE: 114688ALLOCATED_SIZE: 114688
AUTOEXTEND_SIZE: 0SERVER_VERSION: 8.0.35SPACE_VERSION: 1ENCRYPTION: NSTATE: normalSPACE: 0x3832PATH: af5bb7e2-9f34-11ee-8d33-525400b409f6.ibd
1 row in set (0.00 sec)mysql>
查看數(shù)據(jù)文件:
root@mysql-vm:/var/lib/mysql# ls -l af5bb7e2-9f34-11ee-8d33-525400b409f6.ibd
-rw-r----- 1 mysql mysql 114688 Dec 20 20:38 af5bb7e2-9f34-11ee-8d33-525400b409f6.ibd
root@mysql-vm:/var/lib/mysql#
例3:創(chuàng)建datadir路徑外的表空間
通用表空間數(shù)據(jù)文件可以放置在數(shù)據(jù)目錄之外的位置,但是路徑必須是innodb_directories的值或者附加到innodb_directories值的變量(innodb_data_home_dir、innodb_undo_directory和datadir)之一。
innodb_directories變量參數(shù)
innodb_directories變量參數(shù)內容如下:
Command-Line Format | –innodb-directories=dir_name |
---|---|
System Variable | innodb_directories |
Scope | Global |
Dynamic | No |
SET_VAR Hint Applies | No |
Type | Directory name |
Default Value | NULL |
innodb_directories參數(shù)的默認值為NULL。但是innodb_data_home_dir、innodb_undo_directory和datadir定義的目錄會會附加到innodb_directories參數(shù)值上。
例:
mysql> show variables like 'innodb_directories';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| innodb_directories | |
+--------------------+-------+
1 row in set (0.01 sec)mysql> show variables like 'innodb_data_home_dir';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| innodb_data_home_dir | |
+----------------------+-------+
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.01 sec)mysql> show variables like 'datadir';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| datadir | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.00 sec)mysql>
/var/lib/mysql-files
drwx------ 12 mysql mysql 4096 Dec 24 22:10 mysql
參考:
https://dev.mysql.com/doc/refman/8.0/en/innodb-parameters.html#sysvar_innodb_directories
修改innodb_directories變量參數(shù)需要修改MySQL配置文件,并重新啟動MySQL服務。
例:創(chuàng)建datadir路徑外的表空間
如果在未定義路徑中創(chuàng)建通用表空間,由于不是已知目錄,會發(fā)生如下ERROR 3121 (HY000)錯誤。
mysql> show variables like 'innodb_directories';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| innodb_directories | |
+--------------------+-------+
1 row in set (0.01 sec)mysql> create tablespace sqlplusdb_tb1 add datafile '/var/lib/mysql-files/sqlplusdb_tb1.ibd';
ERROR 3121 (HY000): The DATAFILE location must be in a known directory.
mysql>
可以通過修改innodb_directories變量參數(shù)添加已知目錄。
修改innodb_directories變量參數(shù)
添加如下內容(innodb_directories=/var/lib/mysql-files)到MySQL配置文件中。
例:innodb_directories=/var/lib/mysql-files
root@mysql-vm:/etc/mysql# vi /etc/mysql/mysql.conf.d/mysqld.cnf
root@mysql-vm:/etc/mysql# systemctl stop mysql
root@mysql-vm:/etc/mysql# systemctl start mysql
創(chuàng)建datadir路徑外的表空間
查看變量并創(chuàng)建datadir路徑外的表空間的表空間
mysql> show variables like 'innodb_directories';
+--------------------+----------------------+
| Variable_name | Value |
+--------------------+----------------------+
| innodb_directories | /var/lib/mysql-files |
+--------------------+----------------------+
1 row in set (0.00 sec)mysql> create tablespace test_tb3 add datafile '/var/lib/mysql-files/test_tb3.ibd';
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> select * from-> information_schema.innodb_tablespaces t-> join information_schema.innodb_datafiles d-> on t.SPACE=d.SPACE-> where t.NAME ='test_tb3'\G
*************************** 1. row ***************************SPACE: 92NAME: test_tb3FLAG: 18432ROW_FORMAT: AnyPAGE_SIZE: 16384ZIP_PAGE_SIZE: 0SPACE_TYPE: GeneralFS_BLOCK_SIZE: 4096FILE_SIZE: 114688ALLOCATED_SIZE: 114688
AUTOEXTEND_SIZE: 0SERVER_VERSION: 8.0.35SPACE_VERSION: 1ENCRYPTION: NSTATE: normalSPACE: 0x3932PATH: /var/lib/mysql-files/test_tb3.ibd
1 row in set (0.00 sec)mysql>
參考:
MySQL 8.0 OCP (1Z0-908) 考點精析-安裝與配置考點1:設置系統(tǒng)變量
例4:創(chuàng)建InnoDB通用表空間的限制
可以在數(shù)據(jù)目錄(datadir)中創(chuàng)建通用表空間。
mysql> create tablespace sqlplusdb_tb1 add datafile '/var/lib/mysql/sqlplusdb_tb1.ibd';
Query OK, 0 rows affected (0.02 sec)
但是為了避免與隱式創(chuàng)建的每個表一個文件的表空間發(fā)生沖突,不支持在數(shù)據(jù)目錄(datadir)的子目錄中創(chuàng)建InnoDB通用表空間。
如果在數(shù)據(jù)目錄(datadir)的子目錄中創(chuàng)建InnoDB通用表空間會報ERROR 3121 (HY000)錯誤。
例:
mysql> show variables like 'datadir';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| datadir | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.00 sec)mysql> create tablespace sqlplusdb_tb2 add datafile '/var/lib/mysql/testdb/sqlplusdb_tb1.ibd';
ERROR 3121 (HY000): The DATAFILE location cannot be under the datadir.
mysql>
通用表空間中的表
和Oracle一樣,MySQL在創(chuàng)建表時可以指定表空間也可以修改表的表空間。
例1:創(chuàng)建表指定通用表空間
mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> CREATE TABLE test_t1 (c1 INT) TABLESPACE test_tbs1 ;
Query OK, 0 rows affected (0.01 sec)mysql>
mysql> SHOW CREATE TABLE test_t1;
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| test_t1 | CREATE TABLE `test_t1` (`c1` int DEFAULT NULL
) /*!50100 TABLESPACE `test_tbs1` */ ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)mysql>
mysql> select-> name as 'table_name',-> space_type-> from-> information_schema.innodb_tables-> where-> name like '%test_t1%' \g
+----------------+------------+
| table_name | space_type |
+----------------+------------+
| testdb/test_t1 | General |
+----------------+------------+
1 row in set (0.08 sec)
例2:修改表的表空間
通過ALTER TABLE語句的TABLESPACE選項可以修改表的表空間,將表在通用表空間、獨立表空間或系統(tǒng)表空間之間進行移動。
需要注意的是:
- ALTER TABLE ... TABLESPACE操作將導致對表進行全表重建(即使TABLESPACE屬性未發(fā)生更改也會重建)。
- ALTER TABLE ... TABLESPACE語法不支持將表從臨時表空間移動到持久表空間。
例2-1:修改到不同的通用表空間中
通過ALTER TABLE tbl_name TABLESPACE [=] tablespace_name修改表到不同的通用表空間中
例:
mysql> SHOW CREATE TABLE test_t1;
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| test_t1 | CREATE TABLE `test_t1` (`c1` int DEFAULT NULL
) /*!50100 TABLESPACE `test_tbs1` */ ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)mysql> ALTER TABLE test_t1 tablespace=sqlplusdb_tb1;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> SHOW CREATE TABLE test_t1;
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test_t1 | CREATE TABLE `test_t1` (`c1` int DEFAULT NULL
) /*!50100 TABLESPACE `sqlplusdb_tb1` */ ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)mysql>
例2-2:修改表從獨立表空間或系統(tǒng)表空間到通用表空間
可以通過如下命令修改表從獨立表空間或系統(tǒng)表空間到通用表空間。
ALTER TABLE tbl_name TABLESPACE [=] tablespace_name
例:修改表從獨立表空間到通用表空間
mysql> select-> name as 'table_name',-> space_type-> from-> information_schema.innodb_tables-> where-> name ='testdb/test_file_pertable_tablespace' \g
+--------------------------------------+------------+
| table_name | space_type |
+--------------------------------------+------------+
| testdb/test_file_pertable_tablespace | Single |
+--------------------------------------+------------+
1 row in set (0.00 sec)mysql> alter table test_file_pertable_tablespace tablespace=test_tbs1;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> select-> name as 'table_name',-> space_type-> from-> information_schema.innodb_tables-> where-> name ='testdb/test_file_pertable_tablespace' \g
+--------------------------------------+------------+
| table_name | space_type |
+--------------------------------------+------------+
| testdb/test_file_pertable_tablespace | General |
+--------------------------------------+------------+
1 row in set (0.00 sec)mysql>
例:修改表從系統(tǒng)表空間到通用表空間
mysql> select-> name as 'table_name',-> space_type-> from-> information_schema.innodb_tables-> where-> name ='testdb/test_file_pertable' \g
+---------------------------+------------+
| table_name | space_type |
+---------------------------+------------+
| testdb/test_file_pertable | System |
+---------------------------+------------+
1 row in set (0.01 sec)mysql> alter table test_file_pertable tablespace=test_tbs1;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> select-> name as 'table_name',-> space_type-> from-> information_schema.innodb_tables-> where-> name ='testdb/test_file_pertable' \g
+---------------------------+------------+
| table_name | space_type |
+---------------------------+------------+
| testdb/test_file_pertable | General |
+---------------------------+------------+
1 row in set (0.00 sec)mysql>
例2-3:修改表從通用表空間或系統(tǒng)表空間到獨立表空間
可以通過如下命令修改表從通用表空間或系統(tǒng)表空間到獨立表空間。
ALTER TABLE tbl_name TABLESPACE [=] innodb_file_per_table;
例:修改表從通用表空間到獨立表空間
mysql> select-> name as 'table_name',-> space_type-> from-> information_schema.innodb_tables-> where-> name ='testdb/test_file_pertable' \g
+---------------------------+------------+
| table_name | space_type |
+---------------------------+------------+
| testdb/test_file_pertable | General |
+---------------------------+------------+
1 row in set (0.00 sec)mysql> alter table test_file_pertable tablespace=innodb_file_per_table;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> select-> name as 'table_name',-> space_type-> from-> information_schema.innodb_tables-> where-> name ='testdb/test_file_pertable' \g
+---------------------------+------------+
| table_name | space_type |
+---------------------------+------------+
| testdb/test_file_pertable | Single |
+---------------------------+------------+
1 row in set (0.01 sec)mysql>
例2-4:修改表從通用表空間或獨立表空間到系統(tǒng)表空間
可以通過如下命令修改表從通用表空間或獨立表空間到系統(tǒng)表空間。
ALTER TABLE tbl_name TABLESPACE [=] innodb_system;
例:修改表從獨立表空間到系統(tǒng)表空間
mysql> select-> name as 'table_name',-> space_type-> from-> information_schema.innodb_tables-> where-> name ='testdb/test_file_pertable' \g
+---------------------------+------------+
| table_name | space_type |
+---------------------------+------------+
| testdb/test_file_pertable | Single |
+---------------------------+------------+
1 row in set (0.00 sec)mysql>
mysql> alter table test_file_pertable tablespace=innodb_system;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> select-> name as 'table_name',-> space_type-> from-> information_schema.innodb_tables-> where-> name ='testdb/test_file_pertable' \g
+---------------------------+------------+
| table_name | space_type |
+---------------------------+------------+
| testdb/test_file_pertable | System |
+---------------------------+------------+
1 row in set (0.00 sec)
參考
15.6.3.3 General Tablespaces
https://dev.mysql.com/doc/refman/8.0/en/general-tablespaces.html
13.1.21 CREATE TABLESPACE Statement
https://dev.mysql.com/doc/refman/8.0/en/create-tablespace.html
Chapter 2 Server Error Message Reference
https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html
Error number: 3121; Symbol: ER_WRONG_FILE_NAME; SQLSTATE: HY000
Message: Incorrect File Name ‘%s’.