OceanBase 数据库 V4.2.5 BP1、V4.3.5 BP1 版本新增 MySQL 租户下基于 TDE 透明加密的列加密功能。提供 enhanced_aes_encrypt() 函数和 enhanced_aes_decrypt() 函数分别用于列数据的加密和解密。同时新增两个用户级权限 ENCRYPT 和 DECRYPT 用于列加密的二级鉴权,当用户同时拥有 DML 语句的执行权限以及加密(或解密)权限时,才能在 DML 语句中使用加密(或解密)函数。
详细说明
前提条件
OceanBase 数据库版本:V4.2.5 BP1 及之后版本、V4.3.5 BP1 及之后版本。
目前仅 MySQL 模式租户支持列加密。
需要先为该租户开启 TDE 透明加密。
如果没有开启租户 TDE 透明加密的话,会遇到报错。
MySQL [test]> insert into t1(c1, c2) values(1, enhanced_aes_encrypt("test123")); ERROR 4360 (HY000): the keystore opened with dont have a master key仅支持对
varbinary类型的列进行加解密操作。如果对非
varbinary类型执行加密操作的话,会遇到报错。MySQL [test]> create table t2(c1 int primary key, c2 varchar(32)) tablespace sectest_tbs1; Query OK, 0 rows affected (0.09 sec) MySQL [test]> insert into t2(c1, c2) values(1, enhanced_aes_encrypt("test123")); ERROR 1366 (HY000): Incorrect string value for column 'c2' at row 1用户需要额外拥有加密(或解密)权限,才能在 DML 语句中使用加密(或解密)函数。
-- 普通用户如果需要使用加密(或解密)函数,需要额外赋权 MySQL [test]> grant encrypt, decrypt on *.* to test_user1; Query OK, 0 rows affected (0.11 sec)
测试验证
测试验证步骤如下。
开启透明表空间加密参数。
$ mysql -hxx.xxx.80.111 -P2881 -uroot@mysqlt -pxxx -A -c test Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 3221525483 Server version: 5.7.25 OceanBase 4.2.5.4 (r104020022025062018-b1433bac8a0d2b186e76956d5f6a8c4b9ae1632c) (Built Jun 20 2025 18:48:58) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [test]> show parameters like 'tde_method'\G *************************** 1. row *************************** zone: zone1 svr_type: observer svr_ip: xx.xxx.80.111 svr_port: 2882 name: tde_method data_type: NULL value: none info: none : transparent encryption is none, none means cannot use tde, internal : transparent encryption is in the form of internal tables, bkmi : transparent encryption is in the form of external bkmi section: OBSERVER scope: TENANT source: DEFAULT edit_level: DYNAMIC_EFFECTIVE default_value: none isdefault: 1 1 row in set (0.01 sec) MySQL [test]> alter system set tde_method='internal'; Query OK, 0 rows affected (0.01 sec)生成主密钥。
MySQL [test]> ALTER INSTANCE ROTATE INNODB MASTER KEY; Query OK, 0 rows affected (0.07 sec)创建表空间并指定加密算法。
MySQL [test]> CREATE TABLESPACE sectest_tbs1 encryption = 'y'; Query OK, 0 rows affected (0.04 sec)创建使用
varbinary列的表,同时指定表空间为上面的加密表空间。MySQL [test]> create table t1(c1 int primary key, c2 varbinary(32)) tablespace sectest_tbs1; Query OK, 0 rows affected (0.09 sec) MySQL [test]> desc t1; +-------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+-------+ | c1 | int(11) | NO | PRI | NULL | | | c2 | varbinary(32) | YES | | NULL | | +-------+---------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)使用
enhanced_aes_encrypt对插入数据进行加密。MySQL [test]> insert into t1(c1, c2) values(1, enhanced_aes_encrypt("test123")); Query OK, 1 row affected (0.02 sec) MySQL [test]> select * from t1; +----+--------------------------+ | c1 | c2 | +----+--------------------------+ | 1 | ▒▒ "▒▒XZ▒:▒z[A▒B▒ | +----+--------------------------+ 1 row in set (0.00 sec)注意:加密后数据会变得更长,原因是 AES 加密后的数据至少是 16B 的整数倍,同时这个列加密函数需要额外预留 8B 用来存 master key id。所以有可能出现加密后数据长度超过字段类型定义的长度导致报错。
MySQL [test]> insert into t1 values (2, enhanced_aes_encrypt("abcdefghijklmnopqrstuvwxyz")); ERROR 1406 (22001): Data too long for column 'c2' at row 1使用
enhanced_aes_decrypt解密数据。MySQL [test]> select c1, enhanced_aes_decrypt(c2) from t1; +----+--------------------------+ | c1 | enhanced_aes_decrypt(c2) | +----+--------------------------+ | 1 | test123 | +----+--------------------------+ 1 row in set (0.00 sec)无法根据已加密数据的明文进行过滤。
MySQL [test]> select * from t1 where c2='test123'; Empty set (0.00 sec) MySQL [test]> delete from t1 where c2='test123'; Query OK, 0 rows affected (0.00 sec)明文修改数据会自动撤销该行该字段数据的加密。
MySQL [test]> update t1 set c2="test456" where c1=1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 MySQL [test]> select * from t1; +----+---------+ | c1 | c2 | +----+---------+ | 1 | test456 | +----+---------+ 1 row in set (0.00 sec) MySQL [test]> select c1, enhanced_aes_decrypt(c2) from t1; ERROR 9721 (HY000): input value for %s is not valid
适用版本
OceanBase 数据库 V4.2.x 系列:V4.2.5 BP1()及更高的版本(仅 MySQL 模式租户)。
OceanBase 数据库 V4.3.x 系列:V4.3.5 BP1()及更高的版本(仅 MySQL 模式租户)。
备注
OceanBase 数据库 MySQL 模式租户跟列加密相关的权限列表如下表所示。
| 权限类别 | 权限 | 描述 |
|---|---|---|
| 全局权限 | ENCRYPT |
确定用户是否可以调用 ENHANCED_AES_ENCRYPT 函数。仅当用户同时具有 ENCRYPT 权限,以及相应的 DML 权限时,才能在 DML 语句中使用上述加密函数。GRANT/REVOKE ALL PRIVILEGES 不包含该权限。说明:此权限从 V4.2.5 BP1、V4.3.5 BP1 开始支持。 |
| 全局权限 | DECRYPT |
确定用户是否可以调用 ENHANCED_AES_DECRYPT 函数。仅当用户同时具有 DECRYPT 权限,以及相应的 DML 权限时,才能在 DML 语句中使用上述解密函数。GRANT/REVOKE ALL PRIVILEGES 不包含该权限。说明:此权限从 V4.2.5 BP1、V4.3.5 BP1 开始支持。 |