使用 PowerShell 将弹性池添加到故障转移组Use PowerShell to add an elastic pool to a failover group
Azure SQL 数据库
此 Azure PowerShell 脚本示例在 Azure SQL 数据库中创建一个数据库,将其添加到弹性池,创建一个故障转移组,然后测试故障转移。This Azure PowerShell script example creates a database in Azure SQL Database, adds it to an elastic pool, creates a failover group, and tests failover.
如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅。If you don't have an Azure trail subscription, create a trial subscription before you begin.
备注
本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介。To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShell。For Az module installation instructions, see Install Azure PowerShell.
本教程需要 Az PowerShell 1.4.0 或更高版本。This tutorial requires Az PowerShell 1.4.0 or later. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzAccount -Environment AzureChinaCloud
来创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Connect-AzAccount -Environment AzureChinaCloud
to create a connection with Azure.
示例脚本Sample scripts
# Set variables for your server and database
$subscriptionId = '<Subscription-ID>'
$randomIdentifier = $(Get-Random)
$resourceGroupName = "myResourceGroup-$randomIdentifier"
$location = "China East 2"
$adminLogin = "azureuser"
$password = "PWD27!"+(New-Guid).Guid
$serverName = "mysqlserver-$randomIdentifier"
$poolName = "myElasticPool"
$databaseName = "mySampleDatabase"
$drLocation = "China North 2"
$drServerName = "mysqlsecondary-$randomIdentifier"
$failoverGroupName = "failovergrouptutorial-$randomIdentifier"
# The ip address range that you want to allow to access your server
# Leaving at 0.0.0.0 will prevent outside-of-azure connections
$startIp = "0.0.0.0"
$endIp = "0.0.0.0"
# Show randomized variables
Write-host "Resource group name is" $resourceGroupName
Write-host "Password is" $password
Write-host "Server name is" $serverName
Write-host "DR Server name is" $drServerName
Write-host "Failover group name is" $failoverGroupName
# Set subscription ID
Set-AzContext -SubscriptionId $subscriptionId
# Create a resource group
Write-host "Creating resource group..."
$resourceGroup = New-AzResourceGroup -Name $resourceGroupName -Location $location -Tag @{Owner="SQLDB-Samples"}
$resourceGroup
# Create a server with a system-wide unique server name
Write-host "Creating primary logical server..."
New-AzSqlServer -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-Location $location `
-SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $adminLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
Write-host "Primary logical server = " $serverName
# Create a server firewall rule that allows access from the specified IP range
Write-host "Configuring firewall for primary logical server..."
New-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-FirewallRuleName "AllowedIPs" -StartIpAddress $startIp -EndIpAddress $endIp
Write-host "Firewall configured"
# Create General Purpose Gen5 database with 2 vCore
Write-host "Creating a gen5 2 vCore database..."
$database = New-AzSqlDatabase -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-DatabaseName $databaseName `
-Edition "GeneralPurpose" `
-VCore 2 `
-ComputeGeneration Gen5 `
-MinimumCapacity 1 `
-SampleName "AdventureWorksLT"
$database
# Create primary Gen5 elastic 2 vCore pool
Write-host "Creating elastic pool..."
$elasticPool = New-AzSqlElasticPool -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-ElasticPoolName $poolName `
-Edition "GeneralPurpose" `
-vCore 2 `
-ComputeGeneration Gen5
$elasticPool
# Add single db into elastic pool
Write-host "Creating elastic pool..."
$addDatabase = Set-AzSqlDatabase -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-DatabaseName $databaseName `
-ElasticPoolName $poolName
$addDatabase
# Create a secondary server in the failover region
Write-host "Creating a secondary logical server in the failover region..."
New-AzSqlServer -ResourceGroupName $resourceGroupName `
-ServerName $drServerName `
-Location $drLocation `
-SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $adminlogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
Write-host "Secondary logical server =" $drServerName
# Create a server firewall rule that allows access from the specified IP range
Write-host "Configuring firewall for secondary logical server..."
New-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName `
-ServerName $drServerName `
-FirewallRuleName "AllowedIPs" -StartIpAddress $startIp -EndIpAddress $endIp
Write-host "Firewall configured"
# Create secondary Gen5 elastic 2 vCore pool
Write-host "Creating secondary elastic pool..."
$elasticPool = New-AzSqlElasticPool -ResourceGroupName $resourceGroupName `
-ServerName $drServerName `
-ElasticPoolName $poolName `
-Edition "GeneralPurpose" `
-vCore 2 `
-ComputeGeneration Gen5
$elasticPool
# Create a failover group between the servers
Write-host "Creating failover group..."
New-AzSqlDatabaseFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-PartnerServerName $drServerName `
-FailoverGroupName $failoverGroupName `
-FailoverPolicy Automatic `
-GracePeriodWithDataLossHours 2
Write-host "Failover group created successfully."
# Add elastic pool to the failover group
Write-host "Enumerating databases in elastic pool...."
$FailoverGroup = Get-AzSqlDatabaseFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-FailoverGroupName $failoverGroupName
$databases = Get-AzSqlElasticPoolDatabase `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-ElasticPoolName $poolName
Write-host "Adding databases to failover group..."
$failoverGroup = $failoverGroup | Add-AzSqlDatabaseToFailoverGroup `
-Database $databases
$failoverGroup
# Check role of secondary replica
Write-host "Confirming the secondary server is secondary...."
(Get-AzSqlDatabaseFailoverGroup `
-FailoverGroupName $failoverGroupName `
-ResourceGroupName $resourceGroupName `
-ServerName $drServerName).ReplicationRole
# Failover to secondary server
Write-host "Failing over failover group to the secondary..."
Switch-AzSqlDatabaseFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $drServerName `
-FailoverGroupName $failoverGroupName
Write-host "Failover group failed over to" $drServerName
# Check role of secondary replica
Write-host "Confirming the secondary server is now primary"
(Get-AzSqlDatabaseFailoverGroup `
-FailoverGroupName $failoverGroupName `
-ResourceGroupName $resourceGroupName `
-ServerName $drServerName).ReplicationRole
# Revert failover to primary server
Write-host "Failing over failover group to the primary...."
Switch-AzSqlDatabaseFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-FailoverGroupName $failoverGroupName
Write-host "Failover group failed over to" $serverName
# Clean up resources by removing the resource group
# Write-host "Removing resource group..."
# Remove-AzResourceGroup -ResourceGroupName $resourceGroupName
# Write-host "Resource group removed =" $resourceGroupName
清理部署Clean up deployment
使用以下命令删除资源组及其相关的所有资源。Use the following command to remove the resource group and all resources associated with it.
Remove-AzResourceGroup -ResourceGroupName $resourceGroupName
脚本说明Script explanation
此脚本使用以下命令。This script uses the following commands. 表中的每条命令链接到特定于命令的文档。Each command in the table links to command-specific documentation.
命令Command | 注释Notes |
---|---|
New-AzResourceGroupNew-AzResourceGroup | 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored. |
New-AzSqlServerNew-AzSqlServer | 创建托管数据库和弹性池的服务器。Creates a server that hosts databases and elastic pools. |
New-AzSqlServerFirewallRuleNew-AzSqlServerFirewallRule | 为服务器创建服务器级防火墙规则。Creates a server-level firewall rule for a server. |
New-AzSqlDatabaseNew-AzSqlDatabase | 新建数据库。Creates a new database. |
New-AzSqlElasticPoolNew-AzSqlElasticPool | 创建弹性数据库池。Creates an elastic database pool. |
Set-AzSqlDatabaseSet-AzSqlDatabase | 设置数据库的属性,或将现有数据库移到弹性池中。Sets properties for a database, or moves an existing database into an elastic pool. |
New-AzSqlDatabaseFailoverGroupNew-AzSqlDatabaseFailoverGroup | 新建故障转移组。Creates a new failover group. |
Get-AzSqlDatabaseGet-AzSqlDatabase | 获取一个或多个数据库。Gets one or more databases. |
Add-AzSqlDatabaseToFailoverGroupAdd-AzSqlDatabaseToFailoverGroup | 将一个或多个数据库添加到故障转移组。Adds one or more databases to a failover group. |
Get-AzSqlDatabaseFailoverGroupGet-AzSqlDatabaseFailoverGroup | 获取或列出数据库故障转移组。Gets or lists database failover groups. |
Switch-AzSqlDatabaseFailoverGroupSwitch-AzSqlDatabaseFailoverGroup | 执行数据库故障转移组的故障转移。Executes a failover of a database failover group. |
Remove-AzResourceGroupRemove-AzResourceGroup | 删除资源组Removes a resource group |
后续步骤Next steps
有关 Azure PowerShell 的详细信息,请参阅 Azure PowerShell 文档。For more information on the Azure PowerShell, see Azure PowerShell documentation.
可以在 Azure SQL 数据库 PowerShell 脚本中找到更多 SQL 数据库 PowerShell 脚本示例。Additional SQL Database PowerShell script samples can be found in the Azure SQL Database PowerShell scripts.