How do I create a read replica for an Amazon Aurora MySQL DB instance?

6 minute read
0

I have an Amazon Aurora MySQL cluster. I would like to create either an in-Region or cross-region read replica or add regions for my Aurora DB cluster. How can I do this?

Short description

Aurora Replicas increase compute capacity within your Aurora MySQL DB cluster. They help in distributing read workload (read scaling) within the region and also act as failover targets. This means that if your primary DB instance fails, an Aurora replica is then promoted to the primary instance.

A cross-Region read replica uses MySQL binary logging to replicate your data to a cluster in another Region. This allows you to scale read operations into a secondary Region closer to your users. This helps improve disaster recovery capabilities. It also makes it easier to migrate from one AWS Region to another.

Amazon Aurora global databases can span multiple AWS Regions. They allow for low latency global reads and provide recovery from the rare outage that might affect an entire AWS Region. Aurora replicates data to the secondary AWS Regions using its own replication protocol and dedicated infrastructure, resulting in latencies typically under one second.

Use the following steps to create either an in-Region or cross-Region read replica, or add a new region to your Aurora MySQL DB cluster.

In addition to Aurora-managed binary logging, you can set up and manage your own binary logging replication environment. In this environment, Aurora can be the source or the target.

Note: These steps do not apply to Aurora Serverless or Aurora multi-master clusters.

Resolution

Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you’re using the most recent AWS CLI version.

Create an in-Region read replica for an Aurora MySQL cluster

Using the Amazon RDS console

1.    Open the Amazon RDS console, and choose Databases from the navigation pane.

2.    Choose the DB cluster that you want to add the new DB instance to.

3.    In the Actions section, choose Add reader.

4.    On the Add reader page, customize the options for your Aurora replica.

5.    Choose Add reader to create the replica.

Using the AWS CLI

To create an Aurora Replica in your DB cluster using the AWS CLI, run the create-db-instance command. Include the name of the DB cluster using the --db-cluster-identifier option. You can also specify an Availability Zone (AZ) for the Aurora replica using the availability-zone parameter.

This example command creates a read replica for Aurora MySQL (same Region and Aurora MySQL 5.7 and Aurora MySQL 8.0 compatible).

Linux, macOS, or Unix:

aws rds create-db-instance --db-instance-identifier sample-instance-us-west-2a \
    --db-cluster-identifier sample-cluster --engine aurora-mysql --db-instance-class db.r6g.large \
    --availability-zone us-west-2a

Windows:

aws rds create-db-instance --db-instance-identifier sample-instance-us-west-2a ^
    --db-cluster-identifier sample-cluster --engine aurora-mysql --db-instance-class db.r6g.large ^
    --availability-zone us-west-2a

Note: If you want to create a read replica for an Aurora MySQL 5.6 compatible cluster, modify the --engine parameter to --engine aurora in this command.

Create a cross-Region read replica for an Aurora MySQL cluster

Before you can create a cross-Region read replica, you must turn on binary logging for your source Aurora MySQL DB cluster. Cross Region-replication uses MySQL binary replication to replay changes on the cross-Region read replica cluster. Then, create a cross-Region cluster using either the Amazon RDS console or the AWS CLI. 

Note: You can't create an encrypted Aurora Replica for an unencrypted Aurora DB cluster. You can't create an unencrypted Aurora Replica for an encrypted Aurora DB cluster.

Turn on binary logging for your DB cluster

1.    Update the binlog_format parameter for your source DB cluster. This is a cluster-level parameter that is in the default cluster parameter group.

2.    If your DB cluster uses the default DB cluster parameter group, create a new DB cluster parameter group to modify the binlog_format settings. It's a best practice to set the binlog_format to MIXED. You can also set binlog_format to ROW or STATEMENT if you need a specific binlog format.

3.    Reboot your Aurora DB cluster for changes to take effect.

Create a cross-Region cluster using the Amazon RDS console

1.    Open the Amazon RDS consoleand then choose the AWS Region that hosts your source DB cluster.

2.    From the navigation pane, choose Instances.

3.    Choose the check box for the DB instance that you want to create a cross-Region read replica for.

4.    For Actions, choose Create cross region read replica.

5.    On the Create cross region read replica page, choose the option settings for your cross-Region read replica DB cluster.

Create a cross-Region cluster using the AWS CLI

To create a cross-Region cluster using the AWS CLI, first create the cluster itself in the target Region using the create-db-cluster call. Then, use the create-db-instance call to create the reader instance.

This example creates a cross Region replica using Linux, macOS or Unix and is Aurora MySQL 5.7 and Aurora MySQL 8.0 compatible.

1.    Create the cluster by running a command similar to the following:

aws rds create-db-cluster \
  --db-cluster-identifier sample-replica-cluster \
  --engine aurora-mysql \
  --replication-source-identifier arn:aws:rds:us-west-2:123456789012:cluster:sample-source-cluster

2.    Verify if the cluster is available:

aws rds describe-db-clusters --db-cluster-identifier sample-replica-cluster

3.    Create instance in the cluster:

aws rds create-db-instance \
  --db-cluster-identifier sample-replica-cluster \
  --db-instance-class db.r6g.large \
  --db-instance-identifier sample-replica-instance \
  --engine aurora-mysql

To create the same using CLI on Windows, use the same steps, but use the ^ symbol as line delimiter instead of ****.

Add an AWS region to an Aurora provisioned cluster to create an Aurora Global Database

Using the Amazon RDS console

  1. Open the Amazon RDS console.

  2. In the navigation pane, choose Databases.

  3. Choose the Aurora global database that needs a secondary Aurora DB cluster. Ensure that the primary Aurora DB cluster has a status of Available.

  4. From Actions, choose Add region.

  5. Choose the AWS Region you want to add. 

**Note:**You can't choose an AWS Region that already has a secondary Aurora DB cluster for the same Aurora global database. Additionally, you cant choose the same AWS Region as the region of the primary Aurora DB cluster. 

  1. Complete the remaining fields for the secondary Aurora cluster in the new AWS Region. Then, choose Add region

Using the AWS CLI

Run the following script on the AWS CLI.

Note: If you're using Windows, replace the \ line delimiter with the ^ symbol.

aws rds --region us-east-2 \
  create-db-cluster \
    --db-cluster-identifier secondary_cluster_id \
    --global-cluster-identifier global_database_id \
    --engine aurora-mysql \
    --engine-version version

aws rds --region us-east-2 \
  create-db-instance \
    --db-instance-class db.r6g.large \
    --db-cluster-identifier secondary_cluster_id \
    --db-instance-identifier db_instance_id \
    --engine aurora-mysql

Related information

Adding Aurora replicas to a DB cluster

Replicating Amazon Aurora MySQL DB clusters across AWS Regions

AWS OFFICIAL
AWS OFFICIALUpdated 2 years ago