Menggunakan PowerShell untuk mengonfigurasi replikasi geografis aktif untuk database gabungan di Azure SQL Database

Berlaku untuk:Azure SQL Database

Contoh skrip Azure PowerShell ini mengonfigurasi geo-replikasi aktif untuk kumpulan database di Azure SQL Database dan melakukan failover ke replika sekunder database.

Jika Anda tidak memiliki langganan Azure, buat akun Azure gratis sebelum memulai.

Catatan

Artikel ini menggunakan modul Azure Az PowerShell, yang merupakan modul PowerShell yang direkomendasikan untuk berinteraksi dengan Azure. Untuk mulai menggunakan modul Az PowerShell, lihat Menginstal Azure PowerShell. Untuk mempelajari cara bermigrasi ke modul Az PowerShell, lihat Memigrasikan Azure PowerShell dari AzureRM ke Az.

Menggunakan Azure Cloud Shell

Azure meng-hosting Azure Cloud Shell, lingkungan shell interaktif yang dapat Anda gunakan melalui browser. Anda dapat menggunakan Bash atau PowerShell dengan Cloud Shell untuk bekerja dengan layanan Azure. Anda dapat menggunakan perintah Cloud Shell yang telah diinstal sebelumnya untuk menjalankan kode dalam artikel ini tanpa harus menginstal apa-apa di lingkungan lokal Anda.

Untuk memulai Azure Cloud Shell:

Opsi Contoh/Tautan
Pilih Coba di sudut kanan atas blok kode. Memilih Coba tidak secara otomatis menyalin kode ke Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
Buka https://shell.azure.com, atau pilih tombol Luncurkan Cloud Shell untuk membuka Cloud Shell di browser Anda. Screenshot that shows how to launch Cloud Shell in a new window.
Pilih tombol Cloud Shell pada bilah menu di kanan atas di portal Microsoft Azure. Screenshot that shows the Cloud Shell button in the Azure portal

Untuk menjalankan kode dalam artikel ini di Azure Cloud Shell:

  1. Mulai Cloud Shell.

  2. Pilih tombol Salin pada blok kode untuk menyalin kode.

  3. Tempelkan kode tersebut ke sesi Cloud Shell dengan memilih Ctrl+Shift+V untuk Windows dan Linux, atau dengan memilih Cmd+Shift+V untuk macOS.

  4. Pilih Enter untuk menjalankan kode.

Jika Anda memilih untuk memasang dan menggunakan PowerShell secara lokal, tutorial ini memerlukan Az PowerShell 1.4.0 atau yang lebih baru. Jika Anda perlu peningkatan, lihat Instal modul Azure PowerShell. Jika Anda menjalankan PowerShell secara lokal, Anda juga perlu menjalankan Connect-AzAccount untuk membuat koneksi dengan Azure.

Skrip sampel

# Connect-AzAccount
$SubscriptionId = ''
# Set the resource group name and location for your serverw
$primaryResourceGroupName = "myPrimaryResourceGroup-$(Get-Random)"
$secondaryResourceGroupName = "mySecondaryResourceGroup-$(Get-Random)"
$primaryLocation = "westus2"
$secondaryLocation = "eastus"
# 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
$adminSqlLgin = "SqlAdmin"
$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"

# 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

$primaryServer = New-AzSqlServer -ResourceGroupName $primaryResourceGroupName `
    -ServerName $primaryServerName `
    -Location $primaryLocation `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLgin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
$secondaryServer = New-AzSqlServer -ResourceGroupName $secondaryResourceGroupName `
    -ServerName $secondaryServerName `
    -Location $secondaryLocation `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLgin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))

# Create a server firewall rule for each server that allows access from the specified IP range
$primaryServerFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $primaryResourceGroupName `
    -ServerName $primaryServerName `
    -FirewallRuleName "AllowedIPs" -StartIpAddress $primaryStartIp -EndIpAddress $primaryEndIp
$secondaryServerFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $secondaryResourceGroupName `
    -ServerName $secondaryServerName `
    -FirewallRuleName "AllowedIPs" -StartIpAddress $secondaryStartIp -EndIpAddress $secondaryEndIp

# Create a pool in each of the servers
$primaryPool = New-AzSqlElasticPool -ResourceGroupName $primaryResourceGroupName `
    -ServerName $primaryServerName `
    -ElasticPoolName $primaryPoolName `
    -Edition "Standard" `
    -Dtu 50 `
    -DatabaseDtuMin 10 `
    -DatabaseDtuMax 50
$secondaryPool = New-AzSqlElasticPool -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-AzSqlDatabase  -ResourceGroupName $primaryResourceGroupName `
    -ServerName $primaryServerName `
    -DatabaseName $databaseName `
    -ElasticPoolName $primaryPoolName

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

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

    
# Monitor Geo-Replication config and health after failover
$database = Get-AzSqlDatabase -ResourceGroupName $secondaryResourceGroupName `
    -ServerName $secondaryServerName `
    -DatabaseName $databaseName
$database | Get-AzSqlDatabaseReplicationLink -PartnerResourceGroupName $primaryResourceGroupName `
    -PartnerServerName $primaryServerName

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

Bersihkan penyebaran

Gunakan perintah berikut untuk menghapus grup sumber daya dan semua sumber daya yang terkait dengannya.

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

Penjelasan skrip

Skrip ini menggunakan perintah berikut. Setiap perintah dalam tabel ditautkan ke dokumentasi spesifik-perintah.

Perintah Catatan
New-AzResourceGroup Membuat grup sumber daya tempat semua sumber daya disimpan.
Baru-AzSqlServer Membuat server yang menghosting database dan kumpulan elastis.
New-AzSqlElasticPool Membuat kumpulan elastis.
Baru-AzSqlDatabase Membuat database di server.
Atur-AzSqlDatabase Memperbarui properti database atau memindahkan database ke dalam, keluar, atau di antara kumpulan elastis.
New-AzSqlDatabaseSecondary Membuat database sekunder untuk database yang sudah ada dan memulai replikasi data.
Dapatkan-AzSqlDatabase Mendapatkan satu atau beberapa database.
Set-AzSqlDatabaseSecondary Mengalihkan database sekunder menjadi utama untuk memulai failover.
Get-AzSqlDatabaseReplicationLink Mendapatkan tautan geo-replikasi antara Azure SQL Database dan grup sumber daya atau server SQL logis.
Remove-AzResourceGroup Menghapus grup sumber daya termasuk semua sumber daya berlapis.

Langkah berikutnya

Untuk informasi selengkapnya tentang Azure PowerShell, lihat Dokumentasi Microsoft Azure PowerShell.

Sampel skrip SQL Database PowerShell tambahan dapat ditemukan di skrip Azure SQL Database PowerShell.