1. 背景mysql
* 一個通用的表空間是一個共享的InnoDB表空間。sql
* 與系統表空間相似,通常的表空間是共享的表空間,能夠存儲多個表的數據bash
* 通常的表空間比文件表的表空間具備潛在的內存優點。ide
* MySQL 將表空間元數據保存到一個表空間的生命週期中。在更少的通常表空間中,多個表對錶空間元數據的內存比在單獨的文件表空間表空間中的相同數量的表要少。 性能
* 通常的表空間數據文件可能放在一個相對於MySQL數據目錄的目錄中,它爲您提供了許多文件表空間的數據文件和存儲管理功能。與文件表的表空間同樣,在MySQL數據目錄以外放置數據文件的能力容許您單獨管理關鍵表的性能,爲特定的表設置RAID或DRBD,或者將表綁定到特定的磁盤。測試
* MySQL 5.7開始支持通用表空間管理功能。
spa
2. MySQL 環境orm
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.18 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show variables like 'version'; +---------------+--------+ | Variable_name | Value | +---------------+--------+ | version | 5.7.18 | +---------------+--------+ 1 row in set (0.01 sec) mysql> show variables like 'datadir'; +---------------+-------------------+ | Variable_name | Value | +---------------+-------------------+ | datadir | /data/mysql_data/ | +---------------+-------------------+ 1 row in set (0.04 sec)
3. 建立通用表空間生命週期
* 建立表空間文件存放目錄ip
[root@MySQL mytest]# mkdir -v /mysql_general_data mkdir: created directory `/mysql_general_data'
* 查看mysqld 運行用戶
[root@MySQL mytest]# ps aux | grep mysqld | grep -v grep root 1468 0.0 0.0 110400 1532 ? S 16:00 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql_data --pid-file=/data/mysql_data/MySQL.pid mysql 1614 0.1 5.0 1309380 196656 ? Sl 16:00 0:06 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql_data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql_data/error.log --pid-file=/data/mysql_data/MySQL.pid
* 修改表空間文件存放目錄所屬用戶與組爲mysql運行用戶 [ 此步必須 ]
[root@MySQL mytest]# chown -v mysql.mysql /mysql_general_data changed ownership of `/mysql_general_data' to mysql:mysql
* 建立通用表空間
ADD datafile: 指定通用表空間文件存放路徑
FILE_BLOCK_SIZE: 指定文件塊大小,推薦與Innodb_page_size參數大小對應
ENGINE: 指定存儲引擎
mysql> CREATE TABLESPACE ts1 ADD datafile '/mysql_general_data/ts1.ibd' FILE_BLOCK_SIZE=16384 ENGINE=InnoDB; Query OK, 0 rows affected (0.06 sec) mysql> show variables like 'innodb_page_size'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | innodb_page_size | 16384 | +------------------+-------+ 1 row in set (0.02 sec)
* 查看通用表空間文件
mysql> system ls -l /mysql_general_data; total 64 -rw-r----- 1 mysql mysql 65536 Jul 5 17:15 ts1.ibd
4. 測試通用表空間文件
* 使用通用表空間做爲數據存儲建立表
mysql> CREATE TABLE test_general( -> id BIGINT PRIMARY KEY NOT NULL AUTO_INCREMENT, -> name VARCHAR(64) NOT NULL -> )ENGINE=InnoDB TABLESPACE=ts1 DEFAULT CHARSET=utf8mb4; Query OK, 0 rows affected (0.04 sec)
* 查看錶信息
mysql> show create table test_general; +--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | test_general | CREATE TABLE `test_general` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(64) NOT NULL, PRIMARY KEY (`id`) ) /*!50100 TABLESPACE `ts1` */ ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 | +--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.06 sec)
* 查看錶文件
mysql> select database(); +------------+ | database() | +------------+ | mytest | +------------+ 1 row in set (0.01 sec) mysql> system ls -l /data/mysql_data/mytest; total 16 -rw-r----- 1 mysql mysql 67 Jul 5 16:30 db.opt -rw-r----- 1 mysql mysql 8586 Jul 5 17:19 test_general.frm
5. 刪除表空間文件
* 有表佔用時直接刪除
mysql> drop tablespace ts1; ERROR 1529 (HY000): Failed to drop TABLESPACE ts1
* 先刪除佔用表,再刪除
mysql> drop table test_general; Query OK, 0 rows affected (0.04 sec) mysql> drop tablespace ts1; Query OK, 0 rows affected (0.03 sec)
6. 總結
以需求驅動技術,技術自己沒有優略之分,只有業務之分。