中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Sqlserver 2014 主從兩節點用腳本完成alwayson搭建

發布時間:2020-08-07 14:50:57 來源:ITPUB博客 閱讀:405 作者:ywxj_001 欄目:關系型數據庫
環境:
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]

go

02:創建 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]

go

01:主節點數據庫備份
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


GO


02:還原數據庫日志備份
restore log [test] from disk=N'D:\dbbackup\test_log.bak'


with norecovery,stats=5

go

01:主節點創建AG
CREATE 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));


02:加入alwayson
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

如果只是建立mirror,不加入alwayson集群
就在02上還原了數據庫日志備份之后測試下兩邊的5022端口就可以:
02:
ALTER DATABASE test SET PARTNER = N'TCP://主機IP:5022'
01:
ALTER DATABASE test SET PARTNER = N'TCP://備機IP:5022'
ALTER DATABASE test SET SAFETY OFF
如果報錯的話:
可能是5022端口沒開放,
或者是對端的數據庫用戶里面沒有加入數據庫服務的啟動賬號。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

大连市| 尚志市| 桐城市| 德阳市| 洛隆县| 曲周县| 金昌市| 通海县| 衢州市| 卓尼县| 长海县| 宜州市| 宽甸| 嵊泗县| 横山县| 石台县| 广平县| 襄汾县| 天长市| 营山县| 平凉市| 徐州市| 巴彦淖尔市| 平定县| 正蓝旗| 葵青区| 盈江县| 江油市| 嘉善县| 洪洞县| 河源市| 天长市| 定结县| 寿宁县| 中江县| 济南市| 开鲁县| 临澧县| 天水市| 苏尼特左旗| 武汉市|