mysql学习(三) 整表加密
参考资料(原文所在网页已经打不开了,因此无法征求原文作者同意,非常抱歉,转载一份,以免文章丢失,在此感谢作者):
https://blog.coderstory.cn/mysql-keyring_file/
前言
MySql社区版从5.7.11开始支持基于表的数据加密方案,模块名为keyring_file,支持加密整张表。这种是加密方式其实是基于文件加密的,一旦mysqld读取key启动后,将会解密整张表的数据,在mysql服务内,读取的数据都是解密后的,也就是说对客户端而言是无感知的。而这个key是本地存放的,mysql服务拥有读写这个key的权限。
总体看这种方案不太安全,原因是数据库文件是加密的,但只要能有mysql服务的账户,那么访问数据都是解密后的,加密不攻自破。而且解密key也是本地存放的,入侵者完全可以一并带走。这种方案只能保证入侵者只拖走了数据库文件后无法读取内容。
1. 搭建mysql容器
参考我以前写的资料:
docker学习(二) 用docker安装mysql容器
https://blog.csdn.net/yangkang1122/article/details/88706611
搭建mysql主-从/主-主集群可以参考另一篇资料:
https://blog.csdn.net/yangkang1122/article/details/88912658
2. mysql整表加密
(1)启用加密模块
INSTALL PLUGIN keyring_file soname ‘keyring_file.so’;
mysql> INSTALL PLUGIN keyring_file soname 'keyring_file.so';
Query OK, 0 rows affected (0.10 sec)
(2)设置加密key存放路径
set global keyring_file_data=‘/var/lib/mysql-keyring/keyring’;
mysql> set global keyring_file_data='/var/lib/mysql-keyring/keyring';
Query OK, 0 rows affected (0.00 sec)
(3)永久启用设置
上述两个步骤都是临时的,重启服务都会失效,我们把配置写到配置文件里,确保重启服务后也能生效
[mysqld]
early-plugin-load=keyring_file.so
keyring_file_data=/var/lib/mysql-keyring/keyring
(4)查看key的存放路径
show global variables like ‘%keyring_file_data%’;
mysql> show global variables like '%keyring_file_data%';
+-------------------+--------------------------------+
| Variable_name | Value |
+-------------------+--------------------------------+
| keyring_file_data | /var/lib/mysql-keyring/keyring |
+-------------------+--------------------------------+
1 row in set (0.00 sec)
(5)查看启用的模块
查看下keyring_file模块是否已经被载入。
show plugins;
mysql> show plugins;
+----------------------------+----------+--------------------+-----------------+---------+
| Name | Status | Type | Library | License |
+----------------------------+----------+--------------------+-----------------+---------+
| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
| mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| sha256_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL |
| CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
| INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
。。。。。。(省略N条)
| keyring_file | ACTIVE | KEYRING | keyring_file.so | GPL |
+----------------------------+----------+--------------------+-----------------+---------+
45 rows in set (0.00 sec)
(6)加密现有的表
alter table table encryption=’Y’;
mysql> create table cc (id int);
Query OK, 0 rows affected (0.01 sec)
mysql> alter table cc encryption='Y';
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
(7)取消加密
alter table table encryption=’N’;
mysql> alter table cc encryption='N';
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0