前言
從 MySQL5.7.11開始,MySQL對(duì)InnoDB支持存儲(chǔ)在單獨(dú)表空間中的表的數(shù)據(jù)加密 。此功能為物理表空間數(shù)據(jù)文件提供靜態(tài)加密。該加密是在引擎內(nèi)部數(shù)據(jù)頁級(jí)別的加密手段,在數(shù)據(jù)頁寫入文件系統(tǒng)時(shí)加密,加密用的是AES算法,而其解密是在從文件讀到內(nèi)存中時(shí)進(jìn)行。
1 配置加密插件
1.1 修改配置文件
在mysql配置文件【mysqld】x項(xiàng)中添加如下內(nèi)容
plugin_dir=/usr/local/mysql5.7/lib/mysql/plugin # 插件路徑,根據(jù)實(shí)際情況修改
early-plugin-load="keyring_file.so" # 加密插件
keyring_file_data=/data/mysql3306/keyring/keyring # 路徑不存在,需要?jiǎng)?chuàng)建
innodb_file_per_table=1 # 只作用于獨(dú)立表空間
1.2 創(chuàng)建加密所需的路徑并配置權(quán)限
創(chuàng)建時(shí)要注意 ,keyring_file_data 里配置的keyring會(huì)在啟動(dòng)時(shí)自動(dòng)創(chuàng)建,本步驟中創(chuàng)建到對(duì)應(yīng)目錄即可
mkdir -p /data/mysql3306/keyring/
chown -R mysql:mysql /data/mysql3306/keyring/
chmod 750 /data/mysql3306/keyring
1.3 重啟MySQL
重啟mysql即可,啟動(dòng)后會(huì)發(fā)現(xiàn)注意/data/mysql3306/keyring 目錄下生成了 keyring文件
注意,重啟后也要看一下mysql錯(cuò)誤日志里有沒有相關(guān)錯(cuò)誤信息,如果沒有錯(cuò)誤則繼續(xù)進(jìn)行
1.4 查看插件狀態(tài)
啟動(dòng)后可以查看插件是否生效
mysql> SELECT PLUGIN_NAME, PLUGIN_STATUS, PLUGIN_Type,PLUGIN_Library FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE 'keyring_file';
+--------------+---------------+-------------+-----------------+
| PLUGIN_NAME | PLUGIN_STATUS | PLUGIN_Type | PLUGIN_Library |
+--------------+---------------+-------------+-----------------+
| keyring_file | ACTIVE | KEYRING | keyring_file.so |
+--------------+---------------+-------------+-----------------+
1 row in set (0.01 sec)
或者用 show plugins命令查看
2 測(cè)試加密表空間
2.1 創(chuàng)建加密的新表
創(chuàng)建一張新表,并添加ENCRYPTION='Y' ,加密表空間
mysql> create table test1(
id int primary key auto_increment,
name varchar(20),
key name(name))
ENCRYPTION='Y';
Query OK, 0 rows affected (0.02 sec)
此時(shí),keyring文件也會(huì)有變化
2.2 新增數(shù)據(jù)
向新增的測(cè)試表里添加測(cè)試數(shù)據(jù),并查看
mysql> insert into test1(id,name) values(1,'anm'),(2,'keyring');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from test1;
+----+---------+
| id | name |
+----+---------+
| 1 | anm |
| 2 | keyring |
+----+---------+
2 rows in set (0.00 sec)
2.3修改是否加密
測(cè)試取消表空間加密
mysql> show create table test1;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test1 | CREATE TABLE `test1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 ENCRYPTION='Y' |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter table test1 ENCRYPTION='N';
Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> show create table test1;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test1 | CREATE TABLE `test1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 ENCRYPTION='N' |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from test1;
+----+---------+
| id | name |
+----+---------+
| 1 | anm |
| 2 | keyring |
+----+---------+
2 rows in set (0.00 sec)
再配置為加密
mysql> alter table test1 ENCRYPTION='Y';
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from test1;
+----+---------+
| id | name |
+----+---------+
| 1 | anm |
| 2 | keyring |
+----+---------+
2 rows in set (0.00 sec)
所以,表空間加密方式是可以在線調(diào)整的,且對(duì)數(shù)據(jù)查詢不影響。
另外,keyring_file_data也是可以動(dòng)態(tài)調(diào)整的,比較簡(jiǎn)單,就不演示了
2.4 統(tǒng)計(jì)表空間加密的表
想要知道哪些表的表空間加密了,可以通過數(shù)據(jù)字典表里查看
mysql> SELECT TABLE_SCHEMA, TABLE_NAME, CREATE_OPTIONS FROM INFORMATION_SCHEMA.TABLES WHERE table_schema='testdb2' and CREATE_OPTIONS='ENCRYPTION="Y"';
+--------------+------------+----------------+
| TABLE_SCHEMA | TABLE_NAME | CREATE_OPTIONS |
+--------------+------------+----------------+
| testdb2 | test1 | ENCRYPTION="Y" |
+--------------+------------+----------------+
1 row in set (0.00 sec)
3. 異常處理
如果keyring文件損壞或被誤刪除了,會(huì)出現(xiàn)什么情況
3.1 備份keyring文件
為了保險(xiǎn)起見,先備份一下keyring文件
[root@mha1 keyring]# cp -p keyring keyring.bak
[root@mha1 keyring]# ll -h
total 8.0K
-rw-r----- 1 mysql mysql 155 Aug 16 09:10 keyring
-rw-r----- 1 mysql mysql 155 Aug 16 09:10 keyring.bak
3.2 刪除keyring
直接刪除keyring文件
[root@mha1 keyring]# rm -f keyring
[root@mha1 keyring]# ll -h
total 4.0K
-rw-r----- 1 mysql mysql 155 Aug 16 09:10 keyring.bak
3.3 查看數(shù)據(jù)是否正常
查看數(shù)據(jù)及新建加密表是否成功
mysql> select * from test1;
+----+---------+
| id | name |
+----+---------+
| 1 | anm |
| 2 | keyring |
+----+---------+
2 rows in set (0.00 sec)
mysql> create table test2(id int primary key auto_increment, name varchar(20),key name(name)) ENCRYPTION='Y';
Query OK, 0 rows affected (0.01 sec)
也就是說此時(shí),即使keyrig文件丟失也是可以正常操作的
3.4 重啟數(shù)據(jù)庫(kù)
重啟數(shù)據(jù)庫(kù)后,會(huì)發(fā)現(xiàn),又自動(dòng)生成了keyring文件
此時(shí)再查看加密表
mysql> select * from test1;
ERROR 3185 (HY000): Can't find master key from keyring, please check in the server log if a keyring plugin is loaded and initialized successfully.
創(chuàng)建加密表
mysql> create table test3(id int primary key auto_increment, name varchar(20),key name(name)) ENCRYPTION='Y';
Query OK, 0 rows affected (0.02 sec)
新建表是可以的,因?yàn)榇藭r(shí)相當(dāng)于初始化的時(shí)候。
那么再將原keyring還原,然后再重啟數(shù)據(jù)庫(kù),會(huì)發(fā)現(xiàn)又能成功了
mysql> select * from test1;
+----+---------+
| id | name |
+----+---------+
| 1 | anm |
| 2 | keyring |
+----+---------+
2 rows in set (0.00 sec)
4 keyring管理
4.1 定期備份
可以每日進(jìn)行備份,但是備份的路徑和日常備份分開,需要還原的時(shí)候再拷貝至目標(biāo)文件
4.2 定期更新
為了考慮安全性,當(dāng)懷疑key泄露時(shí),需要進(jìn)行更新。更新后原先的表依舊可以正常方案,因?yàn)楦轮没腋淖僲aster encryption key 并重新加密 tablespace keys,不會(huì)對(duì)表空間重新加密或解密。更新的方法:
-- 更新 master key
mysql> ALTER INSTANCE ROTATE INNODB MASTER KEY;
Query OK, 0 rows affected (0.00 sec)
-- 更新后依舊能正常訪問
mysql> select * from test1;
+----+---------+
| id | name |
+----+---------+
| 1 | anm |
| 2 | keyring |
+----+---------+
2 rows in set (0.00 sec)
至此,InnoDB表空間的簡(jiǎn)單使用就演示完畢。其中還有很多細(xì)節(jié),可以查看官方文檔進(jìn)行探索,https://dev.mysql.com/doc/refman/5.7/en/innodb-data-encryption.html。
總結(jié)
到此這篇關(guān)于MySQL InnoDB表空間加密的文章就介紹到這了,更多相關(guān)MySQL InnoDB表空間加密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- MySQL InnoDB架構(gòu)的相關(guān)總結(jié)
- MySQL InnoDB ReplicaSet(副本集)簡(jiǎn)單介紹
- 詳解MySQL InnoDB存儲(chǔ)引擎的內(nèi)存管理
- MySQL Innodb關(guān)鍵特性之插入緩沖(insert buffer)
- MySQL InnoDB 鎖的相關(guān)總結(jié)
- 如何區(qū)分MySQL的innodb_flush_log_at_trx_commit和sync_binlog
- Mysql InnoDB的鎖定機(jī)制實(shí)例詳解
- Mysql技術(shù)內(nèi)幕之InnoDB鎖的深入講解
- 修改MySQL數(shù)據(jù)庫(kù)引擎為InnoDB的操作
- 簡(jiǎn)述MySQL InnoDB存儲(chǔ)引擎
- MySQL InnoDB 事務(wù)鎖源碼分析