Sqlserver 2014 主從兩節點用指令碼完成alwayson搭建
Sqlserver兩節點已經完成故障轉移叢集搭建:
節點01:以下簡稱01,為主節點
節點02:以下簡稱02,為輔助節點
資料庫服務的啟動賬號必須建立在資料庫使用者裡面。
01:建立 EndPoint 並賦權
create endpoint [Hadr_endpoint]
authorization sa
state=started
as tcp(listener_port=5022,listener_ip=all)
for database_mirroring(role=all,authentication=windows, ENCRYPTION = REQUIRED ALGORITHM RC4)
go
GRANT CONNECT ON ENDPOINT:: [Hadr_endpoint] TO [資料庫服務啟動賬號,這裡設定為service01]
go02:建立 EndPoint並賦權
create endpoint [Hadr_endpoint]
authorization sa
state=started
as tcp(listener_port=5022,listener_ip=all)
for database_mirroring(role=all,authentication=windows, ENCRYPTION = REQUIRED ALGORITHM RC4)
go
GRANT CONNECT ON ENDPOINT:: [Hadr_endpoint] TO [service01]
go01:主節點資料庫備份
use master
GO
BACKUP DATABASE [test] TO DISK = N'D:\dbbackup\test_full.bak' WITH COMPRESSION, NOFORMAT, NOINIT
GO
02:還原資料庫備份use master
go
restore database [test] from disk=N'D:\dbbackup\test_full.bak'
with
move 'test' to 'e:\SQL_Data\test.mdf'
,move 'test_log' to 'e:\SQL_Data\test_log.ldf'
,norecovery,replace,stats=5
go
01:主節點資料庫日誌備份
BACKUP log [test] TO
DISK = N'D:\dbbackup\test_log.bak' WITH COMPRESSION, NOFORMAT, NOINIT
restore log [test] from disk=N'D:\dbbackup\test_log.bak'
with norecovery,stats=5
go
01:主節點建立AGCREATE AVAILABILITY GROUP [test_AlwaysOn]
WITH (AUTOMATED_BACKUP_PREFERENCE = SECONDARY)
FOR DATABASE [test]
REPLICA ON
N'test01' WITH (
ENDPOINT_URL = N'TCP://test01.pousheng.com:5022'
,FAILOVER_MODE = MANUAL
,AVAILABILITY_MODE = SYNCHRONOUS_COMMIT
,BACKUP_PRIORITY = 50
,SECONDARY_ROLE(ALLOW_CONNECTIONS = ALL))
,N'test02' WITH (
ENDPOINT_URL = N'TCP://test02.pousheng.com:5022'
,FAILOVER_MODE = MANUAL
,AVAILABILITY_MODE = SYNCHRONOUS_COMMIT
,BACKUP_PRIORITY = 50
,SECONDARY_ROLE(ALLOW_CONNECTIONS = ALL));
ALTER AVAILABILITY GROUP test_AlwaysOn JOIN;
02:設定副本
ALTER DATABASE [test] SET HADR AVAILABILITY GROUP = [test_AlwaysOn];
01:修改同步模式為非同步
ALTER AVAILABILITY GROUP [test_AlwaysOn] MODIFY REPLICA ON N'test02' WITH (AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT)
go
檢視同步狀態即可。
OK。
新加資料庫:
01:可用性組新增資料庫
ALTER AVAILABILITY GROUP [test_AlwaysOn] ADD DATABASE [xxx];
02:設定副本
ALTER DATABASE [xxx] SET HADR AVAILABILITY GROUP = [test_AlwaysOn];
新加節點:
02:加節點
ALTER AVAILABILITY GROUP [test_AlwaysOn]
ADD REPLICA ON 'test01'
WITH (
ENDPOINT_URL = 'TCP://test01.pousheng.com:5022',
AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,
FAILOVER_MODE = MANUAL,
BACKUP_PRIORITY = 50,
SECONDARY_ROLE(ALLOW_CONNECTIONS = ALL)
);01:連節點
ALTER AVAILABILITY GROUP test_AlwaysOn JOIN;
查詢資料庫備份執行指令碼:
select 'backup database ['+name+'] to disk=N''F:\backup\'+name+'.bak'' with STATS=5' as cmd
from sys.databases with(nolock)
where database_id>=4
order by name
查詢資料庫還原執行指令碼:
with tb as (
select 'RESTORE DATABASE '+db_name(database_id)+' FROM Disk=N''e:\backup\'+db_name(database_id)+'.bak'' WITH RECOVERY, ' as db,'MOVE '''+name+''' TO ''f:\sqldata\'+SUBSTRING(physical_name,len(physical_name)-CHARINDEX('\',REVERSE(physical_name))+2,CHARINDEX('\',REVERSE(physical_name))-1)+'''' as fl From sys.master_files with(nolock)
)
SELECT db+[values] FROM( SELECT DISTINCT db FROM tb)A
OUTER APPLY( SELECT [values]= STUFF(REPLACE(REPLACE(( SELECT fl FROM tb N WHERE db = A.db FOR XML AUTO ), '', ''), 1, 1, ''))N order by db
go
就在02上還原了資料庫日誌備份之後測試下兩邊的5022埠就可以:
02:
ALTER DATABASE test SET PARTNER = N'TCP://主機IP:5022'
01:
ALTER DATABASE test SET PARTNER = N'TCP://備機IP:5022'
如果報錯的話:
可能是5022埠沒開放,
或者是對端的資料庫使用者裡面沒有加入資料庫服務的啟動賬號。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22996654/viewspace-2149821/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Sqlserver 2014 alwayson架構主節點執行alter table導致從節點的阻塞問題SQLServer架構
- Sqlserver 2014 alwayson故障轉移群集節點被踢出群集SQLServer
- Sqlserver 2014 alwayson 架構新增輔助節點上的資料庫SQLServer架構資料庫
- Sqlserver查詢alwayson同步情況指令碼(2)SQLServer指令碼
- Sqlserver查詢alwayson同步情況指令碼(1)SQLServer指令碼
- SQLServer2012對現有AlwaysOn群集新加節點SQLServer
- 快速搭建主從的指令碼和問題排查指令碼
- SQLServer2012搭建AlwaysOn報錯SQLServer
- mongodb主從仲裁節點配置MongoDB
- 用指令碼完成mysql工作指令碼MySql
- 對SQL Server 2014 alwayson 架構的資料庫在主節點加新的datafile檔案SQLServer架構資料庫
- SQL Server AlwaysOn配置兩個節點加共享資料夾仲裁見證SQLServer
- MYSQL5.6.40原始碼安裝 主從搭建 主主搭建MySql原始碼
- 實踐 - 搭建Redis一主兩從三哨兵Redis
- SQLServer 常用指令碼SQLServer指令碼
- 搭建PowerDNS+LAP+NFS+MySQL主從半節點同步實現LAMP架構DNSNFSMySqlLAMP架構
- 丐版sqlserver AlwaysOn叢集SQLServer
- 刪除alwayson主節點一個表的統計資訊導致一個輔助節點的同步延遲
- sqlserver 巡檢指令碼SQLServer指令碼
- SQLServer備份指令碼SQLServer指令碼
- SQLServer巡檢指令碼SQLServer指令碼
- python監控mysql主從指令碼PythonMySql指令碼
- Sqlserver重啟alwayson監聽埠SQLServer
- 主庫RAC,備庫單節點ASM的dataguard搭建ASM
- SQLServer2012搭建AlwaysOn報錯:Certificate not found. State 89SQLServer
- Shell指令碼應用兩個例子指令碼
- MySQL 5.7 用mysqldump搭建gtid主從MySql
- MySQL 5.7 用xtrabackup搭建gtid主從MySql
- MySQL 5.5 Slave節點備份指令碼(mysqldump)MySql指令碼
- Redis主從搭建Redis
- mysql主從搭建MySql
- SQL Server AlwaysOn搭建SQLServer
- Shell指令碼監控MySQL主從狀態指令碼MySql
- 5.6 MySql主從自動切換指令碼MySql指令碼
- Postgrsql 從節點當機,主節點執行DML語句出現等待情況SQL
- SQLServer 2012 AG主節點無法截斷事務日誌SQLServer
- 分享兩個實用的shell指令碼指令碼
- [20141119]使用指令碼完成sql_profile工作指令碼SQL