我需要镜像一些数据库,并在它们上使用透明数据加密(TDE),因为我们的数据必须在“静止”时进行加密。
我在主体和镜像上都设置了TDE。当我设置两个数据库的镜像时,我遇到了这个问题。由于我使用的是TDE,所以我不知道通过gui设置镜像的方法,因此我不得不使用t-sql来完成工作。
以下是我在镜像服务器上使用的代码
--Restore the full backup to the mirrored mdf and ldf
OPEN MASTER KEY DECRYPTION BY PASSWORD = '1Password'
RESTORE DATABASE TDE
FROM disk = '\\SERVERNAME\SQL_Stuff\Backup\TDE_FULL.bak'
WITH NORECOVERY,
REPLACE,
MOVE 'TDE' TO 'E:\TDE.mdf',
REPLACE,
MOVE 'TDE_log' TO 'G:\TDE.ldf'
CLOSE MASTER KEY
GO
--Restore the log backup to the mirrored db
OPEN MASTER KEY DECRYPTION BY PASSWORD = '1Password'
RESTORE LOG TDE
FROM DISK = '\\SERVERNAME\SQL_Stuff\Backup\TDE_LOG.trn'
WITH NORECOVERY;
CLOSE MASTER KEY
GO
--Drop/Create Mirroring endpoint on mirror
--DROP ENDPOINT TDE
CREATE ENDPOINT TDE
STATE = STARTED
AS TCP ( LISTENER_PORT = 7025 )
FOR DATABASE_MIRRORING (
ROLE = PARTNER
);
GO
--Check the endpoints for the mirror
USE MASTER
SELECT * FROM sys.database_mirroring_endpoints
GO
--Set the principal on the mirrored db
OPEN MASTER KEY DECRYPTION BY PASSWORD = '1Password'
ALTER DATABASE TDE SET PARTNER = 'TCP://PRINCIPAL.DOMAIN.local:7022'
GO
CLOSE MASTER KEY
GO
以下是我在主体服务器上使用的代码。
----------------------Mirroring Section----------------------------------
--Full Backup of Principal
USE TDE
GO
BACKUP DATABASE TDE
TO DISK = '\\SERVERNAME\SQL_Stuff\Backup\TDE_FULL.bak'
WITH COMPRESSION,
NAME = 'Full Backup of TDE';
GO
---Log Backup of Principal
USE TDE
GO
BACKUP LOG TDE
TO DISK = '\\SERVERNAME\SQL_Stuff\Backup\TDE_LOG.trn'
WITH COMPRESSION,
NAME = 'Log backup of TDE'
GO
--Drop/Create Mirroring endpoint on principal
--DROP ENDPOINT TDE
CREATE ENDPOINT TDE
STATE = STARTED
AS TCP ( LISTENER_PORT = 7022 )
FOR DATABASE_MIRRORING (
ROLE = PARTNER
);
GO
--Check the endpoints for the princple
USE master
select * from sys.database_mirroring_endpoints
GO
--Set the mirror db on the principal db
OPEN MASTER KEY DECRYPTION BY PASSWORD = '1Password'
ALTER DATABASE TDE SET PARTNER = 'TCP://MIRROR.DOMAIN.local:7025'
CLOSE MASTER KEY
GO
我设置了镜像端点1st,然后是主体端点。然后,ALTER DATABASE
在镜像上,然后在主体上,发出,得到错误:
Msg 1416, Level 16, State 31, Line 2
Database "TDE" is not configured for database mirroring.
我对此无所适从。镜像处于“正在还原”状态,但是我可以肯定该错误是在谈论主体数据库。
谢谢你提供的所有帮助!
主体TDE的更新代码:
--Create Master Key in Master Database
USE MASTER
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '1Password';
PRINT 'created master key'
go
--Backing up the master key file
USE master;
OPEN MASTER KEY DECRYPTION BY PASSWORD = '1Password';
BACKUP MASTER KEY TO FILE = '\\SERVERNAME\TDE_Master_Key.key' ENCRYPTION BY PASSWORD = '1Password';
GO
--Create Server Certificate in the Master Database encrypted with master key (created above) which would be used to create USER database encryption key.
USE Master
CREATE CERTIFICATE Cert_For_TDE WITH SUBJECT = 'Master_Cert_for_TDE', EXPIRY_DATE = '3500-Jan-01';
Go
--Backing up the server cert file
--USE master;
BACKUP CERTIFICATE Cert_For_TDE TO FILE = '\\SERVERNAME\TDE_Cert.cer'
WITH PRIVATE KEY ( FILE = '\\SERVERNAME\TDE_Cert_Key.key', ENCRYPTION BY PASSWORD = '1Password');
GO
--Create user database key
USE TDE
CREATE DATABASE ENCRYPTION KEY
WITH ALGORITHM = AES_256 ENCRYPTION BY SERVER CERTIFICATE Cert_For_TDE;
GO
--Enabling Transparent Database Encryption for the USER Database
USE master;
GO
ALTER DATABASE TDE SET ENCRYPTION ON
GO
TDE的镜像代码:
--restore the backed up key to the mirror
use master
RESTORE MASTER KEY
FROM FILE = '\\SERVERNAME\TDE_Master_Key.key'
DECRYPTION BY PASSWORD = '1Password'
ENCRYPTION BY PASSWORD = '1Password';
GO
--restore the backed up cert to the mirror
USE Master;
OPEN MASTER KEY DECRYPTION BY PASSWORD = '1Password'
CREATE CERTIFICATE Cert_For_TDE
FROM FILE = '\\SERVERNAME\TDE_Cert.cer' WITH PRIVATE KEY ( FILE = '\\SERVERNAME\TDE_Cert_Key.key', DECRYPTION BY PASSWORD = '1Password');
GO
Principal上的Update2 sys.database_mirroring_endpoints与sys.tcp_endpoints结合在一起:
endpoint_id name principal_id state_desc role_desc connection_auth_desc certificate_id encryption_algorithm_desc port ip_address
65545 TDE 261 STARTED PARTNER NEGOTIATE 0 RC4 7022 NULL
在镜像上将sys.database_mirroring_endpoints与sys.tcp_endpoints结合在一起显示:
endpoint_id name principal_id state_desc role_desc connection_auth_desc certificate_id encryption_algorithm_desc port ip_address
65537 TDE 261 STARTED PARTNER NEGOTIATE 0 RC4 7025 NULL
数据库是否处于完全恢复模式?在执行设置时,是否有任何日志备份作业正在运行?您是否在主体上备份了主密钥并在TDE的镜像上还原了主密钥?
—
罗伯特·L·戴维斯
是的,两个数据库都已完全恢复。在执行备份时,没有作业在上面运行(仅一张表有4行),这需要2秒。我已经备份了主密钥并将其还原到镜像中。明天恢复工作时,我将发布该代码。
—
RateControl 2011年
我添加了TDE脚本和一些dmvs
—
RateControl