在 Azure 中,如何在发生 SQL 服务器故障转移时配置警报或通知?

In Azure, how can you configure an alert or notification when a SQL Server failover happened?

在 Azure 中,如果您使用故障转移组设置 SQL 服务器并将故障转移策略设置为自动,那么如何在发生 SQL 服务器故障转移时配置警报或通知?如果无法在 Monitor 中设置,是否可以在其他地方编写脚本?

A​​zure SQL 数据库仅支持这些警报指标:

当 SQL 服务器发生故障时,我们无法使用警报。您可以从此文档中获取:Create alerts for Azure SQL Database and Data Warehouse using Azure portal.

希望对您有所帮助。

找到一种在 Azure 中使用自动化帐户 > Runbook > 使用 Powershell 编写脚本的方法。像这样的简单脚本应该可以做到。只需要找出 运行 作为帐户并按计划或警报触发它。

function sendEmailAlert
{
    # Send email
}


function checkFailover
{
    $list = Get-AzSqlDatabaseFailoverGroup -ResourceGroupName "my-resourceGroup" -server "my-sql-server"

    if ( $list.ReplicationRole -ne 'Primary')
    { 
        sendEmailAlert
    }
}

checkFailover

感谢 CKelly - 为我提供了一个良好的开端,让我开始了解 Azure 中应该是标准的东西。我创建了一个 Azure 自动化帐户,添加了 Az.Account、Az.Automation 和 Az.Sql 模块,然后向您的代码中添加了更多内容。在 Azure 中,我创建了一个 SendGrid 帐户。

#use the Azure Account Automation details to login to Azure
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

#create email alert
function sendEmailAlert
{
    # Send email
   $From = "<email from>"
$To = "<email of stakeholders to receive this message>"
$SMTPServer = "smtp.sendgrid.net"
$SMTPPort = "587"
$Username = "<sendgrid username>"
$Password = "<sendgridpassword>"
$subject = "<email subject>"
$body = "<text to go in email body>"
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$smtp.Send($From, $To, $subject, $body)
}

#create failover check and send if the primary server has changed
function checkFailover
{
    $list = Get-AzSqlDatabaseFailoverGroup -ResourceGroupName "<the resourcegroup>" -server "<SQl Databse server>"

    if ( $list.ReplicationRole -ne 'Primary')
    { 
        sendEmailAlert
    }
}

checkFailover

这个过程可能会对其他人有所帮助。