This page describes how to create a read replica for a Cloud SQL instance.
A read replica is a copy of the primary instance that reflects changes to the primary in almost real time. You can use a read replica to do the following:
- Offload read requests or analytics traffic from the primary
- Perform a regional migration or fail over to another region for disaster recovery purposes (if the replica is a cross-region replica, that is, a replica created in a region different from that of the primary)
For more information about how replication works, see Replication in Cloud SQL.
Before you begin
If you are creating the first replica for this instance, ensure that the instance meets the requirements for primary instances. Learn more.
Creating a read replica
The steps for creating a read replica are below.
Console
- Go to the Cloud SQL Instances page in the Google Cloud Console.
- Find the instance you want to create a replica for, and open its
more actions
menu at the far right of the listing. - Select Create read replica.
If you do not see that choice, the instance is a replica; you cannot create a replica of a replica.
- If the instance had backups and binary logging enabled, continue with
Step 6. Otherwise, select Automate
backups and Enable binary logging, click Continue, and
then click Save and restart to restart the instance.
Enabling binary logging causes the instance to be restarted.
- In the Create read replica page, update the instance ID, if needed, and any other configuration options required, including name, region, and zone.
- Click Create.
Cloud SQL creates a backup, if needed, and creates the replica. You are returned to the instance page for the primary.
gcloud
- Check the status of the primary instance:
gcloud sql instances describe PRIMARY_INSTANCE_NAME
If the
databaseReplicationEnabled
property istrue
, the instance is a replica; you cannot create a replica of a replica. - If the
enabled
property underbackupConfiguration
isfalse
, enable backups for the primary instance now:gcloud sql instances patch PRIMARY_INSTANCE_NAME --backup-start-time >HH:MM
Thebackup-start-time
parameter is specified in 24-hour time, in the UTC±00 time zone, and specifies the start of a 4-hour backup window. Backups can start any time during the backup window. - If the
binaryLogEnabled
property isfalse
, enable binary logs on the primary instance:gcloud sql instances patch --enable-bin-log PRIMARY_INSTANCE_NAME
Enabling binary logs causes the instance to be restarted. - Create the replica:
gcloud sql instances create REPLICA_NAME --master-instance-name=PRIMARY_INSTANCE_NAME
You can specify a different tier size using the--tier
parameter, if needed. You can specify a different region using the--region
parameter. If the primary instance has a private IP address only, add the--no-assign-ip
parameter to the command.
-
Binary logging is supported on read replica instances (MySQL 5.7 and 8.0
only. Not supported on legacy HA failover replicas). Enable binary logging
on a replica with the same
gcloud
command, using the replica's instance name instead of the primary's instance name.gcloud sql instances patch --enable-bin-log REPLICA_INSTANCE_NAME
Binary logging durability on the replica (but not on the primary) instance can be set with thesync_binlog
flag, which controls how often the MySQL server synchronizes the binary log to disk.Backups cannot be enabled on replica instances, but binary logging can be enabled on a replica even when backups are disabled, unlike the primary.
The binlog retention period on replica instances is automatically set to one day, unlike seven days on primary instances.
REST v1beta4
- Get the current backup configuration
Use the
get
method of the instances resource to return the database version and current backup configuration for the master.Before using any of the request data below, make the following replacements:
- project-id: The project ID
- primary-instance-name: The name of the primary instance
HTTP method and URL:
GET https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/primary-instance-name
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
- Verify that the replication fields are set
If either
enabled
orbinaryLogEnabled
arefalse
on the primary instance, use thepatch
method of the instances resource to enable them both. In the request, specify any properties of the backup configuration that you want to change.To enable backups, set
enabled
totrue
and thestartTime
to a time of day inHH:MM
format. ThestartTime
parameter is specified in 24-hour time, in the UTC±00 time zone, and specifies the start of a 4-hour backup window. Backups can start any time during the backup window.To enable point-in-time recovery, set
Binary logging is supported on read replica instances (MySQL 5.7 and 8.0 only). Enable binary logging on a replica with the same API, using the replica's instance ID instead of the primary's instance ID. Binary logging durability on the replica (but not on the primary) instance can be set with thebinaryLogEnabled
totrue
on the primary instance.sync_binlog
flag, which controls how often the MySQL server synchronizes the binary log to disk. Backups cannot be enabled on replica instances, but binary logging can be enabled on a replica even when backups are disabled, unlike the primary.Before using any of the request data below, make the following replacements:
- project-id: The project ID
- instance-id: The instance ID (primary or replica)
- start-time The time in the format "HH:MM"
- enabled: Set to true for a primary instance. Set to false for a replica instance
HTTP method and URL:
PATCH https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id
Request JSON body:
{ "settings": { "backupConfiguration": { "startTime": "start-time", "enabled": enabled, "binaryLogEnabled": true } } }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
- Create the read replica
Use the
insert
method of the instances resource to create the read replica. ThedatabaseVersion
property must be the same as the primary. For a cross-region read replica, specify a region other than the primary instance's region.Before using any of the request data below, make the following replacements:
- project-id: The project ID
- database-version: Enum version string (for example, MYSQL_8_0)
- primary-instance-name: The name of the primary instance
- primary-instance-region: The region of the primary instance
- replica-region: The region of the replica instance
- replica-name: The name of the replica instance
- machine-type: Enum string of the machine (tier) type. For example: "db-n1-standard-4"
HTTP method and URL:
POST https://www.googleapis.com/sql/v1beta4/projects/project-id/instances
Request JSON body:
{ "masterInstanceName": "primary-instance-name", "project": "project-id", "databaseVersion": "database-version", "name": "replica-name", "region": "replica-region", "settings": { "tier": "machine-type", "settingsVersion": 0, } }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
Configuring read replicas for IAM database authentication
Read replicas do not have the cloudsql.iam_authentication
flag enabled
automatically when it is enabled on the primary instance.
To configure a read replica for IAM database authentication:
- Go to the Cloud SQL Instances page in the Google Cloud Console.
- Click the name of the primary instance.
- In the Configuration tile, look for the
cloudsql.iam_authentication
flag. If the flag isn't on the list, enabling the flag in the read replica is unnecessary. If the flag is on the list, you must enable the flag on the read replica. If you need to enable the flag on the read replica, continue with the next step.. - Select Replicas from the left navigation.
- Click the name of the replica you want to edit.
- Click EDIT.
- Under Configuration options, expand Flags.
- Select + Add item.
- Enter
cloudsql.iam_authentication
for the flag name. Make sure that On is selected for this flag. - Click Save.
Troubleshooting
Click the links in the table for details:
For this problem... | The issue might be... | Try this... |
---|---|---|
Read replica did not start replicating on creation. | There could be many root causes. | Check the logs to find more information. |
Unable to create read replica - invalidFlagValue error. | One of the flags provided explicitly or by default is invalid. | Check flag values and logs to find more information. |
Unable to create read replica - unknown error. | There could be many root causes. | Check the logs to find more information. |
Disk is full. | The primary instance disk size can become full during replica creation. | Upgrade the primary instance to a larger disk size. |
Replica instance is using too much memory. | Replicas can cache often-requested read operations. | Restart the replica instance to reclaim the temporary memory space. |
Replication stopped. | Max storage space was reached and automatic storage increase is not enabled. | Enable automatic storage increase. |
Replication lag is consistently high. | There can be many different root causes. | Here are a few things to try. |
Read replica did not start replicating on creation
The read replica did not start replicating on creation.
The issue might be
There is probably a more specific error in the log files.
Things to try
Inspect the logs in Cloud Logging to find the actual error.
Unable to create read replica - invalidFlagValue error
Unable to create read replica - invalidFlagValue
The issue might be
One of the flags in the request is invalid. It could be a flag you provided explicitly or one that was set to a default value.
Things to try
First, check that the value of the max_connections
flag is greater
than or equal to the value on the primary.
If the max_connections
flag is set appropriately,
inspect the logs in
Cloud Logging to find the actual error.
Unable to create read replica - unknown error
Unable to create read replica - unknown error
.
The issue might be
There is probably a more specific error in the log files.
Things to try
Inspect the logs in Cloud Logging to find the actual error.
If the error is: set Service Networking service account as
servicenetworking.serviceAgent role on consumer project
, then disable and
re-enable the Service Networking
API
. This action creates the service account necessary to continue
with the process.
Disk is full
UPDATE_DISK_SIZE
or mysqld: disk is full
error.
The issue might be
The primary instance disk size can become full during replica creation.
Things to try
Edit the primary instance to upgrade it to a larger disk size.
Replica instance is using too much memory
The replica instance is using too much memory.
The issue might be
The replica uses temporary memory to cache often-requested read operations, which can lead it to use more memory than the primary instance.
Things to try
Restart the replica instance to reclaim the temporary memory space.
Replication stopped
Replication stopped.
The issue might be
The maximum storage limit was reached and >automatic storage
increase is disabled
.
Things to try
Edit the instance to enable automatic storage increase
.
Replication lag is consistently high
Replication lag is consistently high.
The issue might be
The write load is too high for the replica to handle. Replication lag takes place when the SQL thread on a replica is unable to keep up with the IO thread. Some kinds of queries or workloads can cause temporary or permanent high replication lag for a given schema. Some of the typical causes of replication lag are:
- Slow queries on the replica. Find and fix them.
- All tables must have a unique/primary key. Every update on such a table without a unique/primary key causes full table scans on th replica.
- Queries like
DELETE ... WHERE field < 50000000
cause replication lag with row-based replication since a huge number of updates are piled up on the replica.
Things to try
Some possible solutions:
- Configure parallel replication.
- Edit the instance to increase the size of the replica.
- Reduce the load on the database.
- Index the tables.
- Identify and fix slow queries.
- Recreate the replica.
What's next
- Learn how to manage replicas.
- Learn about cross-region replicas.