MySQL 5.7新支持--------通用表空間實戰
1. 背景
* 一個通用的表空間是一個共享的InnoDB表空間。
* 與系統表空間類似,一般的表空間是共享的表空間,可以存儲多個表的數據
* 一般的表空間比文件表的表空間具有潛在的內存優勢。
* MySQL 將表空間元數據保存到一個表空間的生命周期中。在更少的一般表空間中,多個表對表空間元數據的內存比在單獨的文件表空間表空間中的相同數量的表要少。
* 一般的表空間數據文件可能放在一個相對於MySQL數據目錄的目錄中,它為您提供了許多文件表空間的數據文件和存儲管理功能。與文件表的表空間一樣,在MySQL數據目錄之外放置數據文件的能力允許您單獨管理關鍵表的性能,為特定的表設置RAID或DRBD,或者將表綁定到特定的磁盤。
* MySQL 5.7開始支持通用表空間管理功能。
2. MySQL 環境
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. 創建通用表空間
* 創建表空間文件存放目錄
[[email protected] mytest]# mkdir -v /mysql_general_data mkdir: created directory `/mysql_general_data‘
* 查看mysqld 運行用戶
[[email protected] 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運行用戶 [ 此步必須 ]
[[email protected] 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. 總結
以需求驅動技術,技術本身沒有優略之分,只有業務之分。
本文出自 “sea” 博客,請務必保留此出處http://lisea.blog.51cto.com/5491873/1945446
MySQL 5.7新支持--------通用表空間實戰