Book Image

Professional SQL Server High Availability and Disaster Recovery

By : Ahmad Osama
Book Image

Professional SQL Server High Availability and Disaster Recovery

By: Ahmad Osama

Overview of this book

Professional SQL Server High Availability and Disaster Recovery explains the high availability and disaster recovery technologies available in SQL Server: Replication, AlwaysOn, and Log Shipping. You’ll learn what they are, how to monitor them, and how to troubleshoot any related problems. You will be introduced to the availability groups of AlwaysOn and learn how to configure them to extend your database mirroring. Through this book, you will be able to explore the technical implementations of high availability and disaster recovery technologies that you can use when you create a highly available infrastructure, including hybrid topologies. Note that this course does not cover SQL Server Failover Cluster Installation with shared storage. By the end of the book, you’ll be equipped with all that you need to know to develop robust and high performance infrastructure.
Table of Contents (9 chapters)
Professional SQL Server High Availability and Disaster Recovery
Preface

Lesson 5: Managing AlwaysOn Availability Groups


Activity 5: Manual Failover

Solution:

  1. Execute the following query to failover from DPLPR to DPLHA:

    :Connect DPLHA
    ALTER AVAILABILITY GROUP [DPLAG] FAILOVER;
  2. Execute the following query on DPLHA to verify that the failover is complete:

    SELECT 
      ag.name AS AvailabilityGroup,
      ar.replica_server_name AS ReplicaName,
      ars.role_desc AS Role,
      ars.operational_state_desc
    FROM 
    sys.availability_groups ag join sys.availability_replicas ar 
    on ag.group_id=ar.group_id
    join sys.dm_hadr_availability_replica_states ars 
    on ar.replica_id=ars.replica_id

    The Role column for the DPLHA replica should state Primary.

  3. To fall back to DPLPR, execute the following query:

    :Connect DPLPR
    ALTER AVAILABILITY GROUP [DPLAG] FAILOVER;

    To verify the failover, execute the query from step 2 once again. The Role column for the DPLPR replica should state Primary.

Activity 6: Adding a New Database to an Existing Availability Group

Solution:

  1. Execute the following query at DPLPR to add the Customer database to the DPLAG availability group:

    USE Master;
    ALTER AVAILABILITY GROUP DPLAG ADD DATABASE [Customer];  
    GO
  2. Execute the following query to take a full backup of the Customer database at DPLPR:

    BACKUP DATABASE Customer TO DISK='C:\Code\Customer_FullBackup.bak' WITH INIT, STATS=10, COMPRESSION
  3. Execute the following query at DPLHA and DPLDR to restore the full backup of the Customer database:

    USE [master]
    RESTORE DATABASE [Customer] FROM  DISK = N'C:\Code\Customer_Fullbackup.bak' WITH  FILE = 1,  NOUNLOAD,  STATS = 5, NORECOVERY
  4. Execute the following query to take a log backup of the Customer database at DPLPR:

    BACKUP LOG Customer TO DISK='C:\Code\Customer_Logbackup.trn' WITH COMPRESSION, INIT, STATS=10
  5. Execute the following query to restore the log backup at DPLHA and DPLDR:

    RESTORE LOG Customer FROM DISK='C:\Code\Customer_LogBackup.trn' WITH NORECOVERY, STATS=10
  6. Execute the following query at DPLHA and DPLDR to join the Customer database to the DPLAG availability group:

    USE Master;
    ALTER DATABASE Customer SET HADR AVAILABILITY GROUP = DPLAG;

You can use the AlwaysOn dashboard to verify that the database was successfully added to the availability group.

You can also use SSMS Object Explorer to add the database to the availability group. However, you'd have to perform the backup and restore manually.