DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • Using AWS Data Lake and S3 With SQL Server: A Detailed Guide With Research Paper Dataset Example
  • Securing AWS RDS SQL Server for Retail: Comprehensive Strategies and Implementation Guide
  • Fortifying Web Applications: A Guide To Preventing SQL Injection in AWS RDS SQL Server
  • Disaster Recovery and High Availability Solutions in SQL Server

Trending

  • Next-Gen Lie Detector: Stack Selection
  • Outsmarting Cyber Threats: How Large Language Models Can Revolutionize Email Security
  • Tenv v2.0: The Importance of Explicit Behavior for Version Manager
  • Operational Excellence Best Practices
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. High Availability and Disaster Recovery (HADR) in SQL Server on AWS

High Availability and Disaster Recovery (HADR) in SQL Server on AWS

We will guide you through the process of configuring HADR for SQL Server on AWS, providing practical code examples for setting up and recovering a SQL Server database.

By 
Vijay Panwar user avatar
Vijay Panwar
DZone Core CORE ·
Jul. 01, 24 · Tutorial
Like (1)
Save
Tweet
Share
3.4K Views

Join the DZone community and get the full member experience.

Join For Free

High Availability and Disaster Recovery (HADR) play a vital role in maintaining the integrity of data, reducing downtime, and safeguarding against data loss in enterprise database systems. AWS offers a range of HADR options for SQL Server, which leverage the powerful capabilities of SQL Server along with the scalability and flexibility of AWS. In this article, we will guide you through the process of configuring HADR for SQL Server on AWS, providing practical code examples for setting up and recovering a SQL Server database.

1. Setting up SQL Server on AWS

Launch an EC2 Instance With SQL Server

  1. Initiate the deployment of an EC2 instance: Utilize the AWS Management Console to commence the deployment of an EC2 instance by selecting the suitable SQL Server AMI (Amazon Machine Image). Opt for the instance type that aligns with your specific performance criteria.
  2. Set up security groups: Guarantee that the security groups permit the essential inbound and outbound traffic to facilitate seamless SQL Server communication.
Shell
 
aws ec2 run-instances \

    --image-id ami-0abcdef1234567890 \

    --instance-type t3.large \

    --key-name MyKeyPair \

    --security-group-ids sg-0123456789abcdef0 \

    --subnet-id subnet-6e7f829e


Please note that AWS provides a free tier and I am using a free tier here.

launch an instance

Install and Configure SQL Server

1. Connect to the EC2 instance: Use SSH to connect to your EC2 instance.

Shell
 
ssh -i MyKeyPair.pem ec2-user@ec2-12-34-56-78.compute-1.amazonaws.com


2. Install SQL Server: If not pre-installed, download and install SQL Server.

Shell
 
sudo yum install -y https://packages.microsoft.com/config/rhel/7/prod.repo

sudo yum install -y mssql-server


3. Configure SQL Server: Run the setup to configure the SQL Server.

Shell
 
sudo /opt/mssql/bin/mssql-conf setup


4. Verify installation: Ensure SQL Server is running.

Shell
 
systemctl status mssql-server


2. High Availability (Always On Availability Groups)

What Is High Availability?

SQL Server's High Availability (HA) feature guarantees the continuous accessibility and functionality of databases, even in the face of hardware or software failures. This is particularly crucial for critical applications where any downtime can significantly disrupt business operations. Always On Availability Groups (AG) in SQL Server provide a robust HA solution that offers enterprise-level protection and automated failover capabilities.

Utilizing Always On Availability Groups allows a group of databases to failover together, ensuring consistency across related databases. Each availability group comprises primary and secondary replicas. The primary replica handles all read-write operations, while secondary replicas can be configured to support read-only operations and backup jobs, thereby optimizing resource utilization.

In AWS, HA is further enhanced by deploying SQL Server instances across multiple Availability Zones (AZs). This multi-AZ deployment guarantees that even if an entire data center experiences an outage, the secondary replicas in other AZs can seamlessly take over with minimal downtime. To facilitate smooth failover, AWS Elastic Load Balancer (ELB) can be employed to redirect traffic to the new primary replica.

To implement HA, it is necessary to enable Always On Availability Groups in SQL Server, configure the required endpoints, and set up the replicas. Regular monitoring and maintenance of the AGs, combined with AWS's resilient infrastructure, ensure that your SQL Server databases achieve high availability, thereby minimizing the risk of downtime and data loss.

Step 1: Enable Always On Availability Groups

1. Open SQL Server Configuration Manager and enable Always On Availability Groups.

MS SQL
 
EXEC sp_configure 'show advanced options', 1;

RECONFIGURE;

EXEC sp_configure 'availability groups', 1;

RECONFIGURE;


2. Restart the SQL Server to apply the changes.

Shell
 
sudo systemctl restart mssql-server


Step 2: Create and Configure Availability Groups

