Create read replicas

MySQL  |  PostgreSQL  |  SQL Server

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 theprimary in almost real time, in normal circumstances. You can use a read replicato offload read requests or analytics traffic from the primary instance.

Additionally, for disaster recovery, you can perform a regional migration. Ifa replica is across-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, seeReplication in Cloud SQL.

Before you begin

If you are creating the first replica for this instance, ensure that theinstance meets the requirements for primary instances.Learn more.

Note: For information about using parallel replication for performanceimprovements, seeConfiguring parallel replication.

Create a read replica

The steps for creating a read replica are below.

Note: If you specify MySQL 8.4 as the database version for your replica, then the default Cloud SQL edition is Enterprise Plus. If you don't specify a database version or you specify a version other than MySQL 8.4, then the default Cloud SQL edition is Enterprise.

If the primary instance has aprivate IP connection and the instance accepts database connections fromCloud SQL Language Connectors only, then you can't create read replicas for the instance.

Console

  1. In the Google Cloud console, go to theCloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Find the instance for which you want to create a replica, and then open themore actions menu next to the listing.
  3. SelectCreate read replica.

    If you don't see that choice, then the instance is a replica; you can't create a replica of a replica.

  4. If the instance had backups and binary logging enabled, continue with the next step. Otherwise, selectAutomate backups andEnable binary logging, clickContinue, and then clickSave and restart to restart the instance.

    Enabling binary logging causes the instance to be restarted.

  5. In theCustomize your instance section of the page, update settings for your replica. Begin by clickingShow configuration options to display the groups of settings. Then, expand the desired groups to review and customize the settings. ASummary 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 theAbout 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 theConnections group, and then deselect thePublic IP check box.

  6. ClickCreate replica.

    Cloud SQL creates a backup, if needed, and creates the replica. You are returned to the instance page for the primary.

gcloud

  1. Check the status of the primary instance:
    gcloudsqlinstancesdescribePRIMARY_INSTANCE_NAME

    If thedatabaseReplicationEnabled property istrue, the instance is a replica; you cannot create a replica of a replica.

  2. If theenabled property underbackupConfiguration isfalse, enable backups for the primary instance now:
    gcloudsqlinstancespatchPRIMARY_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.
  3. If thebinaryLogEnabled property isfalse, enable binary logs on the primary instance:
    gcloudsqlinstancespatchPRIMARY_INSTANCE_NAME\--enable-bin-log
    Enabling binary logs causes the instance to be restarted.
  4. Create the replica:
    gcloudsqlinstancescreateREPLICA_NAME\--master-instance-name=PRIMARY_INSTANCE_NAME

    You can specify a different tier size using the--tier parameter, if needed. If you're creating a replica from a primary instance for MySQL 8.4 and later, and the Cloud SQL edition for the instance is either Enterprise or is Enterprise Plus, 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.

    You can add more parameters for other instance settings. To learn more, seegcloud sql instances create.

    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 anallocated-ip-range-name in that VPC network. If no range is specified, the replica is created in a random range.

  • Binary logging is supported on read replica instances (MySQL 5.7 and later only. Not supported on legacy HA failover replicas). Enable binary logging on a replica with the samegcloud CLI command, using the replica's instance name instead of the primary's instance name.
    gcloudsqlinstancespatchREPLICA_INSTANCE_NAME\--enable-bin-log\--enforce-new-network-architecture

    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.

  • Use theenforce-new-network-architecture flag 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, seeUpgrade an instance to the new network architecture andAllocate an IP address range.

Terraform

To create a read replica, useTerraform resource.

