Legacy configuration for high availability

This page describes the legacy configuration for a MySQL instance for high availability.

In a legacy HA configuration, a Cloud SQL for MySQL instance uses a failover replica to add high availability to the instance. This feature isn't available in Google Cloud console. The new configuration doesn't use failover replicas. Instead, it uses Google's regional persistent disks, which synchronously replicate data at the block-level between two zones in a region.

For more information about the current high availability configuration, see Overview of the High Availability Configuration.

Update an instance from legacy to current high availability

To update your instance from the legacy to the new high-availability configuration, do the following:

  1. Delete the failover replica on the MySQL instances you want to update. See Disabling high availability on an instance.
  2. Configure the instance to use the current version of high availability. See Configuring an existing instance for high availability.

Legacy configuration: Create a new instance configured for high availability

When you create an instance, you can configure it for high availability; Cloud SQL creates the failover replica at the same time that it creates the primary. The legacy feature isn't available in the Google Cloud console. Instead, use gcloud CLI or cURL commands.

To create an instance configured for high availability:

gcloud

  1. Create the primary instance and its failover replica:
    gcloud sql instances create PRIMARY_INSTANCE_NAME \
    --backup-start-time=BACKUP_WINDOW_START_TIME \
    --failover-replica-name=FAILOVER_REPLICA_NAME \
    --cpu=CPU \
    --database-version=DATABASE_VERSION \
    --memory=MEMORY \
    --enable-bin-log
    

    The backup window start time is in the format HH:MM.

    Make sure that you replace the DATABASE_VERSION placeholder with MySQL 5.6 or MySQL 5.7 (--database-version=MYSQL_5_6 or
    --database-version=MYSQL_5_7).

    If you don't specify a version for the database-version parameter, then MySQL 8.0 is selected, by default. The legacy configuration for high availability for MySQL doesn't support version 8.0.

    For a complete list of available parameters, see the gcloud sql instances create reference page.

  2. Configure the root user on the primary instance:
    gcloud sql users set-password root --host=% \
    --instance PRIMARY_INSTANCE_NAME \
    --password PASSWORD
    

    The failover replica is created with the same CPU and MEMORY as the primary instance. You can change the failover replica's CPU and MEMORY later, but it must be at least as large as the primary instance.

curl

  1. Create the primary instance and its failover replica:
    gcloud auth login
    ACCESS_TOKEN="$(gcloud auth print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         --data '{"name":"PRIMARY_INSTANCE_NAME", "region":"REGION",
                  "settings": {
                               "tier":"MACHINE_TYPE",
                               "backupConfiguration": {"binaryLogEnabled": true, "enabled": true}},
                               "failoverReplica": {"name": "FAILOVER_REPLICA_NAME"}}' \
         -X POST \
         https://www.googleapis.com/sql/v1beta4/projects/PROJECT-ID/instances
    
    For the complete list of parameters for the request, see the instances:insert page.
  2. When the primary instance finishes initializing, update the root password on the primary instance:
    gcloud auth login
    ACCESS_TOKEN="$(gcloud auth print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         --data '{"name": "root", "host": "%", "password": "ROOT_PASSWORD"}' \
         'https://www.googleapis.com/sql/v1beta4/projects/PROJECT-ID/instances/INSTANCE_NAME/users?host=%25&name=root'
    
    This change is automatically propagated to the replica.

Legacy configuration: Configure an existing instance for high availability

Configuring an existing instance for high availability causes a few minutes of downtime while the instance is reconfigured.

To configure an existing instance for high availability:

gcloud

  1. Check the status of the primary instance:
    gcloud sql instances describe PRIMARY_INSTANCE_NAME
    

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

  2. If the enabled property under backupConfiguration is false, enable backups for the primary instance now:
    gcloud sql instances patch PRIMARY_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 PRIMARY_INSTANCE_NAME \
    --enable-bin-log
    

    Enabling binary logs causes the instance to be restarted.

  4. Create the replica:
    gcloud sql instances create FAILOVER_REPLICA_NAME \
    --master-instance-name=PRIMARY_INSTANCE_NAME \
    --replica-type=FAILOVER
    

curl

  1. Check the status of the primary instance:
    gcloud auth login
    ACCESS_TOKEN="$(gcloud auth print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         -X GET \
         https://www.googleapis.com/sql/v1beta4/projects/PROJECT-ID/instances/PRIMARY_INSTANCE_NAME?fields=settings
    

    The output will contain the backupConfiguration, for example:

    "backupConfiguration": {
        "kind": "sql#backupConfiguration",
        "startTime": "12:00",
        "enabled": true,
        "binaryLogEnabled": true
    }
    
  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.

    gcloud auth login
    ACCESS_TOKEN="$(gcloud auth print-access-token)"
    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/PRIMARY_INSTANCE_NAME/
    
  3. Use the insert method of the instances resource to create the failover replica.

    gcloud auth login
    ACCESS_TOKEN="$(gcloud auth print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         --data '{"name": "REPLICA_NAME", "masterInstanceName": "PRIMARY_INSTANCE_NAME",
                  "region": "PRIMARY_IMSTANCE_REGION", "databaseVersion": "PRIMARY_DATABASE_VERSION",
                  "replicaConfiguration": {"failoverTarget": true},
                  "settings": {"tier":"MACHINE_TYPE"}}' \
         -X POST \
         https://www.googleapis.com/sql/v1beta4/projects/PROJECT-ID/instances
    

    MACHINE_TYPE must be at least as large as the machine type of the primary instance.

