This page describes the legacy configuration for a
MySQL instance for high availability. Under the legacy configuration, when an instance
has a failover, it uses a failover replica
instance. The new
configuration does not 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.
Legacy configuration: Creating 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 functionality is not available in the Cloud Console. Instead, use gcloud 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] \ --tier [MACHINE_TYPE] --enable-bin-log
The backup window start time is in the format
HH:MM
.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 machine type (tier) as the primary instance. You can change the failover replica's machine type 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 this 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: Configuring 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 --enable-bin-log [PRIMARY_INSTANCE_NAME]
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 a machine type valid for Second Generation instances and at least as large as the machine type of the primary instance.
Initiating 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.
Console
- Go to the Cloud SQL Instances page in the Google Cloud Console.
- Click the primary instance name to open its Instance details page.
- Click Failover in the button bar.
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: Creating 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 Google Cloud's operations suite 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 Google Cloud's operations suite Monitoring, see the Google Cloud's operations suite Monitoring documentation.
Legacy configuration: Setting an alert for a specific failover replica
- Click the Google Cloud's operations suite logo in the upper-left corner to return to the main Google Cloud's operations suite Monitoring page.
- 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 desired Notification methods, and click Save Condition.
Legacy configuration: Setting 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 do not use this suffix for any other types of Cloud SQL instances.
Create your failover replica group:
- Go to the Cloud SQL Instances page in the Google Cloud Console.
- From the left-hand menu, select Monitoring to open the Google Cloud's operations suite Monitoring page, and log in.
- 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:
- Click the Google Cloud's operations suite logo in the upper-left corner to return to the main Google Cloud's operations suite Monitoring page.
- 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: Disabling 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
- Go to the Cloud SQL Instances page in the Google Cloud Console.
- 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 Google Cloud's operations suite Monitoring.
- Create read replicas for your instance.