resource "google_sql_database_instance" "read_replica" {  name                 = "mysql-replica-instance-name"  master_instance_name = google_sql_database_instance.primary.name  region               = "europe-west4"  database_version     = "MYSQL_8_0"  replica_configuration {    failover_target = false  }  settings {    tier              = "db-n1-standard-2"    availability_type = "ZONAL"    disk_size         = "100"  }  # set `deletion_protection` to true, will ensure that one cannot accidentally delete this instance by  # use of Terraform whereas `deletion_protection_enabled` flag protects this instance at the GCP level.  deletion_protection = false}

Apply the changes

To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.

Prepare Cloud Shell

  1. LaunchCloud Shell.
  2. Set the default Google Cloud project where you want to apply your Terraform configurations.

    You only need to run this command once per project, and you can run it in any directory.

    export GOOGLE_CLOUD_PROJECT=PROJECT_ID

    Environment variables are overridden if you set explicit values in the Terraform configuration file.

Prepare the directory

Each Terraform configuration file must have its own directory (alsocalled aroot module).

  1. InCloud Shell, create a directory and a new file within that directory. The filename must have the.tf extension—for examplemain.tf. In this tutorial, the file is referred to asmain.tf.
    mkdirDIRECTORY && cdDIRECTORY && touch main.tf
  2. If you are following a tutorial, you can copy the sample code in each section or step.

    Copy the sample code into the newly createdmain.tf.

    Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.

  3. Review and modify the sample parameters to apply to your environment.
  4. Save your changes.
  5. Initialize Terraform. You only need to do this once per directory.
    terraform init

    Optionally, to use the latest Google provider version, include the-upgrade option:

    terraform init -upgrade

Apply the changes

  1. Review the configuration and verify that the resources that Terraform is going to create or update match your expectations:
    terraform plan

    Make corrections to the configuration as necessary.

  2. Apply the Terraform configuration by running the following command and enteringyes at the prompt:
    terraform apply

    Wait until Terraform displays the "Apply complete!" message.

  3. Open your Google Cloud project to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.
Note: Terraform samples typically assume that the required APIs are enabled in your Google Cloud project.

Delete the changes

To delete your changes, do the following:

  1. To disable deletion protection, in your Terraform configuration file set thedeletion_protection argument tofalse.
    deletion_protection =  "false"
  2. Apply the updated Terraform configuration by running the following command and enteringyes at the prompt:
    terraform apply
  1. Remove resources previously applied with your Terraform configuration by running the following command and enteringyes at the prompt:

    terraform destroy

REST v1

  1. Get the current backup configuration

    Use theget method of the instances resource to return the database version and current backup configuration for the primary.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • primary-instance-name: The name of the primary instance

    HTTP method and URL:

    GET https://sqladmin.googleapis.com/v1/projects/project-id/instances/primary-instance-name

    To send your request, expand one of these options:

    curl (Linux, macOS, or Cloud Shell)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.

    Execute the following command:

    curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://sqladmin.googleapis.com/v1/projects/project-id/instances/primary-instance-name"

    PowerShell (Windows)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.

    Execute the following command:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method GET `
    -Headers $headers `
    -Uri "https://sqladmin.googleapis.com/v1/projects/project-id/instances/primary-instance-name" | Select-Object -Expand Content

    You should receive a JSON response similar to the following:

    Response

    "backupConfiguration": {    "kind": "sql#backupConfiguration",    "startTime": "12:00",    "enabled": false,    "binaryLogEnabled": false}
  2. Verify that the replication fields are set

    If eitherenabled orpointInTimeEnabled arefalse, use thepatch method of the instances resource to enable them both. In the request, specify any properties of the backup configuration that you wish to change.

    To enable backups, setenabled 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, setpointInTimeEnabled totrue.

    Before using any of the request data, make the following replacements:

    • PROJECT_ID: the ID orproject number of the Google Cloud project that contains the instance
    • INSTANCE_NAME: the name of the primary or read replica instance that you're configuring for high availability
    • START_TIME: the time (in hours and minutes)

    HTTP method and URL:

    PATCH https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_NAME

    Request JSON body:

    {  "settings":  {    "backupConfiguration":    {      "startTime": "START_TIME",      "enabled": true,      "binaryLogEnabled": true    }  }}

    To send your request, expand one of these options:

    curl (Linux, macOS, or Cloud Shell)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.

    Save the request body in a file namedrequest.json, and execute the following command:

    curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_NAME"

    PowerShell (Windows)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.

    Save the request body in a file namedrequest.json, and execute the following command:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method PATCH `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_NAME" | Select-Object -Expand Content

    You should receive a JSON response similar to the following:

    Response

    {  "kind": "sql#operation",  "targetLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_NAME",  "status": "PENDING",  "user": "user@example.com",  "insertTime": "2020-01-21T22:43:37.981Z",  "operationType": "UPDATE",  "name": "OPERATION_ID",  "targetId": "INSTANCE_NAME",  "selfLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID",  "targetProject": "PROJECT_ID"}
  3. Create the read replica

    Use theinsert method of the instances resource to create the read replica. ThedatabaseVersion property must be the same as the primary. If the primary instance uses an internal IP address, you can specify anallocatedIpRange the same way as when youcreate a primary instance. If no range is specified, the replica is created in a random range. For a cross-region read replica, specify a region other than the primary instance's region.

    Before using any of the request data, 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 type. For example: "db-custom-1-3840"
    • private-network: The authorized network that you're adding or selecting to create a private connection.
    You set the value of theipv4Enabled parameter tofalse because you want the Cloud SQL instance to be associated with a private IP address, not a public IP one.

    You can use thesqlNetworkArchitecture field to enforce the use of the new networkarchitecture for the instance upon creation, even if the project isn't fully upgraded.For more details about the new network architecture and its implications, seeUpgrade an instance to the new network architecture andAllocate 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:

    curl (Linux, macOS, or Cloud Shell)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.

    Save the request body in a file namedrequest.json, and execute the following command:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://sqladmin.googleapis.com/v1/projects/project-id/instances"

    PowerShell (Windows)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.

    Save the request body in a file namedrequest.json, and execute the following command:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://sqladmin.googleapis.com/v1/projects/project-id/instances" | Select-Object -Expand Content

    You should receive a JSON response similar to the following:

    Response

    {  "kind": "sql#operation",  "targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id",  "status": "PENDING",  "user": "user@example.com",  "insertTime": "2020-01-21T22:43:37.981Z",  "operationType": "CREATE_REPLICA",  "name": "operation-id",  "targetId": "instance-id",  "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id/operations/operation-id",  "targetProject": "project-id"}

REST v1beta4

  1. Get the current backup configuration

    Use theget method of the instances resource to return the database version and current backup configuration for the master.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • primary-instance-name: The name of the primary instance

    HTTP method and URL:

    GET https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/primary-instance-name

    To send your request, expand one of these options:

    curl (Linux, macOS, or Cloud Shell)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.

    Execute the following command:

    curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/primary-instance-name"

    PowerShell (Windows)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.

    Execute the following command:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method GET `
    -Headers $headers `
    -Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/primary-instance-name" | Select-Object -Expand Content

    You should receive a JSON response similar to the following:

    Response

    "backupConfiguration": {    "kind": "sql#backupConfiguration",    "startTime": "12:00",    "enabled": false,    "binaryLogEnabled": false}
  2. Verify that the replication fields are set

    If eitherenabled 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, setenabled 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, setbinaryLogEnabled totrue on the primary instance.

    Binary logging is supported on read replica instances (MySQL 5.7 and later 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 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.

    Before using any of the request data, make the following replacements:

    • PROJECT_ID: the ID orproject number of the Google Cloud project that contains the instance
    • INSTANCE_NAME: the name of the primary or read replica instance that you're configuring for high availability
    • START_TIME: the time (in hours and minutes)

    HTTP method and URL:

    PATCH https://sqladmin.googleapis.com/v1beta4/projects/PROJECT_ID/instances/INSTANCE_NAME

    Request JSON body:

    {  "settings":  {    "backupConfiguration":    {      "startTime": "START_TIME",      "enabled": true,      "binaryLogEnabled": true    }  }}

    To send your request, expand one of these options:

    curl (Linux, macOS, or Cloud Shell)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.

    Save the request body in a file namedrequest.json, and execute the following command:

    curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://sqladmin.googleapis.com/v1beta4/projects/PROJECT_ID/instances/INSTANCE_NAME"

    PowerShell (Windows)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.

    Save the request body in a file namedrequest.json, and execute the following command:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method PATCH `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://sqladmin.googleapis.com/v1beta4/projects/PROJECT_ID/instances/INSTANCE_NAME" | Select-Object -Expand Content

    You should receive a JSON response similar to the following:

    Response

    {  "kind": "sql#operation",  "targetLink": "https://sqladmin.googleapis.com/v1beta4/projects/PROJECT_ID/instances/INSTANCE_NAME",  "status": "PENDING",  "user": "user@example.com",  "insertTime": "2020-01-21T22:43:37.981Z",  "operationType": "UPDATE",  "name": "OPERATION_ID",  "targetId": "INSTANCE_NAME",  "selfLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID",  "targetProject": "PROJECT_ID"}
  3. Create the read replica

    Use theinsert method of the instances resource to create the read replica. ThedatabaseVersion property must be the same as the primary. If the primary instance uses an internal IP address, you can specify anallocatedIpRange the same way as when youcreate a primary instance. For a cross-region read replica, specify a region other than the primary instance's region.

    Before using any of the request data, 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 type. For example: "db-custom-1-3840"
    • private-network: The authorized network that you're adding or selecting to create a private connection.
    You set the value of theipv4Enabled parameter tofalse because you want the Cloud SQL instance to be associated with a private IP address, not a public IP one.

    You can use thesqlNetworkArchitecture field to enforce the use of the new networkarchitecture for the instance upon creation, even if the project isn't fully upgraded.For more details about the new network architecture and its implications, seeUpgrade an instance to the new network architecture andAllocate 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": "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:

    curl (Linux, macOS, or Cloud Shell)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.

    Save the request body in a file namedrequest.json, and execute the following command:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances"

    PowerShell (Windows)

    Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.

    Save the request body in a file namedrequest.json, and execute the following command:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances" | Select-Object -Expand Content

    You should receive a JSON response similar to the following:

    Response

    {  "kind": "sql#operation",  "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id",  "status": "PENDING",  "user": "user@example.com",  "insertTime": "2020-01-21T22:43:37.981Z",  "operationType": "CREATE_REPLICA",  "name": "operation-id",  "targetId": "instance-id",  "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id",  "targetProject": "project-id"}

Create a read replica of an instance with Private Service Connect enabled

To create a read replica of an instance withPrivate Service Connect enabled, usegcloud CLI or the API. You can create this replica in either the same region or in a different region from the primary instance (cross-region read replica).

The read replica can't replicate from an instance with a different connectivity type. For example, an instance with Private Service Connect enabled can replicate only from another Private Service Connect instance. It also can't replicate from an instance that supports external IP connections or from an instance that's configured withprivate services access.

gcloud

To create a read replica of an instance, use thegcloud sql instances create command:

gcloudsqlinstancescreateREPLICA_INSTANCE_NAME\--master-instance-name=PRIMARY_INSTANCE_NAME\--project=PROJECT_ID\--region=REGION_NAME\--enable-private-service-connect\--allowed-psc-projects=ALLOWED_PROJECTS\--availability-type=AVAILABILITY_TYPE\--no-assign-ip

Make the following replacements:

  • REPLICA_INSTANCE_NAME: the name of the replica instance.
  • PRIMARY_INSTANCE_NAME: the name of the primary instance.
  • PROJECT_ID: the ID orproject number of the Google Cloud project that contains the instance.
  • REGION_NAME: the region name for the replica instance.
  • ALLOWED_PROJECTS: a list of allowed project IDs or numbers, separated by commas. If a project isn't contained in this list, then you can't use it to create an instance and enable Private Service Connect for it.

    Cloud SQL doesn't copy the allowed projects for the primary instance over to the replica. For each replica, you must create aPrivate Service Connect endpoint. If you're using theCloud SQL Auth Proxy orCloud SQL Language Connectors, then create aDNS zone andDNS record for the replicas.

  • AVAILABILITY_TYPE: enable high availability for the instance. For this parameter, specify one of the following values:
    • REGIONAL: enable high availability and is recommended for production instances. The instance fails over to another zone within your selected region.
    • ZONAL: provide no failover capability. This is the default value.

    For more information about setting and removing high availability for instances, seeConfigure an existing instance for high availability andDeactivate high availability for an instance.

REST v1

Before using any of the request data, make the following replacements:

  • PRIMARY_INSTANCE_NAME: the name of the primary instance.
  • PROJECT_ID: the ID orproject number of the Google Cloud project that contains the instance.
  • REPLICA_INSTANCE_NAME: the name of the replica instance.
  • REGION_NAME: the region name for the replica instance.
  • MACHINE_TYPE: the machine type for the instance.
  • AVAILABILITY_TYPE: enable high availability for the instance. For this parameter, specify one of the following values:
    • REGIONAL: enable high availability and is recommended for production instances. The instance fails over to another zone within your selected region.
    • ZONAL: provide no failover capability. This is the default value.

    For more information about setting and removing high availability for instances, seeConfigure an existing instance for high availability andDeactivate high availability for an instance.

  • ALLOWED_PROJECTS: a list of allowed project IDs or numbers, separated by commas. If a project isn't contained in this list, then you can't use it to create an instance and enable Private Service Connect for it.

    Cloud SQL doesn't copy the allowed projects for the primary instance over to the replica. For each replica, you must create aPrivate Service Connect endpoint. If you're using theCloud SQL Auth proxy orCloud SQL Language Connectors, then you must create aDNS zone andDNS record for the replicas.

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": "MYSQL_8_0",  "name": "REPLICA_INSTANCE_NAME",  "region": "REGION_NAME",  "kind": "sql#instance",  "settings":  {    "tier": "MACHINE_TYPE",    "availabilityType": "AVAILABILITY_TYPE",    "settingsVersion": 0,    "ipConfiguration": {      "ipv4Enabled": false,      "pscConfig": {        "allowedConsumerProjects": [ALLOWED_PROJECTS],        "pscEnabled": true      }    },    "kind": "sql#settings",    "pricingPlan": "PER_USE",    "replicationType": "ASYNCHRONOUS",    "tier": "MACHINE_TYPE"  }}

To send your request, expand one of these options:

curl (Linux, macOS, or Cloud Shell)

Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.

Save the request body in a file namedrequest.json, and execute the following command:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances"

PowerShell (Windows)

Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.

Save the request body in a file namedrequest.json, and execute the following command:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances" | Select-Object -Expand Content

You should receive a JSON response similar to the following:

{  "kind": "sql#operation",  "targetLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/REPLICA_INSTANCE_NAME",  "status": "PENDING",  "user": "user@example.com",  "insertTime": "2020-01-16T02:32:12.281Z",  "operationType": "CREATE_REPLICA",  "name": "OPERATION_ID",  "targetId": "REPLICA_INSTANCE_NAME",  "selfLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID",  "targetProject": "PROJECT_ID"}

REST v1beta4

Before using any of the request data, make the following replacements:

  • PRIMARY_INSTANCE_NAME: the name of the primary instance.
  • PROJECT_ID: the ID orproject number of the Google Cloud project that contains the instance.
  • REPLICA_INSTANCE_NAME: the name of the replica instance.
  • REGION_NAME: the region name for the replica instance.
  • MACHINE_TYPE: the machine type for the instance.
  • AVAILABILITY_TYPE: enable high availability for the instance. For this parameter, specify one of the following values:
    • REGIONAL: enable high availability and is recommended for production instances. The instance fails over to another zone within your selected region.
    • ZONAL: provide no failover capability. This is the default value.

    For more information about setting and removing high availability for instances, seeConfigure an existing instance for high availability andDeactivate high availability for an instance.

  • ALLOWED_PROJECTS: a list of allowed project IDs or numbers, separated by commas. If a project isn't contained in this list, then you can't use it to create an instance and enable Private Service Connect for it.

    Cloud SQL doesn't copy the allowed projects for the primary instance over to the replica. For each replica, you must create aPrivate Service Connect endpoint. If you're using theCloud SQL Auth proxy orCloud SQL Language Connectors, then you must create aDNS zone andDNS record for the replicas.

HTTP method and URL:

PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances

Request JSON body:

{  "masterInstanceName": "PRIMARY_INSTANCE_NAME",  "project": "PROJECT_ID",  "databaseVersion": "MYSQL_8_0",  "name": "REPLICA_INSTANCE_NAME",  "region": "REGION_NAME",  "kind": "sql#instance",  "settings":  {    "tier": "MACHINE_TYPE",    "availabilityType": "AVAILABILITY_TYPE",    "settingsVersion": 0,    "ipConfiguration": {      "ipv4Enabled": false,      "pscConfig": {        "allowedConsumerProjects": [ALLOWED_PROJECTS],          "pscEnabled": true      }    },    "kind": "sql#settings",    "pricingPlan": "PER_USE",    "replicationType": "ASYNCHRONOUS",    "tier": "MACHINE_TYPE"  }}

To send your request, expand one of these options:

curl (Linux, macOS, or Cloud Shell)

Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.

Save the request body in a file namedrequest.json, and execute the following command:

curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances"

PowerShell (Windows)

Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.

Save the request body in a file namedrequest.json, and execute the following command:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances" | Select-Object -Expand Content

You should receive a JSON response similar to the following:

{  "kind": "sql#operation",  "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/REPLICA_INSTANCE_NAME",  "status": "PENDING",  "user": "user@example.com",  "insertTime": "2020-01-16T02:32:12.281Z",  "operationType": "CREATE_REPLICA",  "name": "OPERATION_ID",  "targetId": "REPLICA_INSTANCE_NAME",  "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID",  "targetProject": "PROJECT_ID"}

Configure read replicas for IAM database authentication

Read replicas do not have thecloudsql_iam_authentication flag enabledautomatically when it is enabled on the primary instance.

To configure a read replica for IAM database authentication:

  1. In the Google Cloud console, go to theCloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open theOverview page of an instance, click the instance name.
  3. In the Configuration tile, look for thecloudsql_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..
  4. SelectReplicas from the SQL navigation menu.
  5. Click the name of the replica you want to edit.
  6. ClickEdit.
  7. In theConfiguration options section, expandFlags.
  8. Select+ Add item.
  9. Entercloudsql_iam_authentication for the flag name. Make sure thatOn is selected for this flag.
  10. ClickSave.

Create cascading replicas

This section describes how to create and manage cascading replicas.

For information about how cascading replicas work, seeCascading replicas.

Steps to create a cascading replica

Console

  1. In the Google Cloud console, go to theCloud SQL Instances page.

    Go to Cloud SQL Instances

  2. For MySQL 5.7 or later,enable replication.
  3. Click theReplicas tab for the replica that will act as a parent for the replica that you want to create.
  4. ClickCreate replica.
  5. On theCreate read replica page, update the instance ID, and any other configuration options, including the name, region, and zone.
  6. ClickCreate.

    Cloud SQL creates a replica. You're returned to the instance page for the parent replica.

  7. Follow steps 4-6 for each new cascading replica that you want to create.

gcloud

  1. If you're using MySQL version 5.7 or later, then enable binlogs for the primary of the new replica:
    gcloudsqlinstancespatch--enable-bin-logPARENT_REPLICA_NAME
    ReplacePARENT_REPLICA_NAME with the name of the parent replica.
  2. Create the new replica by specifying your primary replica as the primary instance using the--master-instance-name flag:
  3. gcloudsqlinstancescreateREPLICA_NAME\--master-instance-name=PARENT_REPLICA_NAME\
    Replace the following:
    • REPLICA_NAME: the unique ID for the replica that you are creating
    • PARENT_REPLICA_NAME: the name of the parent replica
  4. After you create the cascading replica, you can see that the changesmade to the primary instance are replicated through all the replicas inthe cascading replicas chain.

curl

  1. If you are using MySQL version 5.7 or later, enable binary logging:

    To enable binary logging, save the following JSON in a file named request.JSON, then invoke the curl command to enable binary logging.
    {"settings":{"backupConfiguration":{"enabled":false,"binaryLogEnabled":true}}}
  2. To create a replica under the parent replica, edit the following JSON code sample, and save it to a file calledrequest.json:
    {"masterInstanceName":"PARENT_REPLICA_NAME","project":"PROJECT_ID","name":"REPLICA_NAME","region":"REPLICA_REGION","settings":{"tier":"MACHINE_TYPE",}}
  3. Run the following command:
    curl-XPOST-H"Authorization: Bearer "$(gcloudauthprint-access-token)-H"Content-Type: application/json; charset=utf-8"-d@request.json"https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances"

Troubleshoot

IssueTroubleshooting
Read replica didn't start replicating on creation.There's probably a more specific error in the log files.Inspect the logs in Cloud Logging to find the actual error.
Unable to create read replica - invalidFlagValue error.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.

First, check that the value of themax_connections flag is greater than or equal to the value on the primary.

If themax_connections flag is set appropriately,inspect the logs in Cloud Logging to find the actual error.

Unable to create read replica - unknown error.There's probably a more specific error in the log files.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 theService Networking API. This action creates the service account necessary to continue with the process.

Disk is full.The primary instance disk size can become full during replica creation. Edit the primary instance to upgrade it to a larger disk size.
The replica instance is using too much memory.The replica uses temporary memory to cache often-requested read operations, which can lead it to use more memory than the primary instance.

Restart the replica instance to reclaim the temporary memory space.

Replication stopped.The maximum storage limit was reached and automatic storage increase isn't enabled.

Edit the instance to enableautomatic storage increase.

Replication lag is consistently high.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 likeDELETE ... WHERE field < 50000000 cause replication lag with row-based replication since a huge number of updates are piled up on the replica.

Some possible solutions include:

Replication lag suddenly spikes.This is caused by long-running transaction(s). When a transaction (single statement or multi-statements) commits on the source instance, the start time of the transaction is recorded in the binary log. When the replica receives this binlog event, it compares that timestamp with the current timestamp to calculate replication lag. Hence, a long-running transaction on the source would result in an immediate large replication lag on the replica. If the amount of row changes in the transaction is large, the replica would also spend a long time to execute it. During the time, replication lag is increasing. Once the replica finishes this transaction, the catch up period would depend on the write workload on the source and the replica's processing speed.

To avoid a long transaction, some possible solutions include:

  • Break the transaction into multiple small transactions
  • Chunk a single large write query into smaller batches
  • Try to separate long SELECT queries from a transaction mixed with DMLs
Changing parallel replication flags results in an error.An incorrect value is set for one of or more of these flags.

On the primary instance that's displaying the error message, set the parallel replication flags:

  1. Modify thebinlog_transaction_dependency_tracking andtransaction_write_set_extractionflags:
    • binlog_transaction_dependency_tracking=COMMIT_ORDER
    • transaction_write_set_extraction=OFF
  2. Add theslave_pending_jobs_size_max flag:

    slave_pending_jobs_size_max=33554432

  3. Modify thetransaction_write_set_extraction flag:

    transaction_write_set_extraction=XXHASH64

  4. Modify thebinlog_transaction_dependency_tracking flag:

    binlog_transaction_dependency_tracking=WRITESET

Replica creation fails with timeout.Long-running uncommitted transactions on the primary instance can cause read replica creation to fail.

Recreate the replica after stopping all running queries.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-11-03 UTC.