Applicability
Transparent data encryption is not supported in OceanBase Database Community Edition.
This topic describes how to enable transparent data encryption for an existing table.
In OceanBase Database, data is encrypted at the tablespace level. OceanBase Database does not store data in multiple data files; tablespaces are introduced for compatibility and can be simply thought of as a collection of tables.
As an example, this topic describes how to enable encryption for an existing table t1 on the encrypted tablespace sectest_ts1.
Limitations
- You cannot enable encryption on the system tenant.
- After transparent data encryption is enabled for a tenant, the encryption mode cannot be changed unless the tenant is rebuilt.
Prerequisites
Before configuring transparent data encryption, you must generate the tenant master key. For more information, see Generate a tenant master key (MySQL-compatible mode).
Create an encrypted tablespace
After the tenant master key is generated, you can create an encrypted tablespace.
Log in to the MySQL-compatible tenant of the cluster as the administrator user.
Create a tablespace and specify the encryption algorithm.
You can specify the encryption algorithms
aes-256,aes-128,aes-192,sm4-cbc,aes-128-gcm,aes-192-gcm,aes-256-gcm, orsm4-gcm. If you specify'y', aes-256 is used by default.Example:
CREATE TABLESPACE sectest_ts1 encryption = 'y';
Move an existing table to the encrypted tablespace
Log in to the MySQL-compatible tenant of the database as a regular user.
Connect to the database where the table resides, and then move the table
t1to the tablespacesectest_ts1.ALTER TABLE t1 TABLESPACE sectest_ts1;
Perform transparent data encryption on the table
Log in to the MySQL-compatible tenant of the database as a regular user.
Set the
progressive_merge_numvalue of the table to perform full or progressive compaction on the table.progressive_merge_numspecifies the number of progressive compaction rounds for a table. The default value is0, which indicates incremental compaction, where encryption is completed in 100 rounds. A value of1indicates full compaction.Full compaction is typically used when you compact a table. However, if the table contains a large amount of data, a full compaction may take an excessively long time. In this case, progressive compaction is recommended.
Perform full compaction on a table
a. Set the
progressive_merge_numvalue to1.ALTER TABLE t1 set progressive_merge_num = 1;b. Manually initiate one round of compaction.
For more information about how to manually initiate a major compaction, see Manually initiate a major compaction.
Note
After full compaction is completed, data encryption may still be in progress. In this case, query the
oceanbase.V$OB_ENCRYPTED_TABLESview. TheSTATUScolumn may showENCRYPTING, which indicates that encryption is still in progress rather than a compaction failure. Wait untilENCRYPTEDisYESandSTATUSisNORMALbefore you confirm that encryption is complete.c. After compaction is complete, set the
progressive_merge_numvalue back to0.ALTER TABLE t1 set progressive_merge_num = 0;Perform progressive compaction on a table
a. Set the
progressive_merge_numvalue to a number greater than1.Example:
ALTER TABLE t1 SET progressive_merge_num = 3;b. Manually initiate multiple rounds of progressive compaction to encrypt all existing macroblocks in the table and its indexes.
The statement to initiate one round of progressive compaction is as follows:
ALTER SYSTEM MAJOR FREEZE;Note
During progressive compaction, you can query the
V$OB_ENCRYPTED_TABLESview to monitor the encryption progress in real time.
After completion, you can query the following view to confirm whether all macroblocks have been encrypted.
Example:
SELECT * FROM oceanbase.V$OB_ENCRYPTED_TABLES; +----------+------------+---------------+---------------+-----------+----------------------------------+-------------+------------------+------------------+--------+--------+ | TABLE_ID | TABLE_NAME | TABLESPACE_ID | ENCRYPTIONALG | ENCRYPTED | ENCRYPTEDKEY | MASTERKEYID | BLOCKS_ENCRYPTED | BLOCKS_DECRYPTED | STATUS | CON_ID | +----------+------------+---------------+---------------+-----------+----------------------------------+-------------+------------------+------------------+--------+--------+ | 500010 | t1 | 500009 | aes-256 | YES | xxxxxxxxxxxxxxxxxxxxxxxxxxxx7882 | xxxx08 | 0 | 0 | NORMAL | 0 | +----------+------------+---------------+---------------+-----------+----------------------------------+-------------+------------------+------------------+--------+--------+ 1 row in setBased on the query results, all macroblocks are encrypted when all the following conditions are met:
ENCRYPTEDisYES.STATUSisNORMAL(if the status isENCRYPTING, encryption is still in progress. Please wait).BLOCKS_DECRYPTEDis0.
For more information about the fields in the
V$OB_ENCRYPTED_TABLESview, see V$OB_ENCRYPTED_TABLES.
