使用 PowerShell & Azure Database Migration Service 將 SQL Server 線上移轉至 SQL 受控執行個體
在本文中,您將使用 Microsoft Azure PowerShell,將已還原至內部部署 SQL Server 2005 (或更新版本) 執行個體的 Adventureworks2016 資料庫線上移轉至 Azure SQL SQL 受控執行個體。 您可以使用 Microsoft Azure PowerShell 中的 Az.DataMigration
模組,將資料庫從 SQL Server 執行個體移轉至 SQL 受控執行個體。
在本文中,您將學會如何:
- 建立資源群組。
- 建立 Azure 資料庫移轉服務的執行個體。
- 在 Azure Database Migration Service 的執行個體中建立移轉專案。
- 線上執行移轉
提示
在 Azure Database Migration Service 中,您可以在離線或連線時移轉資料庫。 在離線移轉中,當移轉開始時,應用程式即會開始停機。 若要將停機限制於在移轉後完全移轉至新環境所需的時間,請使用線上移轉。 建議您測試離線移轉,以決定停機是否在可接受範圍。 如果無法接受預期的關閉,則請執行線上移轉。
本文提供線上移轉的步驟,但也可以離線移轉。
必要條件
若要完成這些步驟,您需要:
SQL Server 2016 或更新版本 (任何版本)。
AdventureWorks2016 資料庫的本機復本,可在這裡下載。
啟用 SQL Server Express 安裝預設已停用的 TCP/IP 通訊協定。 依照啟用或停用伺服器網路通訊協定一文的說明,啟用 TCP/IP 通訊協定。
Azure 訂用帳戶。 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
SQL 受控執行個體。 您可依照建立 ASQL 受控執行個體一文中的詳細資料來建立 SQL 受控執行個體。
下載及安裝 Data Migration Assistant 3.3 版或更新版本。
使用 Azure Resource Manager 部署模型所建立的 Microsoft Azure 虛擬網路,其可使用 ExpressRoute 或 VPN 為 Azure Database Migration Service 提供與內部部署來源伺服器的站對站連線。
已使用「Data Migration Assistant」完成內部部署資料庫和結構描述移轉的評估,如同執行 SQL Server 移轉評估一文中所述。
使用 Install-Module PowerShell cmdlet,從 PowerShell 資料庫下載並安裝
Az.DataMigration
模組 (0.7.2 版或更新版本)。確定用來連線至來源 SQL Server 執行個體的認證具有 CONTROL SERVER 權限。
確定用來連線至目標 SQL 受控執行個體的認證,在目標 SQL 受控執行個體資料庫上具有 CONTROL DATABASE 權限。
重要
對於線上移轉,您必須已經設定 Microsoft Entra 認證。 如需詳細資訊,請參閱使用入口網站來建立可存取資源的 Microsoft Entra 應用程式和服務主體一文。
建立資源群組
Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。
使用 New-AzResourceGroup
命令建立資源群組。
下列範例會在美國東部區域建立名為 myResourceGroup 的資源群組。
New-AzResourceGroup -ResourceGroupName myResourceGroup -Location EastUS
建立 DMS 的執行個體
您可以使用 New-AzDataMigrationService
Cmdlet,來建立新的 Azure 資料庫移轉服務執行個體。
此 Cmdlet 預期有下列必要參數:
- Azure 資源群組名稱。 您可使用
New-AzResourceGroup
命令來建立如先前所示的 Azure 資源群組,並提供其名稱作為參數。 - 服務名稱。 字串會對應至 Azure Database Migration Service 所需的唯一服務名稱。
- 位置。 指定服務的位置。 指定 Azure 資料中心位置,例如美國西部或東南亞。
- SKU。 此參數會對應至 DMS SKU 名稱。 目前支援的 SKU 名稱為 Basic_1vCore、Basic_2vCores、GeneralPurpose_4vCores。
- 虛擬子網路識別碼。 您可使用 Cmdlet
New-AzVirtualNetworkSubnetConfig
來建立子網路。
下列範例會使用名為 MyVNET 的虛擬網路及名為 MySubnet 的子網路,在位於東部區域的 MyDMSResourceGroup 資源群組中建立名為 MyDMS 的服務。
$vNet = Get-AzVirtualNetwork -ResourceGroupName MyDMSResourceGroup -Name MyVNET
$vSubNet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vNet -Name MySubnet
$service = New-AzDms -ResourceGroupName myResourceGroup `
-ServiceName MyDMS `
-Location EastUS `
-Sku Basic_2vCores `
-VirtualSubnetId $vSubNet.Id`
建立移轉專案
建立 Azure 資料庫移轉服務執行個體之後,建立移轉專案。 Azure 資料庫移轉服務專案需要來源和目標執行個體的連線資訊,以及您想要移轉的資料庫清單。 定義來源和目標連線能力的連線字串。
下列指令碼會定義來源 SQL Server 連線詳細資料:
# Source connection properties
$sourceDataSource = "<mysqlserver.domain.com/privateIP of source SQL>"
$sourceUserName = "domain\user"
$sourcePassword = "mypassword"
下列指令碼會定義目標 SQL 受管理執行個體連線詳細資料:
# Target MI connection properties
$targetMIResourceId = "/subscriptions/<subid>/resourceGroups/<rg>/providers/Microsoft.Sql/managedInstances/<myMI>"
$targetUserName = "<user>"
$targetPassword = "<password>"
定義來源和目標資料庫對應
提供要在此移轉專案中移轉的資料庫
下列指令碼會將來源資料庫對應至目標 SQL 受管理執行個體 (具有所提供的名稱) 上的個別新資料庫。
# Selected databases (Source database name to target database name mapping)
$selectedDatabasesMap = New-Object System.Collections.Generic.Dictionary"[String,String]"
$selectedDatabasesMap.Add("<source database name>", "<target database name> ")
對於多個資料庫,請使用下列格式,將資料庫清單新增至上述指令碼:
$selectedDatabasesMap = New-Object System.Collections.Generic.Dictionary"[String,String]"
$selectedDatabasesMap.Add("<source database name1>", "<target database name1> ")
$selectedDatabasesMap.Add("<source database name2>", "<target database name2> ")
建立 DMS 專案
您可以在 DMS 執行個體內建立 Azure Database Migration Service 專案。
# Create DMS project
$project = New-AzDataMigrationProject `
-ResourceGroupName $dmsResourceGroupName `
-ServiceName $dmsServiceName `
-ProjectName $dmsProjectName `
-Location $dmsLocation `
-SourceType SQL `
-TargetType SQLMI `
# Create selected databases object
$selectedDatabases = @();
foreach ($sourceDbName in $selectedDatabasesMap.Keys){
$targetDbName = $($selectedDatabasesMap[$sourceDbName])
$selectedDatabases += New-AzDmsSelectedDB -MigrateSqlServerSqlDbMi `
-Name $sourceDbName `
-TargetDatabaseName $targetDbName `
-BackupFileShare $backupFileShare `
}
建立備份 FileShare 物件
現在,請建立 FileShare 物件,用以代表 Azure Database Migration Service 可使用 New-AzDmsFileShare cmdlet 移入來源資料庫備份的本機 SMB 網路共用。
# SMB Backup share properties
$smbBackupSharePath = "\\shareserver.domain.com\mybackup"
$smbBackupShareUserName = "domain\user"
$smbBackupSharePassword = "<password>"
# Create backup file share object
$smbBackupSharePasswordSecure = ConvertTo-SecureString -String $smbBackupSharePassword -AsPlainText -Force
$smbBackupShareCredentials = New-Object System.Management.Automation.PSCredential ($smbBackupShareUserName, $smbBackupSharePasswordSecure)
$backupFileShare = New-AzDmsFileShare -Path $smbBackupSharePath -Credential $smbBackupShareCredentials
定義 Azure 儲存體
選取要用來移轉的 Azure 儲存體容器:
# Storage resource id
$storageAccountResourceId = "/subscriptions/<subscriptionname>/resourceGroups/<rg>/providers/Microsoft.Storage/storageAccounts/<mystorage>"
設定 Microsoft Entra 應用程式
針對線上 SQL 受控執行個體移轉提供 Microsoft Entra ID 的必要詳細資料:
# AAD properties
$AADAppId = "<appid-guid>"
$AADAppKey = "<app-key>"
# Create AAD object
$AADAppKeySecure = ConvertTo-SecureString $AADAppKey -AsPlainText -Force
$AADApp = New-AzDmsAadApp -ApplicationId $AADAppId -AppKey $AADAppKeySecure
建立並啟動移轉工作
接下來,建立並啟動 Azure Database Migration Service 工作。 使用變數呼叫來源和目標,並列出要移轉的資料庫資料表:
# Managed Instance online migration properties
$dmsTaskName = "testmigration1"
# Create source connection info
$sourceConnInfo = New-AzDmsConnInfo -ServerType SQL `
-DataSource $sourceDataSource `
-AuthType WindowsAuthentication `
-TrustServerCertificate:$true
$sourcePasswordSecure = ConvertTo-SecureString -String $sourcePassword -AsPlainText -Force
$sourceCredentials = New-Object System.Management.Automation.PSCredential ($sourceUserName, $sourcePasswordSecure)
# Create target connection info
$targetConnInfo = New-AzDmsConnInfo -ServerType SQLMI `
-MiResourceId $targetMIResourceId
$targetPasswordSecure = ConvertTo-SecureString -String $targetPassword -AsPlainText -Force
$targetCredentials = New-Object System.Management.Automation.PSCredential ($targetUserName, $targetPasswordSecure)
下列範例會建立並啟動線上移轉工作:
# Create DMS migration task
$migTask = New-AzDataMigrationTask -TaskType MigrateSqlServerSqlDbMiSync `
-ResourceGroupName $dmsResourceGroupName `
-ServiceName $dmsServiceName `
-ProjectName $dmsProjectName `
-TaskName $dmsTaskName `
-SourceConnection $sourceConnInfo `
-SourceCred $sourceCredentials `
-TargetConnection $targetConnInfo `
-TargetCred $targetCredentials `
-SelectedDatabase $selectedDatabases `
-BackupFileShare $backupFileShare `
-AzureActiveDirectoryApp $AADApp `
-StorageResourceId $storageAccountResourceId
如需詳細資訊,請參閱 New-AzDataMigrationTask。
監視移轉
若要監視移轉,請執行下列工作。
檢查工作的狀態。
# Get migration task status details
$migTask = Get-AzDataMigrationTask `
-ResourceGroupName $dmsResourceGroupName `
-ServiceName $dmsServiceName `
-ProjectName $dmsProjectName `
-Name $dmsTaskName `
-ResultType DatabaseLevelOutput `
-Expand
# Task state will be either of 'Queued', 'Running', 'Succeeded', 'Failed', 'FailedInputValidation' or 'Faulted'
$taskState = $migTask.ProjectTask.Properties.State
# Display task state
$taskState | Format-List
使用下列來取得錯誤清單:-
# Get task errors
$taskErrors = $migTask.ProjectTask.Properties.Errors
# Display task errors
foreach($taskError in $taskErrors){
$taskError | Format-List
}
# Get database level details
$databaseLevelOutputs = $migTask.ProjectTask.Properties.Output
# Display database level details
foreach($databaseLevelOutput in $databaseLevelOutputs){
# This is the source database name.
$databaseName = $databaseLevelOutput.SourceDatabaseName;
Write-Host "=========="
Write-Host "Start migration details for database " $databaseName
# This is the status for that database - It will be either of:
# INITIAL, FULL_BACKUP_UPLOADING, FULL_BACKUP_UPLOADED, LOG_FILES_UPLOADING,
# CUTOVER_IN_PROGRESS, CUTOVER_INITIATED, CUTOVER_COMPLETED, COMPLETED, CANCELLED, FAILED
$databaseMigrationState = $databaseLevelOutput.MigrationState;
# Details about last restored backup. This contains file names, LSN, backup date, etc
$databaseLastRestoredBackup = $databaseLevelOutput.LastRestoredBackupSetInfo
# Details about last restored backup. This contains file names, LSN, backup date, etc
$databaseLastRestoredBackup = $databaseLevelOutput.LastRestoredBackupSetInfo
# Details about last Currently active/most recent backups. This contains file names, LSN, backup date, etc
$databaseActiveBackpusets = $databaseLevelOutput.ActiveBackupSets
# Display info
$databaseLevelOutput | Format-List
Write-Host "Currently active/most recent backupset details:"
$databaseActiveBackpusets | select BackupStartDate, BackupFinishedDate, FirstLsn, LastLsn -ExpandProperty ListOfBackupFiles | Format-List
Write-Host "Last restored backupset details:"
$databaseLastRestoredBackupFiles | Format-List
Write-Host "End migration details for database " $databaseName
Write-Host "=========="
}
執行完全移轉
透過線上移轉,會執行資料庫的完整備份和還原,然後會繼續還原儲存在 BackupFileShare 中的交易記錄。
當 Azure SQL 受控執行個體中的資料庫以最新的資料來進行更新,並與來源資料庫進行同步時,您可以執行完全移轉。
下列範例會完成完全移轉。 使用者可自行決定是否要叫用此命令。
$command = Invoke-AzDmsCommand -CommandType CompleteSqlMiSync `
-ResourceGroupName myResourceGroup `
-ServiceName $service.Name `
-ProjectName $project.Name `
-TaskName myDMSTask `
-DatabaseName "Source DB Name"
刪除 Azure Database Migration Service 的執行個體
完成移轉之後,您可以刪除 Azure Database Migration Service 執行個體:
Remove-AzDms -ResourceGroupName myResourceGroup -ServiceName MyDMS
其他資源
如需其他移轉案例 (來源/目標配對) 的相關資訊,請參閱 Microsoft 資料庫移轉指南。
下一步
請在「什麼是 Azure Database Migration Service?」一文中進一步了解 Azure Database Migration Service。