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故障轉移群集節點被踢出群集SQLServer
- Sqlserver查詢alwayson同步情況指令碼(2)SQLServer指令碼
- SQLServer2012對現有AlwaysOn群集新加節點SQLServer
- SQLServer2012搭建AlwaysOn報錯SQLServer
- 對SQL Server 2014 alwayson 架構的資料庫在主節點加新的datafile檔案SQLServer架構資料庫
- mongodb主從仲裁節點配置MongoDB
- SQLServer 常用指令碼SQLServer指令碼
- SQLServer2012搭建AlwaysOn報錯:Certificate not found. State 89SQLServer
- MYSQL5.6.40原始碼安裝 主從搭建 主主搭建MySql原始碼
- 丐版sqlserver AlwaysOn叢集SQLServer
- 實踐 - 搭建Redis一主兩從三哨兵Redis
- SQLServer 2012 AG主節點無法截斷事務日誌SQLServer
- windows2012+sqlserver2017 always on 加域雙節點搭建總結WindowsSQLServer
- 搭建PowerDNS+LAP+NFS+MySQL主從半節點同步實現LAMP架構DNSNFSMySqlLAMP架構
- Shell指令碼應用兩個例子指令碼
- 從節點崩了,還怎麼「主從讀寫分離」?
- Shell指令碼監控MySQL主從狀態指令碼MySql
- 分享兩個實用的shell指令碼指令碼
- windows2012+sqlserver2012 always on 加域雙節點搭建總結(圖文)WindowsSQLServer
- mysql主從搭建MySql
- Redis主從搭建Redis
- Postgrsql 從節點當機,主節點執行DML語句出現等待情況SQL
- SqlServer 主從複製錯誤分析--20598SQLServer
- POSTGRESQL10.3原始碼安裝主從搭建SQL原始碼
- POSTGRESQL10.8原始碼安裝主從搭建SQL原始碼
- MYSQL5.7.22 原始碼安裝 主從搭建MySql原始碼
- 同步線上伺服器程式碼到web節點指令碼伺服器Web指令碼
- ogg 12.3 for sqlserver 2016/2014 CDC模式配置SQLServer模式
- consul 多節點/單節點叢集搭建
- [SQLServer]NetCore中將SQLServer資料庫備份為Sql指令碼SQLServerNetCore資料庫指令碼
- centos 搭建redis主從CentOSRedis
- MYSQL主從搭建5.6.38MySql
- redis cluster 故障後,主從位於不同節點的修復。Redis
- FILE+POS 方式 GreatSQL 主從複製架構給主節點磁碟擴容SQL架構
- Docker 方式 MySQL 主從搭建DockerMySql
- Mysql主從搭建(docker compose)MySqlDocker
- redis-cluster主從搭建Redis
- mysql主從複製搭建MySql
- MongoDB叢集搭建(包括隱藏節點,仲裁節點)MongoDB