使用 PowerShell 为 Azure SQL 数据库中的共用数据库配置活动异地复制

适用于:Azure SQL 数据库

此 Azure PowerShell 脚本示例为 Azure SQL 数据库中的共用数据库配置活动异地复制,并将其故障转移到数据库的次要副本。

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

注意

本文使用 Azure Az PowerShell 模块,这是与 Azure 交互时推荐使用的 PowerShell 模块。 若要开始使用 Az PowerShell 模块,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

本教程需要 Az PowerShell 1.4.0 或更高版本。 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。 此外,还需要运行 Connect-AzAccount -EnvironmentName AzureChinaCloud 以创建与 Azure 的连接。

示例脚本

# Connect-AzAccount -Environment AzureChinaCloud
$subscriptionId = "<Subscription-ID>"
# Set the resource group name and location for your server
$primaryResourceGroupName = "myPrimaryResourceGroup-$(Get-Random)"
$secondaryResourceGroupName = "mySecondaryResourceGroup-$(Get-Random)"
$primaryLocation = "chinaeast2"
$secondaryLocation = "chinanorth2"
# The logical server names have to be unique in the system
$primaryServerName = "primary-server-$(Get-Random)"
$secondaryServerName = "secondary-server-$(Get-Random)"
# Set an admin login and password for your servers
$adminSqlLogin = "<admin>"
$password = "<password>"
# The sample database name
$databaseName = "mySampleDatabase"
# The IP address ranges that you want to allow to access your servers
$primaryStartIp = "0.0.0.0"
$primaryEndIp = "0.0.0.0"
$secondaryStartIp = "0.0.0.0"
$secondaryEndIp = "0.0.0.0"
# The elastic pool names
$primaryPoolName = "PrimaryPool"
$secondaryPoolName = "SecondaryPool"

# Set subscription
Set-AzContext -SubscriptionId $subscriptionId

# Create two new resource groups
$primaryResourceGroup = New-AzResourceGroup -Name $primaryResourceGroupName -Location $primaryLocation
$secondaryResourceGroup = New-AzResourceGroup -Name $secondaryResourceGroupName -Location $secondaryLocation

# Create two new logical servers with a system-wide unique server name

$adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force)

$primaryServerParams = @{
    ResourceGroupName           = $primaryResourceGroupName
    ServerName                  = $primaryServerName
    Location                    = $primaryLocation
    SqlAdministratorCredentials = $adminCredential
}
$primaryServer = New-AzSqlServer @primaryServerParams

$secondaryServerParams = @{
    ResourceGroupName           = $secondaryResourceGroupName
    ServerName                  = $secondaryServerName
    Location                    = $secondaryLocation
    SqlAdministratorCredentials = $adminCredential
}
$secondaryServer = New-AzSqlServer @secondaryServerParams

# Create a server firewall rule for each server that allows access from the specified IP range
$primaryFirewallParams = @{
    ResourceGroupName = $primaryResourceGroupName
    ServerName        = $primaryServerName
    FirewallRuleName  = "AllowedIPs"
    StartIpAddress    = $primaryStartIp
    EndIpAddress      = $primaryEndIp
}
$primaryServerFirewallRule = New-AzSqlServerFirewallRule @primaryFirewallParams

$secondaryFirewallParams = @{
    ResourceGroupName = $secondaryResourceGroupName
    ServerName        = $secondaryServerName
    FirewallRuleName  = "AllowedIPs"
    StartIpAddress    = $secondaryStartIp
    EndIpAddress      = $secondaryEndIp
}
$secondaryServerFirewallRule = New-AzSqlServerFirewallRule @secondaryFirewallParams

# Create a pool in each of the servers
$primaryPoolParams = @{
    ResourceGroupName = $primaryResourceGroupName
    ServerName        = $primaryServerName
    ElasticPoolName   = $primaryPoolName
    Edition           = "Standard"
    Dtu               = 50
    DatabaseDtuMin    = 10
    DatabaseDtuMax    = 50
}
$primaryPool = New-AzSqlElasticPool @primaryPoolParams

$secondaryPoolParams = @{
    ResourceGroupName = $secondaryResourceGroupName
    ServerName        = $secondaryServerName
    ElasticPoolName   = $secondaryPoolName
    Edition           = "Standard"
    Dtu               = 50
    DatabaseDtuMin    = 10
    DatabaseDtuMax    = 50
}
$secondaryPool = New-AzSqlElasticPool @secondaryPoolParams

# Create a blank database in the pool on the primary server
$databaseParams = @{
    ResourceGroupName = $primaryResourceGroupName
    ServerName        = $primaryServerName
    DatabaseName      = $databaseName
    ElasticPoolName   = $primaryPoolName
}
$database = New-AzSqlDatabase @databaseParams

# Establish Active Geo-Replication
$primaryDatabaseParams = @{
    ResourceGroupName = $primaryResourceGroupName
    ServerName        = $primaryServerName
    DatabaseName      = $databaseName
}
$database = Get-AzSqlDatabase @primaryDatabaseParams
$secondaryParams = @{
    PartnerResourceGroupName = $secondaryResourceGroupName
    PartnerServerName        = $secondaryServerName
    SecondaryElasticPoolName = $secondaryPoolName
    AllowConnections         = "All"
}
$database | New-AzSqlDatabaseSecondary @secondaryParams

# Initiate a planned failover
$secondaryDatabaseParams = @{
    ResourceGroupName = $secondaryResourceGroupName
    ServerName        = $secondaryServerName
    DatabaseName      = $databaseName
}
$database = Get-AzSqlDatabase @secondaryDatabaseParams
$database | Set-AzSqlDatabaseSecondary -PartnerResourceGroupName $primaryResourceGroupName -Failover

# Monitor Geo-Replication config and health after failover
$database = Get-AzSqlDatabase @secondaryDatabaseParams
$replicationLinkParams = @{
    PartnerResourceGroupName = $primaryResourceGroupName
    PartnerServerName        = $primaryServerName
}
$database | Get-AzSqlDatabaseReplicationLink @replicationLinkParams

# Clean up deployment
# Remove-AzResourceGroup -ResourceGroupName $primaryResourceGroupName
# Remove-AzResourceGroup -ResourceGroupName $secondaryResourceGroupName

清理部署

使用以下命令删除资源组及其相关的所有资源。

Remove-AzResourceGroup -ResourceGroupName $primaryresourcegroupname
Remove-AzResourceGroup -ResourceGroupName $secondaryresourcegroupname

脚本说明

此脚本使用以下命令。 表中的每条命令链接到特定于命令的文档。

命令 注释
New-AzResourceGroup 创建用于存储所有资源的资源组。
New-AzSqlServer 创建托管数据库和弹性池的服务器。
New-AzSqlElasticPool 创建弹性池。
New-AzSqlDatabase 在服务器中创建数据库。
Set-AzSqlDatabase 更新数据库属性,或者将数据库移入、移出弹性池或在弹性池之间移动。
New-AzSqlDatabaseSecondary 为现有数据库创建辅助数据库,并开始数据复制。
Get-AzSqlDatabase 获取一个或多个数据库。
Set-AzSqlDatabaseSecondary 将辅助数据库切换为主数据库,以便启动故障转移。
Get-AzSqlDatabaseReplicationLink 获取 Azure SQL 数据库和资源组或逻辑 SQL 服务器之间的异地复制链路。
Remove-AzResourceGroup 删除资源组,包括所有嵌套的资源。