This page describes the legacy configuration for a MySQL instance for high availability (HA).
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 recommended 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:
- Delete the failover replica on the MySQL instances you want to update. For more information, see Disable high availability on an instance.
- Configure the instance to use the current version of high availability. For more information, see Configure 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
- 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.
- 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
- 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. -
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
- 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 failover replica for 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
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. - If the
binaryLogEnabled
property isfalse
, enable binary logs:gcloud sql instances patch PRIMARY_INSTANCE_NAME \ --enable-bin-log
Enabling binary logs causes the instance to be restarted.
- Create the replica:
gcloud sql instances create FAILOVER_REPLICA_NAME \ --master-instance-name=PRIMARY_INSTANCE_NAME \ --replica-type=FAILOVER
curl
- 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 }
- If either
enabled
orbinaryLogEnabled
arefalse
, use thepatch
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
totrue
and thestartTime
to a value which is the start of the backup window. To enable binary logging, setbinaryLogEnabled
totrue
.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/
-
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
- 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
- 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
- In the navigation panel of the Google Cloud console, select Monitoring, and then select Alerting.
- Click Create Alerting Policy.
- Enter a descriptive name for your alert.
- Select Metric Threshold and click Next to open the Target tab.
- For Resource Type, select Cloud SQL.
- For Applies To, select Single, then select your failover replica.
- Click Next to open the Configuration tab.
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 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:
-
In the Google Cloud console, go to the Monitoring page.
- From the upper menu bar, select Groups > Create....
- Enter a descriptive name for your group.
- Leave Name and Contains selected, and enter your name substring.
- Click Save Group. The summary page for the new group opens.
- Confirm that the expected instances are included in the group.
Create the alert for the failover replica group:
- In the navigation panel of the Google Cloud console, select Monitoring, and then select Alerting.
- Click Create Alerting Policy.
- Enter a descriptive name for your group alert.
- Select Metric Threshold and click Next to open the Target tab.
- For Resource Type, select Cloud SQL.
- For Applies To, select Group, then select your failover group.
- Leave Any Member Violates selected and click Next to open the Configuration tab.
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 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
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select the failover instance associated with the instance you want to disable high availability on.
- On the Instance details page, click Delete.
- In the Delete Replica window, retype the name of failover instance.
- Click Delete. The configuration for the original instance changes to zonal.
gcloud
For reference information, seegcloud sql instances delete
.
gcloud sql instances delete FAILOVER_REPLICA_NAME
curl
The following request uses theinstances: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
- Learn more about how the high availability configuration works.
- Test how your application responds to lost connections by restarting your instance.
- Learn more about managing your database connections.
- Learn more about Cloud Monitoring.
- Create read replicas for your instance.