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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Sqlserver查詢alwayson同步情況指令碼(2)SQLServer指令碼
- Sqlserver查詢alwayson同步情況指令碼(1)SQLServer指令碼
- SQLServer2012映象主庫掛掉如何切換到映象備庫SQLServer
- 切換阿里巴巴開源映象站映象——Kali映象阿里
- CentOS7 切換阿里映象源並安裝 dockerCentOS阿里Docker
- Ubuntu切換為阿里映象源Ubuntu阿里
- mac下 brew 切換阿里映象Mac阿里
- docker hub切換國內映象Docker
- nrm -- 快速切換下載映象
- 檢視SQLServer的LCK資源等待情況SQLServer
- Asp-Net-Core開發筆記:使用alpine映象並加入健康檢查筆記
- npm,registry,映象源,npm切換源,yarn,cnpm,taobao,nrsNPMYarn
- AIX ROOTVG映象操作AI
- 2、docker映象操作Docker
- AIX同步映象VG中的新LV,手工映象LVAI
- 標記本地映象並上傳到映象庫中
- 樹莓派製作映象並縮小映象尺寸樹莓派
- 檢查備份情況的指令碼指令碼
- 線上直播原始碼,npm設定映象的方法 可切換原始碼NPM
- pip更換映象源
- sqlserver關於mirror映象的總結SQLServer
- Linux檢查Docker映象,容器的磁碟空間LinuxDocker
- Docker映象的基本操作Docker
- 切換歸檔模式後後臺程式的情況模式
- 下載docker映象並推送至阿里雲映象服務Docker阿里
- 檢查oracle的patch的升級情況Oracle
- SQLServer映象報錯Connection handshake failedSQLServerAI
- SQLServer映象雲市場售賣釋出SQLServer
- Jetson Nano 更換映象源NaN
- 切換代理IP時如何檢查IP?
- dataguard 手動切換,檢查指令碼指令碼
- docker映象、容器以及命令操作Docker
- SQLServer 檢查1SQLServer
- AIX rootvg 映象,以及更換硬碟AI硬碟
- npm 的國內映象更換NPM
- pip 配置與更換映象源
- node.js更換映象源Node.js
- Docker_03 映象-基礎操作Docker