Usare PowerShell per configurare la replica geografica attiva per un database SQL di Azure in poolUse PowerShell to configure active geo-replication for a pooled Azure SQL database

Questo esempio di script di PowerShell configura la replica geografica attiva per un database SQL di Azure in un pool elastico e ne esegue il failover nella replica secondaria del database SQL di Azure.This PowerShell script example configures active geo-replication for an Azure SQL database in an elastic pool and fails it over to the secondary replica of the Azure SQL database.

Questo esempio richiede il modulo Azure PowerShell 4.0 o versioni successive.This sample requires the Azure PowerShell module version 4.0 or later. Eseguire Get-Module -ListAvailable AzureRM per trovare la versione.Run Get-Module -ListAvailable AzureRM to find the version. Se è necessario eseguire l'installazione o l'aggiornamento, vedere come installare il modulo Azure PowerShell.If you need to install or upgrade, see Install Azure PowerShell module.

Eseguire Login-AzureRmAccount per creare una connessione con Azure.Run Login-AzureRmAccount to create a connection with Azure.

Script di esempioSample scripts

# Login-AzureRmAccount
# Set the resource group name and location for your serverw
$primaryresourcegroupname = "myPrimaryResourceGroup-$(Get-Random)"
$secondaryresourcegroupname = "mySecondaryResourceGroup-$(Get-Random)"
$primarylocation = "westus2"
$secondarylocation = "southcentralus"
# 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
$adminlogin = "ServerAdmin"
$password = "ChangeYourAdminPassword1"
# 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"

# Create two new resource groups
$primaryresourcegroupname = New-AzureRmResourceGroup -Name $primaryresourcegroupname -Location $primarylocation
$secondaryresourcegroupname = New-AzureRmResourceGroup -Name $secondaryresourcegroupname -Location $secondarylocation

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

$primaryserver = New-AzureRmSqlServer -ResourceGroupName $primaryresourcegroupname `
    -ServerName $primaryservername `
    -Location $primarylocation `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminlogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
$secondaryserver = New-AzureRmSqlServer -ResourceGroupName $secondaryresourcegroupname `
    -ServerName $secondaryservername `
    -Location $secondarylocation `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminlogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))

# Create a server firewall rule for each server that allows access from the specified IP range
$primaryserverfirewallrule = New-AzureRmSqlServerFirewallRule -ResourceGroupName $primaryresourcegroupname `
    -ServerName $primaryservername `
    -FirewallRuleName "AllowedIPs" -StartIpAddress $primarystartip -EndIpAddress $primaryendip
$secondaryserverfirewallrule = New-AzureRmSqlServerFirewallRule -ResourceGroupName $secondaryresourcegroupname `
    -ServerName $secondaryservername `
    -FirewallRuleName "AllowedIPs" -StartIpAddress $secondarystartip -EndIpAddress $secondaryendip

# Create a pool in each of the servers
$primarypool = New-AzureRmSqlElasticPool -ResourceGroupName $primaryresourcegroupname `
    -ServerName $primaryservername `
    -ElasticPoolName $primarypoolname `
    -Edition "Standard" `
    -Dtu 50 `
    -DatabaseDtuMin 10 `
    -DatabaseDtuMax 50
$secondarypool = New-AzureRmSqlElasticPool -ResourceGroupName $secondaryresourcegroupname `
    -ServerName $secondaryservername `
    -ElasticPoolName $secondarypoolname `
    -Edition "Standard" `
    -Dtu 50 `
    -DatabaseDtuMin 10 `
    -DatabaseDtuMax 50

# Create a blank database in the pool on the primary server
$database = New-AzureRmSqlDatabase  -ResourceGroupName $primaryresourcegroupname `
    -ServerName $primaryservername `
    -DatabaseName $databasename `
    -ElasticPoolName $primarypoolname

# Establish Active Geo-Replication
$database = Get-AzureRmSqlDatabase -ResourceGroupName $primaryresourcegroupname `
    -ServerName $primaryservername `
    -DatabaseName $databasename
$database | New-AzureRmSqlDatabaseSecondary -PartnerResourceGroupName $secondaryresourcegroupname `
    -PartnerServerName $secondaryservername `
    -SecondaryElasticPoolName $secondarypoolname `
    -AllowConnections "All"

