---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, Nounloa D, 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, NOU Nload, 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 =--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) 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 =--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) 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
|