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, in normal circumstances. You can use a read replica to offload read requests or analytics traffic from the primary instance.
Additionally, for disaster recovery, you can perform a regional migration. If a replica is a cross-region replica, you can perform a failover to another region; specifically, you can promote a replica to a standalone instance (in which case, existing replicas would not consider that instance as 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.
Create a read replica
The steps for creating a read replica are below.
If the primary instance has a private IP connection and the instance accepts database connections from Cloud SQL Language Connectors only, then you can't create read replicas for the instance.
Console
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Find the instance for which you want to create a replica, and then open the
more actionsmenu next to the listing. - Select Create read replica.
If you don't see that choice, then the instance is a replica; you can't create a replica of a replica.
In the Customize your instance section of the page, update settings for your replica. Begin by clicking Show configuration options to display the groups of settings. Then, expand the desired groups to review and customize the settings. A Summary of all the options you select appears on the right. Customizing these settings is optional. Defaults are assigned in every case where no customizations are made.
For more details about each setting, see the About instance settings page.
For example, to allow other Google Cloud services, such as BigQuery, to access data in Cloud SQL and make queries against this data over an internal connection, expand the Connections group, and then deselect the Public IP check box.
- Click Create replica.
Cloud SQL creates a backup, if needed, and creates the replica. You are returned to the instance page for the primary.
gcloud
Create the replica:
gcloud sql instances create REPLICA_NAME \ --master-instance-name=MASTER_INSTANCE_NAME
--tier
parameter, if needed. Also, if you're creating a replica from a primary instance, and the Cloud SQL edition for the instance is either Enterprise or is Enterprise Plus, version 16 and later, then you don't have to specify a value for this parameter. The replica inherits the machine type from the primary instance.
You can specify a different region using the --region
parameter.
If the primary instance has an internal IP address only, and you want to
allow other Google Cloud services, such as BigQuery, to
access data in Cloud SQL and make queries against this data over
an internal connection, then add the --enable-google-private-path parameter to the command.
You must create the replica in the same VPC network
as the primary instance. You
can also specify an allocated-ip-range-name in that
VPC network. If no range is specified, the replica is
created in a random range.
Terraform
To create a read replica, use a Terraform resource.
REST v1
Use the
insert
method of the instances resource to create the read replica. The region
and databaseVersion properties must be the same as the master.
Before using any of the request data, make the following replacements:
- project-id: The project ID
- database-version: Enum version string (for example, POSTGRES_12)
- 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 type. For example: "db-custom-1-3840"
- private-network: The authorized network that you're adding or selecting to create a private connection.
You can use the sqlNetworkArchitecture field to enforce the use of the new network
architecture for the instance upon creation, even if the project isn't fully upgraded.
For more details about the new network architecture and its implications, see
Upgrade an instance to the new network architecture and Allocate an IP address range.
HTTP method and URL:
POST https://sqladmin.googleapis.com/v1/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,
"ipConfiguration": {
object (IpConfiguration)
},
{
"ipv4Enabled": false,
"privateNetwork": private-network,
"requireSsl": boolean,
"authorizedNetworks": [
{
object (AclEntry)
}
],
"allocatedIpRange": string
}
},
"sqlNetworkArchitecture": "NEW_NETWORK_ARCHITECTURE"
}
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
REST v1beta4
Use the
insert
method of the instances resource to create the read replica. The region
and databaseVersion properties must be the same as the master.
Before using any of the request data, make the following replacements:
- project-id: The project ID
- database-version: Enum version string (for example, POSTGRES_12)
- 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 type. For example: "db-custom-1-3840"
- private-network: The authorized network that you're adding or selecting to create a private connection.
You can use the sqlNetworkArchitecture field to enforce the use of the new network
architecture for the instance upon creation, even if the project isn't fully upgraded.
For more details about the new network architecture and its implications, see
Upgrade an instance to the new network architecture and Allocate an IP address range.
HTTP method and URL:
POST https://sqladmin.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": "