代码改变世界

SQL Server如何查看AlwaysOn的Failover记录信息

2025-01-10 11:11  潇湘隐者  阅读(105)  评论(1)    收藏  举报

SQL Server AlwaysOn发生了故障转移(Failover)后,我们如何查看AlwaysOn在什么时间点发生故障转移呢?下面简单的总结了一些资料。

PowerShell脚本查看

Windows事件日志系统中的事件ID=1641,表示群集角色已从一个节点移动到另一个节点。所以我们可以使用PowerShell脚本获取 /过滤这类事件ID。

Get-WinEvent -filterHashTable @{logname ='Microsoft-Windows-FailoverClustering/Operational'; id=1641}| sort TimeCreated | ft -AutoSize

--下面是案例

PS C:\Windows\system32> Get-WinEvent -filterHashTable @{logname ='Microsoft-Windows-FailoverClustering/Operational'; id=1641}| sort TimeCreated | ft -AutoSize


   ProviderName: Microsoft-Windows-FailoverClustering

TimeCreated              Id LevelDisplayName Message
-----------              -- ---------------- -------
12/27/2024 2:06:36 PM  1641 Information      Clustered role '*******UAT' is moving from cluster node '***dbu04' to cluster node '***dbu05'.
12/27/2024 2:08:07 PM  1641 Information      Clustered role '*******UAT' is moving from cluster node '***dbu05' to cluster node '***dbu04'.
12/30/2024 9:20:35 AM  1641 Information      Clustered role '*******UAT' is moving from cluster node '***dbu04' to cluster node '***dbu06'.
12/30/2024 9:21:35 AM  1641 Information      Clustered role '*******UAT' is moving from cluster node '***dbu06' to cluster node '***dbu04'.
12/30/2024 9:45:35 AM  1641 Information      Clustered role '*******UAT' is moving from cluster node '***dbu04' to cluster node '***dbu06'.
12/30/2024 10:08:55 AM 1641 Information      Clustered role '*******UAT' is moving from cluster node '***dbu06' to cluster node '***dbu04'.
12/30/2024 10:11:21 AM 1641 Information      Clustered role '*******UAT' is moving from cluster node '***dbu04' to cluster node '***dbu06'.


PS C:\Windows\system32>

SQL脚本查询日志

WITH CTE_AG_XEL AS (
SELECT object_name
     , CONVERT(XML, event_data) AS data
FROM sys.fn_xe_file_target_read_file('AlwaysOn*.xel'nullnullnull)
WHERE object_name = 'error_reported'
),
MSG_DTL AS
(
SELECT data.value('(/event/@timestamp)[1]','datetime'AS [event_timestamp],
       data.value('(/event/data[@name=''error_number''])[1]','int'AS [error_number],
       data.value('(/event/data[@name=''message''])[1]','varchar(max)'AS [message]
FROM CTE_AG_XEL
WHERE data.value('(/event/data[@name=''error_number''])[1]','int') = 1480
)
SELECT DATEADD(HOURDATEDIFF(HOURGETUTCDATE(), GETDATE()), event_timestamp) AS event_timestamp
           , [error_number]
           , [message]
FROM MSG_DTL
ORDER BY event_timestamp DESC;