从 MySQL5.7.11开始,MySQL对InnoDB支持存储在单独表空间中的表的数据加密 。此功能为物理表空间数据文件提供静态加密。该加密是在引擎内部数据页级别的加密手段,在数据页写入文件系统时加密,加密用的是AES算法,而其解密是在从文件读到内存中时进行。
在mysql配置文件【mysqld】x项中添加如下内容
plugin_dir=/usr/local/mysql5.7/lib/mysql/plugin # 插件路径,根据实际情况修改
early-plugin-load="keyring_file.so" # 加密插件
keyring_file_data=/data/mysql3306/keyring/keyring # 路径不存在,需要创建
innodb_file_per_table=1 # 只作用于独立表空间
创建时要注意 ,keyring_file_data 里配置的keyring会在启动时自动创建,本步骤中创建到对应目录即可
mkdir -p /data/mysql3306/keyring/
chown -R mysql:mysql /data/mysql3306/keyring/
chmod 750 /data/mysql3306/keyring
重启mysql即可,启动后会发现注意/data/mysql3306/keyring 目录下生成了 keyring文件
注意,重启后也要看一下mysql错误日志里有没有相关错误信息,如果没有错误则继续进行
启动后可以查看插件是否生效
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命令查看
创建一张新表,并添加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)
此时,keyring文件也会有变化
向新增的测试表里添加测试数据,并查看
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)
修改是否加密
测试取消表空间加密
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)
所以,表空间加密方式是可以在线调整的,且对数据查询不影响。
另外,keyring_file_data也是可以动态调整的,比较简单,就不演示了
想要知道哪些表的表空间加密了,可以通过数据字典表里查看
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)
如果keyring文件损坏或被误删除了,会出现什么情况
为了保险起见,先备份一下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
直接删除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
查看数据及新建加密表是否成功
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)
也就是说此时,即使keyrig文件丢失也是可以正常操作的
重启数据库后,会发现,又自动生成了keyring文件
此时再查看加密表
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.
创建加密表
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)
新建表是可以的,因为此时相当于初始化的时候。
那么再将原keyring还原,然后再重启数据库,会发现又能成功了
mysql> select * from test1;
+----+---------+
| id | name |
+----+---------+
| 1 | anm |
| 2 | keyring |
+----+---------+
2 rows in set (0.00 sec)
可以每日进行备份,但是备份的路径和日常备份分开,需要还原的时候再拷贝至目标文件
为了考虑安全性,当怀疑key泄露时,需要进行更新。更新后原先的表依旧可以正常方案,因为更新只会改变master encryption key 并重新加密 tablespace keys,不会对表空间重新加密或解密。更新的方法:
-- 更新 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表空间的简单使用就演示完毕。其中还有很多细节,可以查看官方文档进行探索,https://dev.mysql.com/doc/refman/5.7/en/innodb-data-encryption.html。
往期精彩回顾
4. 监控利器出鞘:Prometheus+Grafana监控MySQL、Redis数据库
5. PostgreSQL主从复制--物理复制
6. MySQL传统点位复制在线转为GTID模式复制
7. MySQL敏感数据加密及解密
8. MySQL数据备份及还原(一)
9. MySQL数据备份及还原(二)
扫
码
关
注