本文主要记录MySQL企业版TDE插件keyring_encrypted_file的安装和使用。
TDE说明
TDE( Transparent Data Encryption,透明数据加密) 指的是无需修改应用就可以实现数据的加解密,在数据写磁盘的时候加密,读的时候自动解密。加密后其他人即使能够访问数据库文件,没有key也无法读取数据,从而达到防止非法访问的目的。
-
MySQL的支持情况
InnoDB使用的是两层的加密键架构,包括一个master加密键和表空间加密键,master加密键是用来加解密表空间键的,而表空间加密键保持不变,放置在表空间文件的文件头,这样可以不时轮换master的加密键,从而提供更高的安全性,而表空间的数据不用重新进行加解密,节省时间,对生产系统的影响也较小。
静态数据加密功能支持基于高级加密标准 (Advanced EncryptionStandard,AES) 块的加密算法。它使用电码本(Electronic Codebook ) 块加密模式进行表空间密钥加密,使用密码分组链接 (CipherBlock Chaining) 块加密模式进行数据加密。
加密的key都是通过keyring(钥匙圈)来进行管理的,使用的是插件和组件的形式。这些插件或组件使用本身的后台存储或者与其它的密钥后端存储系统通信,MySQL 5.7开始支持TDE,8.0提供更多的加密选项,社区版和企业版都可以,区别是企业版提供额外的keyring组件和插件,可以提供更好的安全性,如果要达到合规的要求,需要使用企业版,详细支持情况列表如下。
对于不同的版本,支持的文件加密也是有区别的,8.0.23或以上,支持的加密包括有:独立表空间、通用表空间、DoubleWrite文件、mysql系统表空间、Redo log和UndoLog文件的加密。
根据官方文档,启用后性能影响百分比在个位数,实际的影响情况可用工作负载测试下。启用加密后,数据库的占用空间也变化不大。
启用加密后,对于备份策略也需要调整,需要考虑到master键的保存,并不时作备份恢复测试,确保即使生产数据完全丢失也可以恢复数据。要注意的是,如果使用MySQL Enterprise Backup (MEB)备份TDE的表空间,备份下来也是加密的,而mysqldump备份是没有加密的,考虑到安全问题可手动加密。
一、系统环境
操作系统:Ubuntu 20.04.5 LTS
数据库版本:8.0.35-commercial MySQL Enterprise Server - Commercial
TDE插件类型:keyring_encrypted_file
二、添加配置文件
vim /etc/mysql/mysql.conf.d/mysqld.cnf
添加到mydqld下
[mysqld]
# keyring
early-plugin-load=keyring_encrypted_file.so
keyring_file_data=/database/mysql/keyring/keyring_file
keyring_encrypted_file_password=uYeW0fD6aODGzgWz
三、添加目录和授权
根据配置创建保持key的目录和授权。否则启动会报权限问题。
mkdir -p /database/mysql/keyring
chown -R mysql:mysql /database/mysql/keyring/
chmod 750 /database/mysql/keyring/
三、安装插件
使用下面命令安装插件
INSTALL PLUGIN keyring_file soname 'keyring_file.so';
题外话 卸载插件 UNINSTALL PLUGIN keyring_file;
四、查看插件安装情况
SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE 'keyring%';
ACTIVE表明插件已经安装成功
五、重启后插件生效
systemctl start mysql
下面是启动后mysql的日志
root@xmachine:/home/admin# tail -n 100 /var/log/mysql/error.log
2024-03-04T07:39:08.736046Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 10000 (requested 10240)
2024-03-04T07:39:08.990458Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2024-03-04T07:39:08.990490Z 0 [Warning] [MY-011068] [Server] The syntax 'slave_skip_errors' is deprecated and will be removed in a future release. Please use replica_skip_errors instead.
2024-03-04T07:39:08.990526Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2024-03-04T07:39:08.992119Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.35-commercial) starting as process 3889861
2024-03-04T07:39:08.996032Z 0 [Warning] [MY-013360] [Server] Plugin keyring_encrypted_file reported: ''keyring_encrypted_file plugin' is deprecated and will be removed in a future release. Please use component_keyring_encrypted_file instead'
2024-03-04T07:39:08.997311Z 0 [Warning] [MY-013907] [InnoDB] Deprecated configuration parameters innodb_log_file_size and/or innodb_log_files_in_group have been used to compute innodb_redo_log_capacity=4294967296. Please use innodb_redo_log_capacity instead.
2024-03-04T07:39:08.999044Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-03-04T07:39:09.533703Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-03-04T07:39:09.649068Z 0 [Warning] [MY-013360] [Server] Plugin keyring_file reported: ''keyring_file plugin' is deprecated and will be removed in a future release. Please use component_keyring_file instead'
2024-03-04T07:39:09.759970Z 0 [Warning] [MY-010068] [Server] CA certificate /database/mysql/ca.pem is self signed.
2024-03-04T07:39:09.759999Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2024-03-04T07:39:09.791955Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2024-03-04T07:39:09.792000Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.35-commercial' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Enterprise Server - Commercial.
六、查询插件状态
查看加密插件状态
select plugin_name,plugin_status from information_schema.plugins where plugin_name like 'keyring%';
查询主密钥文件的位置
show variables like 'keyring_file_data';
查看相应的目录是否生成加密key文件。
查看密钥
show variables like '%keyring%';
七、加密和解密表操作
# 数据表创建时加密:
CREATE TABLE t1 (c1 INT) ENCRYPTION='Y'
# 已创建数据表加密
ALTER TABLE t1 ENCRYPTION='Y'
# 数据表解密:
ALTER TABLE t1 ENCRYPTION='N'
八、轮换主密钥
执行下面的轮换主密钥命令需要 SUPER 权限
ALTER INSTANCE ROTATE INNODB MASTER KEY;
九、查看表加密状态
查看加密的表:
SELECT TABLE_SCHEMA, TABLE_NAME, CREATE_OPTIONS FROM INFORMATION_SCHEMA.TABLES WHERE CREATE_OPTIONS LIKE '%ENCRYPTION%';
查看未加密的表
select concat(TABLE_SCHEMA,".",TABLE_NAME) from INFORMATION_SCHEMA.TABLES where (TABLE_SCHEMA,TABLE_NAME) not in (SELECT TABLE_SCHEMA,TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE CREATE_OPTIONS LIKE '%ENCRYPTION%' and table_schema not in ('information_schema','performance_schema','sys','mysql','universe')) and TABLE_SCHEMA in ('mydata');