16.2 添加节点和数据库
16.2.1 添加节点
以上操作,脚本为:
--- YOU MUST EXECUTE THE FOLLOWING SCRIPT IN SQLCMD MODE. :Connect SQLSVR3 USE [master] GO CREATE LOGIN [LOCALDOMAIN\SQLHAUser] FROM WINDOWS GO :Connect SQLSVR1 use [master] GO GRANT CONNECT ON ENDPOINT::[Hadr_endpoint] TO [LOCALDOMAIN\SQLHAUser] GO use [master] GO GRANT CONNECT ON ENDPOINT::[Hadr_endpoint] TO [LOCALDOMAIN\administrator] GO :Connect SQLSVR2 use [master] GO GRANT CONNECT ON ENDPOINT::[Hadr_endpoint] TO [LOCALDOMAIN\SQLHAUser] GO use [master] GO GRANT CONNECT ON ENDPOINT::[Hadr_endpoint] TO [LOCALDOMAIN\Administrator] GO :Connect SQLSVR3 USE [master] GO CREATE ENDPOINT [Hadr_endpoint] AS TCP (LISTENER_PORT = 5022) FOR DATA_MIRRORING (ROLE = ALL, ENCRYPTION = REQUIRED ALGORITHM AES) GO IF (SELECT state FROM sys.endpoints WHERE name = N'Hadr_endpoint') <> 0 BEGIN ALTER ENDPOINT [Hadr_endpoint] STATE = STARTED END GO use [master] GO GRANT CONNECT ON ENDPOINT::[Hadr_endpoint] TO [LOCALDOMAIN\SQLHAUser] GO :Connect SQLSVR3 IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='AlwaysOn_health') BEGIN ALTER EVENT SESSION [AlwaysOn_health] ON SERVER WITH (STARTUP_STATE=ON); END IF NOT EXISTS(SELECT * FROM sys.dm_xe_sessions WHERE name='AlwaysOn_health') BEGIN ALTER EVENT SESSION [AlwaysOn_health] ON SERVER STATE=START; END GO :Connect SQLSVR1 USE [master] GO ALTER AVAILABILITY GROUP [HAGroup01] ADD REPLICA ON N'SQLSVR3' WITH (ENDPOINT_URL = N'TCP://SQLSVR3.LocalDomain.Local:5022', FAILOVER_MODE = MANUAL, AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT, BACKUP_PRIORITY = 50, SECONDARY_ROLE(ALLOW_CONNECTIONS = NO)); GO :Connect SQLSVR3 ALTER AVAILABILITY GROUP [HAGroup01] JOIN; GO :Connect SQLSVR1 BACKUP DATABASE [SQLDB01] TO DISK = N'\\SQLSVR1\HAGroup\SQLDB01.bak' WITH COPY_ONLY, FORMAT, INIT, SKIP, REWIND, NOUNLOAD, COMPRESSION, STATS = 5 GO :Connect SQLSVR3 RESTORE DATABASE [SQLDB01] FROM DISK = N'\\SQLSVR1\HAGroup\SQLDB01.bak' WITH NORECOVERY, NOUNLOAD, STATS = 5 GO :Connect SQLSVR1 BACKUP LOG [SQLDB01] TO DISK = N'\\SQLSVR1\HAGroup\SQLDB01_20150613040532.trn' WITH NOFORMAT, NOINIT, NOSKIP, REWIND, NOUNLOAD, COMPRESSION, STATS = 5 GO :Connect SQLSVR3 RESTORE LOG [SQLDB01] FROM DISK = N'\\SQLSVR1\HAGroup\SQLDB01_20150613040532.trn' WITH NORECOVERY, NOUNLOAD, STATS = 5 GO :Connect SQLSVR3 -- Wait for the replica to start communicating begin try declare @conn bit declare @count int declare @replica_id uniqueidentifier declare @group_id uniqueidentifier set @conn = 0 set @count = 30 -- wait for 5 minutes if (serverproperty('IsHadrEnabled') = 1) and (isnull((select member_state from master.sys.dm_hadr_cluster_members where upper(member_name COLLATE Latin1_General_CI_AS) = upper(cast(serverproperty('ComputerNamePhysicalNetBIOS') as nvarchar(256)) COLLATE Latin1_General_CI_AS)), 0) <> 0) and (isnull((select state from master.sys.database_mirroring_endpoints), 1) = 0) begin select @group_id = ags.group_id from master.sys.availability_groups as ags where name = N'HAGroup01' select @replica_id = replicas.replica_id from master.sys.availability_replicas as replicas where upper(replicas.replica_server_name COLLATE Latin1_General_CI_AS) = upper(@@SERVERNAME COLLATE Latin1_General_CI_AS) and group_id = @group_id while @conn <> 1 and @count > 0 begin set @conn = isnull((select connected_state from master.sys.dm_hadr_availability_replica_states as states where states.replica_id = @replica_id), 1) if @conn = 1 begin -- exit loop when the replica is connected, or if the query cannot find the replica status break end waitfor delay '00:00:10' set @count = @count - 1 end end end try begin catch -- If the wait loop fails, do not stop execution of the alter database statement end catch ALTER DATABASE [SQLDB01] SET HADR AVAILABILITY GROUP = [HAGroup01]; GO GO |
16.2.2 添加数据库
以上操作,脚本为:
--- YOU MUST EXECUTE THE FOLLOWING SCRIPT IN SQLCMD MODE. :Connect SQLSVR1 USE [master] GO ALTER AVAILABILITY GROUP [HAGroup01] ADD DATABASE [SQLDB02]; GO :Connect SQLSVR1 BACKUP DATABASE [SQLDB02] TO DISK = N'\\SQLSVR1\HAGroup\SQLDB02.bak' WITH COPY_ONLY, FORMAT, INIT, SKIP, REWIND, NOUNLOAD, COMPRESSION, STATS = 5 GO :Connect SQLSVR2 RESTORE DATABASE [SQLDB02] FROM DISK = N'\\SQLSVR1\HAGroup\SQLDB02.bak' WITH NORECOVERY, NOUNLOAD, STATS = 5 GO :Connect SQLSVR3 RESTORE DATABASE [SQLDB02] FROM DISK = N'\\SQLSVR1\HAGroup\SQLDB02.bak' WITH NORECOVERY, NOUNLOAD, STATS = 5 GO :Connect SQLSVR1 BACKUP LOG [SQLDB02] TO DISK = N'\\SQLSVR1\HAGroup\SQLDB02_20150613041128.trn' WITH NOFORMAT, NOINIT, NOSKIP, REWIND, NOUNLOAD, COMPRESSION, STATS = 5 GO :Connect SQLSVR2 RESTORE LOG [SQLDB02] FROM DISK = N'\\SQLSVR1\HAGroup\SQLDB02_20150613041128.trn' WITH NORECOVERY, NOUNLOAD, STATS = 5 GO :Connect SQLSVR2 -- Wait for the replica to start communicating begin try declare @conn bit declare @count int declare @replica_id uniqueidentifier declare @group_id uniqueidentifier set @conn = 0 set @count = 30 -- wait for 5 minutes if (serverproperty('IsHadrEnabled') = 1) and (isnull((select member_state from master.sys.dm_hadr_cluster_members where upper(member_name COLLATE Latin1_General_CI_AS) = upper(cast(serverproperty('ComputerNamePhysicalNetBIOS') as nvarchar(256)) COLLATE Latin1_General_CI_AS)), 0) <> 0) and (isnull((select state from master.sys.database_mirroring_endpoints), 1) = 0) begin select @group_id = ags.group_id from master.sys.availability_groups as ags where name = N'HAGroup01' select @replica_id = replicas.replica_id from master.sys.availability_replicas as replicas where upper(replicas.replica_server_name COLLATE Latin1_General_CI_AS) = upper(@@SERVERNAME COLLATE Latin1_General_CI_AS) and group_id = @group_id while @conn <> 1 and @count > 0 begin set @conn = isnull((select connected_state from master.sys.dm_hadr_availability_replica_states as states where states.replica_id = @replica_id), 1) if @conn = 1 begin -- exit loop when the replica is connected, or if the query cannot find the replica status break end waitfor delay '00:00:10' set @count = @count - 1 end end end try begin catch -- If the wait loop fails, do not stop execution of the alter database statement end catch ALTER DATABASE [SQLDB02] SET HADR AVAILABILITY GROUP = [HAGroup01]; GO :Connect SQLSVR3 RESTORE LOG [SQLDB02] FROM DISK = N'\\SQLSVR1\HAGroup\SQLDB02_20150613041128.trn' WITH NORECOVERY, NOUNLOAD, STATS = 5 GO :Connect SQLSVR3 -- Wait for the replica to start communicating begin try declare @conn bit declare @count int declare @replica_id uniqueidentifier declare @group_id uniqueidentifier set @conn = 0 set @count = 30 -- wait for 5 minutes if (serverproperty('IsHadrEnabled') = 1) and (isnull((select member_state from master.sys.dm_hadr_cluster_members where upper(member_name COLLATE Latin1_General_CI_AS) = upper(cast(serverproperty('ComputerNamePhysicalNetBIOS') as nvarchar(256)) COLLATE Latin1_General_CI_AS)), 0) <> 0) and (isnull((select state from master.sys.database_mirroring_endpoints), 1) = 0) begin select @group_id = ags.group_id from master.sys.availability_groups as ags where name = N'HAGroup01' select @replica_id = replicas.replica_id from master.sys.availability_replicas as replicas where upper(replicas.replica_server_name COLLATE Latin1_General_CI_AS) = upper(@@SERVERNAME COLLATE Latin1_General_CI_AS) and group_id = @group_id while @conn <> 1 and @count > 0 begin set @conn = isnull((select connected_state from master.sys.dm_hadr_availability_replica_states as states where states.replica_id = @replica_id), 1) if @conn = 1 begin -- exit loop when the replica is connected, or if the query cannot find the replica status break end waitfor delay '00:00:10' set @count = @count - 1 end end end try begin catch -- If the wait loop fails, do not stop execution of the alter database statement end catch ALTER DATABASE [SQLDB02] SET HADR AVAILABILITY GROUP = [HAGroup01]; GO GO |
本文出自 “SQL Server 管理员指南” 博客,谢绝转载!