Initiate failover

Testing failover is optional, but is recommended so that you can see how your application responds in the event of a failover.

To learn more about failovers, see the Failover overview.

gcloud

Initiate the failover:

gcloud sql instances failover PRIMARY_INSTANCE_NAME

curl

  1. Describe the primary instance to get the value of the settingsVersion field.
    gcloud auth login
    ACCESS_TOKEN="$(gcloud auth print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         -X GET \
         https://www.googleapis.com/sql/v1beta4/projects/PROJECT-ID/instances/PRIMARY_INSTANCE_NAME
    
  2. Initiate the failover:
    gcloud auth login
    ACCESS_TOKEN="$(gcloud auth print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         --data '{"failoverContext":{"settingsVersion":"SETTINGS_VERSION"}}' \
         -X POST \
         https://www.googleapis.com/sql/v1beta4/projects/PROJECT-ID/instances/PRIMARY_INSTANCE_NAME/failover
    

The instance fails over and is not available to serve data for a few minutes.

Legacy configuration: Create an alert for replication lag

The time required for a failover operation depends on the amount of replication lag when the failover is initiated. You can use Cloud Monitoring in the Google Cloud console to alert you when replication lag exceeds a threshold.

For more information about replication lag, search for "seconds_behind_master" on the SHOW SLAVE STATUS Syntax page in the MySQL documentation.

For more information about Cloud Monitoring, see the Cloud Monitoring documentation.

Legacy configuration: Set an alert for a specific failover replica

  1. In the navigation panel of the Google Cloud console, select Monitoring, and then select Alerting.
  2. Click Create Alerting Policy.
  3. Enter a descriptive name for your alert.
  4. Select Metric Threshold and click Next to open the Target tab.
  5. For Resource Type, select Cloud SQL.
  6. For Applies To, select Single, then select your failover replica.
  7. Click Next to open the Configuration tab.
  8. Enter the following values:

    Field Value
    If Metric Seconds Behind Master
    Condition above
    Thresholds 120 (or whatever value is appropriate for your environment)
    For 3 minutes

  9. Select your Notification methods, and click Save Condition.

Legacy configuration: Set an alert for a group of failover replicas

If you plan to create multiple failover replicas in the same project, it could be easier to set the alert for the entire group, rather than on each failover replica individually. To create a group for monitoring, use a suffix for the failover replicas' names, such as "-failover". Make sure you don't use this suffix for any other types of Cloud SQL instances.

Create your failover replica group:

  1. In the Google Cloud console, go to the Monitoring page.

    Go to Monitoring

  2. From the upper menu bar, select Groups > Create....
  3. Enter a descriptive name for your group.
  4. Leave Name and Contains selected, and enter your name substring.
  5. Click Save Group. The summary page for the new group opens.
  6. Confirm that the expected instances are included in the group.

Create the alert for the failover replica group:

  1. In the navigation panel of the Google Cloud console, select Monitoring, and then select Alerting.
  2. Click Create Alerting Policy.
  3. Enter a descriptive name for your group alert.
  4. Select Metric Threshold and click Next to open the Target tab.
  5. For Resource Type, select Cloud SQL.
  6. For Applies To, select Group, then select your failover group.
  7. Leave Any Member Violates selected and click Next to open the Configuration tab.
  8. Enter the following values:

    Field Value
    If Metric Seconds Behind Master
    Condition above
    Thresholds 120 (or whatever value is appropriate for your environment)
    For 3 minutes

  9. Select your desired Notification methods, and click Save Condition.

Legacy configuration: Disable high availability on an instance

You need to delete the failover replica instance to remove high availability from the primary instance.

Before you perform this procedure, make sure there are no operations currently running on the primary instance.

To disable high availability:

Console

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

    Go to Cloud SQL Instances

  2. Select the failover instance associated with the instance you want to disable high availability on.
  3. On the Instance details page, click Delete.
  4. In the Delete Replica window, retype the name of failover instance.
  5. Click Delete. The configuration for the original instance changes to zonal.

gcloud

For reference information, see gcloud sql instances delete.
gcloud sql instances delete FAILOVER_REPLICA_NAME

curl

The following request uses the instances:delete method to delete the instance.
gcloud auth login
ACCESS_TOKEN="$(gcloud auth print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
     --header 'Content-Type: application/json'\
      -X DELETE\
https://www.googleapis.com/sql/v1beta4/projects/PROJECT-ID/instances/INSTANCE_NAME

What's next