sqlserver复制延迟监控&复制错误检查

通过查询分发服务器来达到监控sqlserver复制的健康情况

第一步 获取分发服务器

SELECT 
	name as [Database name],
	CASE is_published 
		WHEN 0 THEN 'No' 
		ELSE 'Yes' 
		END AS [Is Published],
	CASE is_merge_published 
		WHEN 0 THEN 'No' 
		ELSE 'Yes' 
		END AS [Is Merge Published],
	CASE is_distributor 
		WHEN 0 THEN 'No' 
		ELSE 'Yes' 
		END AS [Is Distributor],
	CASE is_subscribed 
		WHEN 0 THEN 'No' 
		ELSE 'Yes' 
		END AS [Is Subscribed]
FROM sys.databases
WHERE database_id > 4

第二步 循环每一个分发库,执行下列操作,查询复制延迟情况

SELECT distinct a.publisher
,publication
,agent_name
,distdb
,[agent_type]
,[status]
,[lastrefresh]
,cur_latency
FROM [" + dbName + @"].[dbo].[MSreplication_monitordata] a WITH(NOLOCK)
WHERE publication_id IS NOT NULL  
AND cur_latency IS NOT NULL
AND [time_stamp]>DATEADD(MINUTE,-{0},GETDATE())
AND ([cur_latency]>=10
OR ([agent_type]  in(2,3)/*2 = 日志读取器代理,3 = 分发代理*/
AND [status] in(5,6)/*5= 正在重试,6= 失败*/));

第三步 检测分发错误消息

查询分发服务器,包含有扩展分发代理程序和合并代理程序失败信息的行

SELECT 
publisher.name as publisher, 
subscriber.name as subscriber, 
--MSda.publication, 
MSda.publisher_db, 	
--MSda.subscriber_db,
--MSre.error_code,
MSre.time,
MSre.error_text
FROM [{0}].[dbo].MSdistribution_history MSdh
INNER JOIN [{0}].[dbo].MSdistribution_agents MSda ON MSdh.agent_id = MSda.id
INNER JOIN [{0}].[dbo].MSrepl_errors MSre ON MSdh.error_id = MSre.id
INNER JOIN master.sys.servers publisher ON MSda.publisher_id = publisher.server_id
INNER JOIN master.sys.servers subscriber ON MSda.subscriber_id = subscriber.server_id
WHERE MSdh.error_id <> 0
and MSdh.time>DATEADD(MINUTE,-{1},GETDATE())
posted @ 2019-08-29 14:25  小泥巴2008  阅读(441)  评论(0编辑  收藏  举报