Sqlserver 映象切換操作並檢查映象同步情況
主節點上:
ALTER DATABASE [dbname] SET SAFETY FULL
ALTER DATABASE [dbname] SET PARTNER FAILOVER
切換完畢到從節點,新的主節點上:
ALTER DATABASE [dbname] SET SAFETY OFF
檢查映象同步情況:
SET NOCOUNT ON
DECLARE @db_name varchar(100)
DECLARE @dblist TABLE([db_name] varchar(100))
DECLARE @a TABLE(
database_name SYSNAME
,[role] INT
,mirroring_state INT
,witness_status INT
,log_generation_rate INT
,unsent_log INT
,send_rate INT
,unrestored_log INT
,recovery_rate INT
,transaction_delay INT
,transactions_per_sec INT
,average_delay INT
,time_recorded DATETIME
,time_behind DATETIME
,local_time DATETIME
)
--匯出映象資料庫名列表
insert into @dblist
select a.name
from sys.sysdatabases a (nolock)
join sys.database_mirroring b (nolock) on a.dbid=b.database_id
where mirroring_guid is not null
EXEC msdb.dbo.sp_dbmmonitorupdate
EXEC msdb.dbo.sp_dbmmonitorupdate --若server是初次執行這個sp,可能不會產生資料,所以需要再執行一次
--每個映象資料庫的相關記錄透過迴圈全部匯入表變數
while exists(select * from @dblist)
begin
set @db_name=(select top 1 [db_name] from @dblist)
delete @dblist where [db_name]=@db_name
INSERT INTO @a
EXEC msdb.dbo.sp_dbmmonitorresults @db_name
,0 --返回行數:-1行;-前條;-最近兩小時行數;
,0 --0表示不呼叫sp_dbmmonitorupdate,表示呼叫
end
SELECT database_name
,CASE [role] WHEN 1 THEN '主體資料庫'
WHEN 2 THEN '映象資料庫'
ELSE NULL END AS 'db_role'
,CASE mirroring_state WHEN 0 THEN '已掛起'
WHEN 1 THEN '已斷開'
WHEN 2 THEN '正在同步'
WHEN 3 THEN '掛起故障轉移'
WHEN 4 THEN '已同步'
ELSE NULL END AS mirroring_state
,CASE witness_status WHEN 0 THEN '未知'
WHEN 1 THEN '已連線'
WHEN 2 THEN '已斷開'
ELSE NULL END AS witness_status
,unsent_log AS 'unsent_log[kb]'
,unrestored_log AS 'unrestored_log[kb]'
,send_rate AS 'send_rate[kb/s]'
,recovery_rate AS 'recovery_rate[kb/s]'
,transaction_delay AS 'transaction_delay/ms'
,transactions_per_sec
,average_delay
,time_recorded
,time_behind
,local_time
FROM @a
ALTER DATABASE [dbname] SET SAFETY FULL
ALTER DATABASE [dbname] SET PARTNER FAILOVER
切換完畢到從節點,新的主節點上:
ALTER DATABASE [dbname] SET SAFETY OFF
檢查映象同步情況:
SET NOCOUNT ON
DECLARE @db_name varchar(100)
DECLARE @dblist TABLE([db_name] varchar(100))
DECLARE @a TABLE(
database_name SYSNAME
,[role] INT
,mirroring_state INT
,witness_status INT
,log_generation_rate INT
,unsent_log INT
,send_rate INT
,unrestored_log INT
,recovery_rate INT
,transaction_delay INT
,transactions_per_sec INT
,average_delay INT
,time_recorded DATETIME
,time_behind DATETIME
,local_time DATETIME
)
--匯出映象資料庫名列表
insert into @dblist
select a.name
from sys.sysdatabases a (nolock)
join sys.database_mirroring b (nolock) on a.dbid=b.database_id
where mirroring_guid is not null
EXEC msdb.dbo.sp_dbmmonitorupdate
EXEC msdb.dbo.sp_dbmmonitorupdate --若server是初次執行這個sp,可能不會產生資料,所以需要再執行一次
--每個映象資料庫的相關記錄透過迴圈全部匯入表變數
while exists(select * from @dblist)
begin
set @db_name=(select top 1 [db_name] from @dblist)
delete @dblist where [db_name]=@db_name
INSERT INTO @a
EXEC msdb.dbo.sp_dbmmonitorresults @db_name
,0 --返回行數:-1行;-前條;-最近兩小時行數;
,0 --0表示不呼叫sp_dbmmonitorupdate,表示呼叫
end
SELECT database_name
,CASE [role] WHEN 1 THEN '主體資料庫'
WHEN 2 THEN '映象資料庫'
ELSE NULL END AS 'db_role'
,CASE mirroring_state WHEN 0 THEN '已掛起'
WHEN 1 THEN '已斷開'
WHEN 2 THEN '正在同步'
WHEN 3 THEN '掛起故障轉移'
WHEN 4 THEN '已同步'
ELSE NULL END AS mirroring_state
,CASE witness_status WHEN 0 THEN '未知'
WHEN 1 THEN '已連線'
WHEN 2 THEN '已斷開'
ELSE NULL END AS witness_status
,unsent_log AS 'unsent_log[kb]'
,unrestored_log AS 'unrestored_log[kb]'
,send_rate AS 'send_rate[kb/s]'
,recovery_rate AS 'recovery_rate[kb/s]'
,transaction_delay AS 'transaction_delay/ms'
,transactions_per_sec
,average_delay
,time_recorded
,time_behind
,local_time
FROM @a
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22996654/viewspace-2150062/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQLServer2012映象主庫掛掉如何切換到映象備庫SQLServer
- Sqlserver查詢alwayson同步情況指令碼(2)SQLServer指令碼
- 切換阿里巴巴開源映象站映象——Kali映象阿里
- CentOS7 切換阿里映象源並安裝 dockerCentOS阿里Docker
- docker hub切換國內映象Docker
- Ubuntu切換為阿里映象源Ubuntu阿里
- mac下 brew 切換阿里映象Mac阿里
- nrm -- 快速切換下載映象
- 檢視SQLServer的LCK資源等待情況SQLServer
- Asp-Net-Core開發筆記:使用alpine映象並加入健康檢查筆記
- SQLServer映象報錯Connection handshake failedSQLServerAI
- npm,registry,映象源,npm切換源,yarn,cnpm,taobao,nrsNPMYarn
- Linux檢查Docker映象,容器的磁碟空間LinuxDocker
- 2、docker映象操作Docker
- 標記本地映象並上傳到映象庫中
- 樹莓派製作映象並縮小映象尺寸樹莓派
- sqlserver關於mirror映象的總結SQLServer
- 檢查備份情況的指令碼指令碼
- Docker映象的基本操作Docker
- pip更換映象源
- 下載docker映象並推送至阿里雲映象服務Docker阿里
- 切換代理IP時如何檢查IP?
- 線上直播原始碼,npm設定映象的方法 可切換原始碼NPM
- SQLServer映象雲市場售賣釋出SQLServer
- Jetson Nano 更換映象源NaN
- Docker配置阿里雲映象加速器以及映象的常用操作命令Docker阿里
- Docker_03 映象-基礎操作Docker
- CentOS7操作-配置映象源CentOS
- Scrcpy v1.9可以在螢幕關閉的情況下映象螢幕
- npm 的國內映象更換NPM
- npm換源成淘寶映象NPM
- pip 配置與更換映象源
- Gradle更換阿里雲映象源Gradle阿里
- node.js更換映象源Node.js
- SQLServer搭建域控下映象報錯Connection handshake failedSQLServerAI
- Docker 映象及容器操作命令介紹Docker
- 資料庫映象 (SQL Server)操作模式資料庫SQLServer模式
- pip 換成國內阿里映象源阿里
- 交換機M:N埠映象配置