SQLServer TDE加密部署详解
1.查看master数据库是否已经创建了master key:
select name,is_master_key_encrypted_by_server from sys.databases
如果没有创建,先创建master key:
USE master;
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Password';
GO
备份master key:
BACKUP MASTER KEY TO FILE = 'path_to_file'
ENCRYPTION BY PASSWORD = 'Password1'
还原master key:
Use master
RESTORE MASTER KEY
FROM FILE = 'C:\storedcerts\masterkey'
DECRYPTION BY PASSWORD = 'Password1'
ENCRYPTION BY PASSWORD = 'Password1';
GO
打开master key:
OPEN MASTER KEY DECRYPTION BY PASSWORD=‘Password1';
关闭master key:
CLOSE MASTER KEY;
GO
2.创建证书
USE master;
GO
CREATE CERTIFICATE TDE_cert WITH SUBJECT = 'TDE certificate';
GO
备份证书(备份证书时一定要加PRIVATE KEY,否则还原时会出现秘钥损坏的错误):
Use master
BACKUP CERTIFICATE TDE_cert TO FILE = 'C:\storedcerts\TDE_cert'
WITH PRIVATE KEY ( FILE = 'C:\storedcerts\TDE_private_key' ,
ENCRYPTION BY PASSWORD = 'Password2' );
GO
还原证书(在迁移使用TDE加密的数据库时,还原原数据库加密使用的证书到目的数据库实例上,即可进行正常还原,还原后数据库处于TDE加密状态):
CREATE CERTIFICATE TDE_cert
FROM FILE = 'C:\storedcerts\TDE_cert'
WITH PRIVATE KEY
(
FILE = 'C:\storedcerts\TDE_private_key',
DECRYPTION BY PASSWORD = 'Password2'
);
GO
删除证书:
DROP CERTIFICATE TDE_cert
查看有哪些证书:
select * from sys.certificates;
or
select * from master.sys.certificates;
3. 使用步骤2创建的证书创建一个database密钥:
USE [USer_DB];
GO
CREATE DATABASE ENCRYPTION KEY
WITH ALGORITHM = AES_256
ENCRYPTION BY SERVER CERTIFICATE TDE_cert;
GO
删除DATABASE ENCRYPTION KEY:
DROP DATABASE ENCRYPTION KEY
4. 将数据库设置为TDE
USE [User_DB];
ALTER DATABASE User_DB SET ENCRYPTION ON;
当Alwayson高可用组已经搭建完毕后,只需要在主节点创建证书,然后还原到辅助节点,高可用组就不会报错。
单节点还原TDE加密过的数据库:
1. 新建master key
2. 还原证书
3. 还原数据库
AlwaysOn情况下加密primary数据库后,需要对辅助数据库做相应操作:
1. 在辅助节点新建master key
2. 还原证书
3. 从可用性组移除该数据库
4. 修改辅助数据库状态为norecovery
5. 把该数据库加入高可用组(Join only方式)