# Initiate a planned failover
$database = Get-AzureRmSqlDatabase -ResourceGroupName $secondaryresourcegroupname `
    -ServerName $secondaryservername `
    -DatabaseName $databasename 
$database | Set-AzureRmSqlDatabaseSecondary -PartnerResourceGroupName $primaryresourcegroupname -Failover

    
# Monitor Geo-Replication config and health after failover
$database = Get-AzureRmSqlDatabase -ResourceGroupName $secondaryresourcegroupname `
    -ServerName $secondaryservername `
    -DatabaseName $databasename
$database | Get-AzureRmSqlDatabaseReplicationLink -PartnerResourceGroupName $primaryresourcegroupname `
    -PartnerServerName $primaryservername

# Clean up deployment 
# Remove-AzureRmResourceGroup -ResourceGroupName $primaryresourcegroupname
# Remove-AzureRmResourceGroup -ResourceGroupName $secondaryresourcegroupname

Pulire la distribuzioneClean up deployment

Dopo l'esecuzione dello script di esempio, è possibile usare il comando seguente per rimuovere il gruppo di risorse e tutte le risorse ad esso associate.After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.

Remove-AzureRmResourceGroup -ResourceGroupName $primaryresourcegroupname
Remove-AzureRmResourceGroup -ResourceGroupName $secondaryresourcegroupname

Spiegazione dello scriptScript explanation

Questo script usa i comandi seguenti.This script uses the following commands. Ogni comando della tabella include collegamenti alla documentazione specifica del comando.Each command in the table links to command specific documentation.

ComandoCommand NoteNotes
New-AzureRmResourceGroupNew-AzureRmResourceGroup Consente di creare un gruppo di risorse in cui sono archiviate tutte le risorse.Creates a resource group in which all resources are stored.
New-AzureRmSqlServerNew-AzureRmSqlServer Crea un server logico che ospita un database o un pool elastico.Creates a logical server that hosts a database or elastic pool.
New-AzureRmSqlElasticPoolNew-AzureRmSqlElasticPool Crea un pool elastico all'interno di un server logico.Creates an elastic pool within a logical server.
New-AzureRmSqlDatabaseNew-AzureRmSqlDatabase Crea un database in un server logico come database singolo o in pool.Creates a database in a logical server as a single or a pooled database.
Set-AzureRmSqlDatabaseSet-AzureRmSqlDatabase Aggiorna le proprietà del database o sposta un database all'interno, all'esterno o tra pool elastici.Updates database properties or moves a database into, out of, or between elastic pools.
New-AzureRmSqlDatabaseSecondaryNew-AzureRmSqlDatabaseSecondary Crea un database secondario per un database esistente e avvia la replica dei dati.Creates a secondary database for an existing database and starts data replication.
Get-AzureRmSqlDatabaseGet-AzureRmSqlDatabase Ottiene uno o più database.Gets one or more databases.
Set-AzureRmSqlDatabaseSecondarySet-AzureRmSqlDatabaseSecondary Un database secondario diventa il database primario per avviare il failover.Switches a secondary database to be primary in order to initiate failover.
Get-AzureRmSqlDatabaseReplicationLinkGet-AzureRmSqlDatabaseReplicationLink Ottiene i collegamenti di replica geografica tra un database di SQL Azure e un gruppo di risorse o SQL Server.Gets the geo-replication links between an Azure SQL Database and a resource group or SQL Server.
Remove-AzureRmResourceGroupRemove-AzureRmResourceGroup Consente di eliminare un gruppo di risorse incluse tutte le risorse annidate.Deletes a resource group including all nested resources.

Passaggi successiviNext steps

Per altre informazioni su Azure PowerShell, vedere la documentazione di Azure PowerShell.For more information on the Azure PowerShell, see Azure PowerShell documentation.

Per altri esempi, vedere tra gli script di PowerShell per database SQL di Azure.Additional SQL Database PowerShell script samples can be found in the Azure SQL Database PowerShell scripts.