Note: After launching EC2 and installing SQL Server, you are able to utilize any Integrated Development Environment (IDE) to operate the system and establish a database.

1. Create a database for the availability group.

MS SQL
 
CREATE DATABASE TestDB;


2. Back up the database and transaction log.

MS SQL
 
BACKUP DATABASE TestDB TO DISK = '/var/opt/mssql/data/TestDB.bak';

BACKUP LOG TestDB TO DISK = '/var/opt/mssql/data/TestDB_Log.bak';


3. Create the availability group.

MS SQL
 
CREATE AVAILABILITY GROUP AG_TestDB

    FOR DATABASE TestDB

    REPLICA ON 

        'PrimaryReplica' WITH (

            ENDPOINT_URL = 'TCP://PrimaryReplica:5022',

            AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,

            FAILOVER_MODE = AUTOMATIC

        ),

        'SecondaryReplica' WITH (

            ENDPOINT_URL = 'TCP://SecondaryReplica:5022',

            AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,

            FAILOVER_MODE = AUTOMATIC

        )

    LISTENER 'AGListener' (WITH IP (('10.0.0.10', 1433)));


Step 3: Add Secondary Replicas

1. Join the secondary replicas to the availability group.

MS SQL
 
ALTER AVAILABILITY GROUP AG_TestDB JOIN;


2. Restore the database on secondary replicas.

MS SQL
 
RESTORE DATABASE TestDB FROM DISK = '/var/opt/mssql/data/TestDB.bak' WITH NORECOVERY;

RESTORE LOG TestDB FROM DISK = '/var/opt/mssql/data/TestDB_Log.bak' WITH NORECOVERY;


3. Join the database of the availability group.

MS SQL
 
ALTER DATABASE TestDB SET HADR AVAILABILITY GROUP = AG_TestDB;


Disaster Recovery (Log Shipping)

Step 1: Configure Log Shipping

1. Enable log shipping on the primary database.

MS SQL
 
EXEC sp_add_log_shipping_primary_database

    @database = N'TestDB',

    @backup_directory = N'/var/opt/mssql/data/',

    @backup_retention_period = 4320,

    @monitor_server = N'MonitorServer';


2. Add a secondary database to log shipping.

MS SQL
 
EXEC sp_add_log_shipping_secondary_database

    @primary_server = N'PrimaryServer',

    @primary_database = N'TestDB',

    @secondary_server = N'SecondaryServer',

    @restore_delay = 0,

    @restore_mode = 1,

    @disconnect_users = 0,

    @block_size = 65536,

    @buffer_count = 5,

    @max_transfer_size = 0,

    @load_delay = 0,

    @copy_delay = 0,

    @copy_job_id = NULL,

    @restore_job_id = NULL;


Step 2: Monitor and Manage Log Shipping

1. Monitor log shipping status.

MS SQL
 
EXEC sp_help_log_shipping_monitor;


2. Test the log shipping by performing a failover.

MS SQL
 
RESTORE DATABASE TestDB WITH RECOVERY;


4. Recovery Process

Step 1: Failover To Secondary Replica

1. Initiate failover to the secondary replica in case of primary failure.

MS SQL
 
ALTER AVAILABILITY GROUP AG_TestDB FAILOVER;


Step 2: Restore Backups

1. Restore the database from the latest backup on a new server.

MS SQL
 
RESTORE DATABASE TestDB FROM DISK = '/var/opt/mssql/data/TestDB.bak' WITH RECOVERY;


2. Reconfigure the new server as part of the availability group.

MS SQL
 
ALTER AVAILABILITY GROUP AG_TestDB ADD REPLICA ON 'NewPrimaryReplica'

    WITH (ENDPOINT_URL = 'TCP://NewPrimaryReplica:5022');


These are the optimal methods I adhere to to establish a connection and recover in the event of failover. While I am providing an example here, this is based on actual real-time discoveries I made during my development process.

To establish and oversee High Availability and Disaster Recovery for SQL Server on AWS, it is essential to follow these procedures. This will guarantee the resilience and accessibility of your databases, even in the event of unforeseen failures.

In conclusion, the implementation of High Availability and Disaster Recovery (HADR) in SQL Server on AWS necessitates the configuration of Always Availability Groups for high availability and log shipping for disaster recovery. By utilizing these powerful features, you can guarantee that your SQL Server databases are readily available and can swiftly recover from disasters, thereby reducing downtime and minimizing data loss.

AWS Disaster recovery sql

Opinions expressed by DZone contributors are their own.

Related

  • Using AWS Data Lake and S3 With SQL Server: A Detailed Guide With Research Paper Dataset Example
  • Securing AWS RDS SQL Server for Retail: Comprehensive Strategies and Implementation Guide
  • Fortifying Web Applications: A Guide To Preventing SQL Injection in AWS RDS SQL Server
  • Disaster Recovery and High Availability Solutions in SQL Server

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: