Add Secondary Replica to Availability group Part 3

Adding Secondary Replica

:Connect SecondaryReplica 
use [master] GO 
GRANT CONNECT ON ENDPOINT::[Mirroring_Endpoint] TO [DBPROXadministrator] 
GO 
:Connect PrimaryReplica 
  IF (SELECT state FROM sys.endpoints WHERE name = N'Mirroring_Endpoint') <> 0 
    BEGIN 
      ALTER ENDPOINT [Mirroring_Endpoint] STATE = STARTED 
    END 
    GO 
    use [master] 
    GO 
    GRANT CONNECT ON ENDPOINT::[Mirroring_Endpoint] TO [DBPROXadministrator] 
    GO 
:Connect PrimaryReplica 
  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 SecondaryReplica 
  USE [master] 
  GO 
  ALTER AVAILABILITY GROUP [AGName] ADD REPLICA ON N'PrimaryReplica' 
    WITH (ENDPOINT_URL = N'TCP://Primary.domain.com:5022', FAILOVER_MODE = MANUAL, 
        AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT, BACKUP_PRIORITY = 50, 
        SECONDARY_ROLE(ALLOW_CONNECTIONS = NO)); 
  GO 
:Connect PrimaryReplica 
  ALTER AVAILABILITY GROUP [AG Name] JOIN; 
  GO

 


					

Leave a Reply

Your email address will not be published. Required fields are marked *