Creating Read Replicas

This page describes how to create a read replica for a Cloud SQL instance.

A read replica is a copy of the master that reflects changes to the master instance in almost real time. You create a replica to offload read requests or analytics traffic from the master. You can create multiple read replicas for a single master instance.

For more information about how replication works, see Requirements and Tips for Configuring Replication.

Before you begin

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

Creating a read replica

The steps for creating a read replica are different for Second Generation and First Generation instances.

Create a read replica for a Second Generation master:

Console (2nd Gen)

  1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

    Go to the Cloud SQL Instances page

  2. Find the instance you want to create a replica for, and open its more actions menu at the far right of its listing.

    Image of three-dot menu

  3. Select Create replica.

    If you do not see that choice, the instance is a replica; you cannot create a replica of a replica.

  4. If the instance had backups and binary logging enabled, continue with step 6. Otherwise, enable backups and binary logging and click Continue.

    Image of stepper as you enable backups

  5. Click Save and restart to restart the instance.

    Enabling binary logging causes the instance to be restarted.

  6. In the Create read replica page, update the instance ID if needed, and any other settings as required.

    Image of Create replica pane

  7. Click Create.

    Image of Create button

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

gcloud (2nd Gen)

  1. Check the status of the master instance:
    gcloud sql instances describe [MASTER_INSTANCE_NAME]

    If the databaseReplicationEnabled property is true, the instance is a replica; you cannot create a replica of a replica.

  2. If the enabled property under backupConfiguration is false, enable backups for the master instance now:
    gcloud sql instances patch [MASTER_INSTANCE_NAME] --backup-start-time [HH:MM]
    The backup-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 the binaryLogEnabled property is false, enable binary logs:
    gcloud sql instances patch --enable-bin-log [MASTER_INSTANCE_NAME]
    Enabling binary logs causes the instance to be restarted.
  4. Create the replica:
    gcloud sql instances create [REPLICA_NAME] \
    --master-instance-name=[MASTER_INSTANCE_NAME] --tier=[TIER]
    

    Tier can be any value usable for Second Generation instances.

cURL (2nd Gen)

  1. Use the get method of the instances resource to return the database version and current backup configuration for the master. For example, using cURL, the request is:
    ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         -X GET \
         https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[MASTER_INSTANCE_NAME]
    

    The output contains the backupConfiguration:

    "backupConfiguration": [
     {
        "kind": "sql#backupConfiguration",
        "startTime": "12:00",
        "enabled": false,
        "binaryLogEnabled": false
     }
    
  2. If either enabled or binaryLogEnabled are false, use the patch method of the instances resource to enable them both. Specify the properties of the backup configuration you want to keep as is and those you want to change.

    To enable backups, set enabled to true and the startTime to a value which is the start of the backup window. To enable binary logging, set binaryLogEnabled to true.

    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         --data '{"settings" : {"backupConfiguration" : {"startTime": "[HH:MM]", "enabled": true, "binaryLogEnabled": true}}}' \
         -X PATCH \
         https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[MASTER_INSTANCE_NAME]
    
  3. 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.

    ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         --data '{"masterInstanceName": "[MASTER_INSTANCE_NAME]", "name": "[REPLICA_NAME]",
                  "region": "[MASTER_REGION]", "databaseVersion": [MASTER_DATABASE_VERSION],
                  "settings": {"tier":"[MACHINE_TYPE]", "replicationType": "SYNCHRONOUS"}}' \
         -X POST \
         https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances
    

Create a read replica for a First Generation master:

Console (1st Gen)

  1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

    Go to the Cloud SQL Instances page

  2. Find the instance you want to create a replica for, and open its more actions menu at the far right of its listing.

    Image of three-dot menu

  3. Select Create replica.
  4. If the instance had backups and binary logging enabled, continue with step 7. Otherwise, enable backups and binary logging and click Continue.

    Image of stepper as you enable backups

  5. Click Save and restart to enable binary logging and restart the instance.

    Enabling binary logging causes the instance to be restarted.

  6. If the instance did not have any backups, you must now wait for a backup to be created.

    It could take up to 24 hours for the backup to be created, depending on the backup window of the instance.

  7. In the Create read replica page, update the instance ID if needed, and any other settings as required.
  8. Click Create.

    Cloud SQL creates the replica, and you are returned to the instance page for the master.

gcloud (1st Gen)

  1. Check the status of the master instance:
    gcloud sql instances describe [MASTER_INSTANCE_NAME]

    If the databaseReplicationEnabled property is true, the instance is a replica; you cannot create a replica of a replica.

  2. If the enabled property under backupConfiguration is false, enable backups for the master instance now:
    gcloud sql instances patch [MASTER_INSTANCE_NAME] --backup-start-time [HH:MM]
    The backup-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 the binaryLogEnabled property is false, enable binary logs:
    gcloud sql instances patch --enable-bin-log [MASTER_INSTANCE_NAME]
    Enabling binary logs causes the instance to be restarted.
  4. Confirm that at least one successful backup exists:
    gcloud sql backups list --instance [MASTER_INSTANCE_NAME]

    If this command returns one or more backups, proceed to the next step. Otherwise, you must wait until a backup is created before proceeding. This could take as long as 24 hours, depending on the backup window of the instance.

  5. Create the replica:
    gcloud sql instances create [REPLICA_NAME] --master-instance-name [MASTER_INSTANCE_NAME] --tier [TIER]
    The tier can be any value appropriate for First Generation instances. The default tier is D1.

cURL (1st Gen)

  1. Use the get method of the instances resource to return the values of the current backup configuration. For example, using cURL, the request is:
    ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         -X GET \
         https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[MASTER_INSTANCE_NAME]?fields=settings
    

    The output contains the backupConfiguration:

    "backupConfiguration": [
     {
        "kind": "sql#backupConfiguration",
        "startTime": "12:00",
        "enabled": false,
        "binaryLogEnabled": false
     }
    
  2. If either enabled or binaryLogEnabled are false, use the patch method of the instances resource to enable them both. Specify the properties of the backup configuration you want to keep as is and those you want to change.

    To enable backups, set enabled to true and the startTime to a value which is the start of the backup window. To enable binary logging, set binaryLogEnabled to true.

    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         --data '{"settings" : {"backupConfiguration" : {"startTime": "[HH:MM]", "enabled": true, "binaryLogEnabled": true}}}' \
         -X PATCH \
         https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[MASTER_INSTANCE_NAME]
    
  3. Check whether the instance has any backups.
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         -X GET \
         https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[MASTER_INSTANCE_NAME]/backupRuns
    

    If no backups are shown, you must wait for a backup to be created before proceeding to the next step. This could take up to 24 hours, depending on your backup window.

  4. Use the insert method of the instances resource to create the read replica. The region must be the same region as the master.

    ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         --data '{"masterInstanceName": "[MASTER_INSTANCE_NAME]", "name": "[REPLICA_NAME]", "region": "[REGION]", \
                  "settings": {"tier":"[TIER]", "replicationType": "ASYNCHRONOUS"}}' \
         -X POST \
         https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances
    

What's next

Send feedback about...

Cloud SQL for MySQL