Previously, in my article SQL Server 2012 AlwaysOn, I discussed the components that make up SQL Server 2012’s AlwaysOn and how you can configure your SQL Server environment using AlwaysOn Availability Groups (AAG) to meet the ever-increasing need for ‘High Availability’ (HA) and ‘Disaster Recovery’ (DR). One of the benefits that I outlined was the ability to use the Enterprise Edition feature ‘Transparent Data Encryption’ (TDE) to secure your databases.
Transparent Data Encryption
TDE allows you to protect your databases by performing real-time I/O encryption utilising keys. This prevents anyone who does not have these keys from accessing the data. I am not going to dive into the details of TDE in this article; there are links in the References & Further Reading section later in the article for you to get a better understanding of TDE.
Configuring your AlwaysOn environment
If you have not yet set up and configured a SQL Server 2012 AlwaysOn environment, then please have a read of my article SQL Server AlwaysOn. In this article I shall assume that you already have an environment set up that is already using AlwaysOn Availability Groups (AAG). Normally, if you are going to create a new AAG or need to add a database into an existing AAG, you can simply use the wizards provided in SSMS. However, if you are going to add a database that has been configured to be encrypted using TDE then you will not be able to use these wizards.
Whether you are creating a new AAG, or you are needing to add a database that has been encrypted using TDE, there are steps that you need to complete on your SQL Server 2012 AlwaysOn Primary Replica. These are:
- Create a Master Key on the Primary Replica
- Backup the Master Key
- Create a Certificate protected by the Master Key
- Backup the Certificate
- Create a Database Encryption Key
- Enable a TDE on a Database
Here is some example code that can be used to perform these tasks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
USE MASTER GO -- Create a Master Key CREATE MASTER KEY ENCRYPTION BY Password = 'P@ssw0rd1'; -- Backup the Master Key BACKUP MASTER KEY TO FILE = '\\Path\Encryption_Backups\MyServer_MK' ENCRYPTION BY Password = 'P@ssword2'; -- Create Certificate Protected by Master Key CREATE Certificate MyServer_Cert WITH Subject = 'My DEK Certificate'; -- Backup the Certificate BACKUP Certificate MyServer_Cert TO FILE = '\\Path\Encryption_Backups\MyServer_Cer' WITH Private KEY ( FILE = '\\Path\Encryption_Backups\MyServer_PrivKey', ENCRYPTION BY Password = 'P@ssword3' ); -- Move to the database you wish to enable TDE on USE TestTDE_WithAlwaysOn GO -- Create a Database Encryption Key CREATE DATABASE ENCRYPTION KEY WITH Algorithm = AES_128 ENCRYPTION BY Server Certificate MyServer_Cert; -- Enable the Database for Encryption by TDE ALTER DATABASE TestTDE_WithAlwaysOn SET ENCRYPTION ON; |
We need to undertake some configuration steps on the Secondary Replicas in order to allow us to be able to replicate the Availability Databases. We need to:
- Create a Master Key on the Secondary Replica
- Backup the Master Key
- Create Certificate from the Primary Replica
Here is some example code that can be used to perform these tasks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
USE MASTER GO -- Create a Master Key CREATE MASTER KEY ENCRYPTION BY Password = 'P@ssw0rd1'; -- Backup the Master Key BACKUP MASTER KEY TO FILE = '\\Path\Encryption_Backups\MyServer2_MK' ENCRYPTION BY Password = 'P@ssword2'; -- Create Certificate Protected by Master Key CREATE Certificate MyServer2_Cert FROM FILE = '\\Path\Encryption_Backups\MyServer_Cer' WITH Private KEY ( FILE = '\\Path\Encryption_Backups\MyServer_PrivKey', Decryption BY Password = 'P@ssword3' ); |
The above will need to be undertaken on every Secondary Replica in the SQL Server 2012 AlwaysOn environment. After the Replicas are all configured with the Certificate, the encrypted database(s) can then be made available on all of the Replicas.
When we are using the ‘New Availability Group Wizard’, we have to select those database(s) which will participate in the AlwaysOn Availability Group. If one of the databases has been enabled for encryption utilising TDE, we will not be able to use the wizard to create the AlwaysOn Availability Group. To achieve this, we need to use T-SQL or PowerShell to create the Availability Group.
This T-SQL Code (to run in SQLCMD mode in SSMS) will manually create an Availability Group:
— The following code needs to be run using SQLCMD Mode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
-- The following code needs to be run using SQLCMD Mode :Connect PrimaryReplicaServer USE MASTER GO -- 1/ Create AlwaysOn AAG with TDE enabled database CREATE Availability GROUP [SQL2012_TDE] WITH (Automated_Backup_Preference = Secondary) FOR DATABASE [TestTDE_WithAlwaysOn] Replica ON N'PrimaryReplicaServer' WITH (Endpoint_URL = N'TCP://PrimaryReplicaServer.Domain1.Com:5022', Failover_Mode = Manual, Availability_Mode = Asynchronous_Commit, Backup_Priority = 50, Secondary_Role(Allow_Connections = ALL) ), N'SecondaryReplicaServer' WITH (Endpoint_URL = N'TCP://SecondaryReplicaServer.Domain1.Com:5022', Failover_Mode = Manual, Availability_Mode = Asynchronous_Commit, Backup_Priority = 50, Secondary_Role(Allow_Connections = ALL) ); GO :Connect SecondaryReplicaServer -- 2/ Join the Secondary Replica to the Newly Created AAG. ALTER Availability GROUP [SQL2012_TDE] JOIN; GO :Connect PrimaryReplicaServer -- 3/ Create a Full Backup BACKUP DATABASE [TestTDE_WithAlwaysOn] TO DISK = '\\Path\AlwaysOn_Backups\TestTDE_WithAlwaysOn_DB.bak' WITH Copy_Only; GO :Connect SecondaryReplicaServer -- 4/ Start the restoration process to bring your database to a synchronised state RESTORE DATABASE [TestTDE_WithAlwaysOn] FROM DISK = '\\Path\AlwaysOn_Backups\TestTDE_WithAlwaysOn_DB.bak' WITH NoRecovery; GO :Connect PrimaryReplicaServer -- 5/ Create a TLog Backup BACKUP LOG [TestTDE_WithAlwaysOn] FROM DISK = '\\Path\AlwaysOn_Backups\TestTDE_WithAlwaysOn_TL.bak'; GO :Connect SecondaryReplicaServer -- 6/ Start the restoration process to bring your database to a synchronised state RESTORE LOG [TestTDE_WithAlwaysOn] FROM DISK = '\\Path\AlwaysOn_Backups\TestTDE_WithAlwaysOn_DB.bak' WITH NoRecovery; GO :Connect SecondaryReplicaServer -- 7/ Join the Database to the AAG and bring it into the readable synchronised state. ALTER DATABASE [TestTDE_WithAlwaysOn] SET HADR Availability GROUP = [SQL2012_TDE]; GO |
Alternatively if you prefer to use PowerShell here is some code to manually create an Availability Group:
1 2 3 4 5 6 |
Import-Module "SQLPS" -DisableNameChecking # Create Full Database Backup Backup-SQLDatabase -Database "TestTDE_WithAlwaysOn" -BackupFile "\\Path\AlwaysOn_Backups\TestTDE_WithAlwaysOn_DB.bak" -ServerInstance "ServerCoreNode1" |
1 2 3 4 |
# Create TLog backup Backup-SQLDatabase -Database "TestTDE_WithAlwaysOn" -BackupFile "\\Path\AlwaysOn_Backups\TestTDE_WithAlwaysOn_TL.bak" -ServerInstance "ServerCoreNode1" -BackupAction Log |
1 2 3 4 5 6 7 |
# Restore Database and Log on Secondary (NoRecovery) Restore-SQLDatabase -Database "TestTDE_WithAlwaysOn" -BackupFile "\\Path\AlwaysOn_Backups\TestTDE_WithAlwaysOn_DB.bak" -ServerInstance "ServerCoreNode2" -NoRecovery Restore-SQLDatabase -Database "TestTDE_WithAlwaysOn" -BackupFile "\\Path\AlwaysOn_Backups\TestTDE_WithAlwaysOn_TL.bak" -ServerInstance "ServerCoreNode2" -RestoreAction Log -NoRecovery |
1 2 3 4 |
# Create an In-Memory representation of Primary Replica $PrimaryReplica = New-SQLAvailabilityReplica -Name "ServerCoreNode1" -EndPointURL "TCP://ServerCoreNode1.Test.Com:5022" -AvailabilityMode "AsynchronousCommit" -FailoverMode "Manual" -Version 11 -AsTemplate |
1 2 3 4 |
# Create an In-Memory representation of Secondary Replica $SecondaryReplica = New-SQLAvailabilityReplica -Name "ServerCoreNode2" -EndPointURL "TCP://ServerCoreNode2.Test.Com:5022" -AvailabilityMode "AsynchronousCommit" -FailoverMode "Manual" -Version 11 -AsTemplate |
1 2 3 |
# Create the Availability Group New-SQLAvailabilityGroup -Name "SQL2012_TDE" -Path "SQLServer:\SQL\ServerCoreNode1\Default" -AvailabilityReplica @($PrimaryReplica,$SecondaryReplica) -Database "TestTDE_WithAlwaysOn" |
1 2 3 |
#Join the Secondary Replica to the Availability Group Join-SQLAvailabilityGroup -Path "SQLServer:\SQL\ServerCoreNode2\Default" -Name "SQL2012_TDE" |
1 2 3 |
# Join the Secondary Database to the Availability Group Add-SQLAvailabilityDatabase -Path "SQLServer:\SQL\ServerCoreNode2\Default\AvailabilityGroups\SQL2012_TDE" -Database "TestTDE_WithAlwaysOn" |
If we already have an AAG, and we need to add a database that has been configured for encryption, then a slight modification to the T-SQL code above will allow us to achieve this. Swap Steps 1 & 2 in the T-SQL Code above for the code below:
1 2 3 4 5 6 |
-- The following code needs to be run using SQLCMD Mode :Connect PrimaryReplicaServer -- Add your database to the Existing Availability Group ALTER AVAILABILITY GROUP [SQL2012_TDE] ADD DATABASE [TestTDE_WithAlwaysOn]; GO |
We have now configured the environment to have an Encrypted database participate in a SQL Server 2012 AlwaysOn Availability Group.
References & Further Reading
- Transparent Data Encryption – http://msdn.microsoft.com/en-us/library/bb934049.aspx
- Create Master Key – http://technet.microsoft.com/en-us/library/ms174382.aspx
- Create Certificates – http://msdn.microsoft.com/en-us/library/ms187798.aspx
- Create Database Master Key – http://technet.microsoft.com/en-us/library/aa337551.aspx
- Move a TDE protected Database – http://technet.microsoft.com/en-us/library/ff773063.aspx
- Backup Master Keys – http://technet.microsoft.com/en-us/library/ms174387.aspx
- Backup Certificates – http://msdn.microsoft.com/en-us/library/ms178578.aspx
- Manually Prepare a DB for for AAG’s – http://msdn.microsoft.com/en-us/library/ff878349.aspx
- Encrypted Databases with AAG’s – http://msdn.microsoft.com/en-us/library/hh510178.aspx
- Create an AAG with PowerShell – http://msdn.microsoft.com/en-us/library/gg492181.aspx
Load comments