Configure transparent data encryption (TDE)
Transparent Data Encryption (TDE) encrypts data before writing it to disk and decrypts it when reading into memory, with no increase in file size and no application changes required. You can enable TDE in the ApsaraDB for MongoDB console to protect instance data.
Prerequisites
The instance storage type and architecture must meet the following requirements:
Local disk instances: Support replica set and sharded cluster, database version 4.0 or 4.2.
Cloud disk instances: Support replica set, database version 4.2 to 8.3 (all versions).
The instance uses the WiredTiger storage engine.
The minor version of a cloud disk instance must be no earlier than the following versions:
Major version
Minimum minor version
4.2
4.0.36
4.4
5.0.22
5.0
6.0.23
6.0
7.0.24
7.0
8.0.16
8.0
9.0.9
8.3
9.3.1
ImportantIf the minor version of your instance is earlier than the required version, upgrade the minor version before enabling TDE. For more information, see Change instance configurations.
If your instance does not meet these requirements, Change instance configurations.
Impacts
Enabling TDE restarts the instance, causing a transient connection interruption. Perform this operation during off-peak hours and ensure your application has a reconnection mechanism.
TDE increases CPU utilization.
Encrypted collections cannot be restored to a self-managed database by using physical backups. Use a logical backup instead. Restore data to a self-managed database by using a logical backup.
Considerations
Once enabled, TDE cannot be disabled.
You cannot restore a released, TDE-enabled instance from the Recycle Bin.
TDE encrypts at the instance level and applies only to collections. To encrypt specific fields, use Client-Side Field-Level Encryption (MongoDB 4.2 only).
NoteTo skip encryption for specific collections, Exclude a collection from encryption.
TDE encrypts only new collections created after enablement. Existing collections remain unencrypted.
Key Management Service (KMS) generates and manages TDE keys. ApsaraDB for MongoDB does not provide encryption keys or certificates. What is Key Management Service?.
The encryption key cannot be changed after TDE is enabled. To use a different key, migrate data to a new instance configured with the new key by using Data Transmission Service (DTS). Migrate data between ApsaraDB for MongoDB instances.
Make sure that the KMS key used by TDE is always available. If the key is disabled, deleted, or overdue, the instance is locked and becomes unavailable, and existing backups cannot be used for recovery.
Procedure
Navigate to the Replica Set Instances or Sharded Cluster Instances page. In the top navigation bar, select a resource group and region, and then click the ID of the target instance.
In the left-side navigation pane, click .
Turn on the switch next to TDE Status:.
In the Enable TDE dialog box, select a key generation method based on your requirements.
Use Automatically Generated Key: The system automatically generates a key.
Use Custom Key: Select an existing key from the drop-down list. If the key you want to use is not available, you must first create one. For instructions, see Create a key.
Click OK.
The instance enters the Updating TDE state. The operation is complete when the status changes to Running.
Exclude a collection from encryption
After TDE is enabled, all new collections are encrypted by default. To create an unencrypted collection, specify the storage engine configuration during creation.
Connect to the database by using mongo shell: Connect to a replica set instance or Connect to a sharded cluster instance.
Run the following command to create an unencrypted collection.
Syntax:
db.createCollection("<collection_name>",{ storageEngine: { wiredTiger: { configString: "encryption=(name=none)" } } })NoteIn this command,
collection_nameis the name of the collection.Example:
db.createCollection("customer",{ storageEngine: { wiredTiger: { configString: "encryption=(name=none)" } } })
Encrypt existing collections
Collections that existed before TDE was enabled are not automatically encrypted. Run the encryptExistingCollection command to convert an existing collection and its indexes to encrypted storage.
Prerequisites
TDE is enabled for the instance.
The instance is a cloud disk-based replica set instance that runs MongoDB 8.0, and its minor version is 9.0.14 or later.
The account that runs the command has the
collModprivilege on the target collection, such as an account with thedbOwnerorrootrole.
Procedure
Use Mongo Shell to connect to the primary node of the instance. Run the command only once on the primary node. Secondary nodes complete the encryption operation through automatic synchronization.
Switch to the target database and run one of the following commands:
Encrypt a specific collection:
db.runCommand({ encryptExistingCollection: "<collection_name>" })Encrypt all collections in the current database:
db.runCommand({ encryptExistingCollection: "*" })
The command runs successfully when
okin the response is1.Connect to each node and run
db.serverStatus().wiredTiger.encryptionRewriteto check the encryption progress. WhenpendingIdentsis0on all nodes, all existing data is encrypted.
Impacts
The command briefly locks the target collection. The lock usually lasts milliseconds, during which read and write requests to the collection wait briefly. On an instance with heavy write workloads, the lock may last seconds.
After the command returns, existing data is encrypted in the background without blocking business read or write operations. The process increases disk I/O and CPU utilization. Its impact is comparable to running
compactonce on the collection, and its duration is proportional to the collection's data volume.Data files may temporarily grow during background encryption. Reserve available disk space that is at least the current storage size of the target collection and its indexes.
Considerations
Run the command during off-peak hours.
After a collection is encrypted, it cannot be reverted to an unencrypted state.
If you specify
"*", all collections in the current database are encrypted, including collections configured as unencrypted by usingencryption=(name=none). To keep such collections unencrypted, specify the collection names individually and run the command for each collection that you want to encrypt.A successful response only indicates that new data written to the collection starts to be encrypted. Existing data continues to be processed in the background. Use the encryption progress to determine when the operation is complete. If a node restarts or a primary-secondary switchover occurs during background encryption, encryption automatically resumes and you do not need to run the command again.
Backups created before the command is run are not encrypted.
You cannot run the command on views or in transactions.