Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
本文介绍如何在使用本机还原选项将受 TDE 保护的 SQL Server 数据库迁移到 Azure SQL 托管实例之前迁移证书。
使用本机还原选项将受透明数据加密(TDE)保护的数据库从 SQL Server 迁移到 Azure SQL 托管实例时,必须先将相应的证书迁移到 SQL 托管实例。
本文重点介绍如何将数据库从 SQL Server 迁移到 Azure SQL 托管实例。 若要在 SQL 托管实例之间移动数据库,请查看:
若要完成本文中的步骤,需要符合以下先决条件:
- 已在本地服务器或其他能够访问已导出为文件的证书的计算机上安装了 Pvk2Pfx 命令行工具。 Pvk2Pfx 工具是企业 Windows 驱动程序工具包(一个自包含性命令行环境)的一部分。
- 已安装 Windows PowerShell 5.0 或更高版本。
确保你有以下内容:
在 PowerShell 中运行以下命令,以安装/更新模块:
Install-Module -Name Az.Sql
Update-Module -Name Az.Sql
可直接从源 SQL Server 实例导出证书,或者从证书存储导出(如果证书保存在证书存储中)。
在 SQL Server Management Studio 中使用以下步骤导出证书,并将其转换为 .pfx 格式。 以下步骤中,通用名称 TDE_Cert 和 full_path 被用作证书名称和文件名及路径。 应将它们替换为实际名称。
在 SSMS 中打开新的查询窗口,并连接到源 SQL Server 实例。
使用以下脚本列出 TDE 保护的数据库,并获取用于对要迁移的数据库进行保护加密的证书的名称:
USE master GO SELECT db.name as [database_name], cer.name as [certificate_name] FROM sys.dm_database_encryption_keys dek LEFT JOIN sys.certificates cer ON dek.encryptor_thumbprint = cer.thumbprint INNER JOIN sys.databases db ON dek.database_id = db.database_id WHERE dek.encryption_state = 3
执行以下脚本,将证书导出到一对文件(.cer 和 .pvk),并保存公钥和私钥信息:
USE master GO BACKUP CERTIFICATE TDE_Cert TO FILE = 'c:\full_path\TDE_Cert.cer' WITH PRIVATE KEY ( FILE = 'c:\full_path\TDE_Cert.pvk', ENCRYPTION BY PASSWORD = '<SomeStrongPassword>' )
在 PowerShell 控制台中使用 Pvk2Pfx 工具,将证书信息从一对新建文件复制到 .pfx 文件:
.\pvk2pfx -pvk c:/full_path/TDE_Cert.pvk -pi "<SomeStrongPassword>" -spc c:/full_path/TDE_Cert.cer -pfx c:/full_path/TDE_Cert.pfx
如果证书保存在 SQL Server 本地计算机证书存储中,可使用以下步骤导出该证书:
打开 PowerShell 控制台并执行以下命令,以打开 Microsoft 管理控制台的“证书”管理单元:
certlm
在“证书”MMC 管理单元中,展开路径“个人”>“证书”以查看证书列表。
右键单击证书,然后单击“导出”。
遵循向导将证书和私钥导出为 .pfx 格式。
Importante
迁移的证书仅用于还原受 TDE 保护的数据库。 还原完成后不久,迁移的证书将由不同的保护程序(服务管理的证书或密钥保管库中的非对称密钥)替换,具体取决于在实例上设置的 TDE 类型。
在 PowerShell 中开始准备步骤:
# import the module into the PowerShell session Import-Module Az # connect to Azure with an interactive dialog for sign-in Connect-AzAccount -EnvironmentName AzureChinaCloud # list subscriptions available and copy id of the subscription target the managed instance belongs to Get-AzSubscription # set subscription for the session Select-AzSubscription <subscriptionId>
完成所有准备步骤后,运行以下命令将 base-64 编码的证书上传到目标托管实例:
# If you are using PowerShell 6.0 or higher, run this command: $fileContentBytes = Get-Content 'C:/full_path/TDE_Cert.pfx' -AsByteStream # If you are using PowerShell 5.x, uncomment and run this command instead of the one above: # $fileContentBytes = Get-Content 'C:/full_path/TDE_Cert.pfx' -Encoding Byte $base64EncodedCert = [System.Convert]::ToBase64String($fileContentBytes) $securePrivateBlob = $base64EncodedCert | ConvertTo-SecureString -AsPlainText -Force $password = "<password>" $securePassword = $password | ConvertTo-SecureString -AsPlainText -Force Add-AzSqlManagedInstanceTransparentDataEncryptionCertificate -ResourceGroupName "<resourceGroupName>" ` -ManagedInstanceName "<managedInstanceName>" -PrivateBlob $securePrivateBlob -Password $securePassword
现在,该证书可在指定的托管实例中使用,并且可以成功还原相应的 TDE 保护数据库的备份。
Nota
在 sys.certificates 目录视图中看不到上传的证书。 若要确认证书是否成功上传,可以运行 RESTORE FILELISTONLY 命令。
本文已介绍如何将用于通过透明数据加密保护数据库加密密钥的证书从本地或 IaaS SQL Server 实例迁移到 Azure SQL 托管实例。
请参阅将数据库备份还原到 Azure SQL 托管实例,了解如何将数据库备份还原到 Azure SQL 托管实例。