Self-service maintenance

This page describes self-service maintenance, which lets you accept a maintenance update on your instance outside of the normal flow of scheduled maintenance.

Overview

Cloud SQL regularly releases software improvements, such as patches for known vulnerabilities, through new maintenance versions. While Cloud SQL schedules maintenance updates once every few months to ensure you have the latest maintenance version, you might want to use self-service maintenance if:

  • You need an update sooner than your next scheduled maintenance event.
  • You want to catch up to the latest maintenance version after skipping your most recent scheduled maintenance event.

You're not required to manually update to the latest version. You can let Cloud SQL automatically update your maintenance version during your next scheduled maintenance event.

Manage maintenance on all read replicas of a primary instance

When you perform self-service maintenance on a primary instance that has read replicas, Cloud SQL automatically applies the maintenance update to all its read replicas. If you have more than three read replica instances, then the instances are divided into groups and updated in parallel. As the final step, the maintenance update is applied to the primary instance.

If necessary, you can still perform self-service maintenance on a single read replica. However, to ensure that all read replica instances and the primary instance operate on the same maintenance version, we recommend that you perform self-service maintenance on all the read replicas of a primary instance at the same time.

To update a single read replica instance, specify that read replica instance when you perform self-service maintenance.

Before you begin

Before you perform self-service maintenance, first check the current maintenance version. Then you can determine the target maintenance version that you want to update to and update the instance to that version. If you use read replicas, then update all the read replicas of the primary instance to that version.

To see the current maintenance version of an instance, use the following procedure:

gcloud

Before you run the following gcloud command, make the following replacement:

  • INSTANCE_ID: The instance to query
gcloud sql instances describe INSTANCE_ID
  

The maintenance version that is installed on the instance is indicated next to the maintenanceVersion attribute.

REST v1

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

  • PROJECT_ID: the ID or project number of the Google Cloud project that contains the instance
  • INSTANCE_NAME: the name of the instance that you're configuring for high availability

HTTP method and URL:

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

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "kind": "sql#instance",
  "state": "RUNNABLE",
  "databaseVersion": "MYSQL_8_0"
}

Look for the maintenanceVersion item in the DatabaseInstance section of the response.

REST v1beta4

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

  • PROJECT_ID: the ID or project number of the Google Cloud project that contains the instance
  • INSTANCE_NAME: the name of the instance that you're configuring for high availability

HTTP method and URL:

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

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "kind": "sql#instance",
  "state": "RUNNABLE",
  "databaseVersion": "MYSQL_8_0"
}

Look for the maintenanceVersion item in the DatabaseInstance section of the response.

Determine the target maintenance version

Use the following method to view the maintenance versions you can select as your target. You can't update to a version earlier than the current maintenance version.

gcloud

Before you run the following gcloud command, make the following replacement:

  • INSTANCE_ID: The instance to query
gcloud sql instances describe INSTANCE_ID
  

The available maintenance version is indicated next to the availableMaintenanceVersions attribute.

REST v1

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

  • PROJECT_ID: the ID or project number of the Google Cloud project that contains the instance
  • INSTANCE_NAME: the name of the instance that you're configuring for high availability

HTTP method and URL:

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

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "kind": "sql#instance",
  "state": "RUNNABLE",
  "databaseVersion": "MYSQL_8_0"
}

Look for the availableMaintenanceVersions item in the DatabaseInstance section of the response.

REST v1beta4

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

  • PROJECT_ID: the ID or project number of the Google Cloud project that contains the instance
  • INSTANCE_NAME: the name of the instance that you're configuring for high availability

HTTP method and URL:

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

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "kind": "sql#instance",
  "state": "RUNNABLE",
  "databaseVersion": "MYSQL_8_0"
}

Look for the availableMaintenanceVersions item in the DatabaseInstance section of the response.

Update all read replicas of a primary instance to the target maintenance version

To perform a maintenance update on all read replica instances of a primary instance, use the following procedure.

gcloud

Before using the following gcloud command, make the following replacement:

  • PRIMARY_INSTANCE_ID: The ID of the primary instance
  • MAINTENANCE_VERSION: The Cloud SQL maintenance version to apply
gcloud sql instances patch PRIMARY_INSTANCE_ID \
   --maintenance-version=MAINTENANCE_VERSION
  

At the prompt for confirmation, type Y to continue, or type N to cancel.

If you select Y, then the update process applies the specified maintenance version to all read replicas first before updating the primary instance. Any read replica instances that have already been updated to the target maintenance version are skipped.

If you select N, then no instances are updated.

After the update of the read replicas and the primary instance completes, a message similar to the following appears:

  Patching Cloud SQL instance....done.
  Patching Cloud SQL instance....done.
  Patching Cloud SQL instance....done.
  Updated [https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/PRIMARY_INSTANCE_ID]
  

Terraform

To create an instance, use a Terraform resource.

resource "google_sql_database_instance" "mysql_ssm_instance_name" {
  name                = "mysql-ssm-instance-name"
  region              = "asia-northeast1"
  database_version    = "MYSQL_5_7"
  maintenance_version = "MYSQL_5_7_38.R20220809.02_00"
  settings {
    tier = "db-f1-micro"
  }
  # 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. Launch Cloud 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 (also called a root module).

  1. In Cloud Shell, create a directory and a new file within that directory. The filename must have the .tf extension—for example main.tf. In this tutorial, the file is referred to as main.tf.
    mkdir DIRECTORY && cd DIRECTORY && 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 created main.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 entering yes 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.

Delete the changes

To delete your changes, do the following:

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

    terraform destroy

REST v1

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

  • PROJECT_ID: The project ID
  • PROJECT_ID: The instance ID of the primary instance
  • MAINTENANCE_VERSION: The Cloud SQL software maintenance version to apply

HTTP method and URL:

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

Request JSON body:

{
  "maintenanceVersion": "MAINTENANCE_VERSION"
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "kind": "sql#operation",
  "targetLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID",
  "status": "PENDING",
  "user": "user@example.com",
  "insertTime": "2020-01-16T02:32:12.281Z",
  "startTime": "08:00",
  "endTime": "08:01",
  "operationType": UPDATE,
  "name": "OPERATION_ID",
  "targetId": "PRIMARY_INSTANCE_ID",
  "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:

  • PROJECT_ID: The project ID
  • INSTANCE_ID: The instance ID of the primary instance
  • MAINTENANCE_VERSION: The Cloud SQL software maintenance version to apply

HTTP method and URL:

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

Request JSON body:

{
  "maintenanceVersion": "MAINTENANCE_VERSION"
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "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-16T02:32:12.281Z",
  "startTime": "08:00",
  "endTime": "08:01",
  "operationType": UPDATE,
  "name": "OPERATION_ID",
  "targetId": "INSTANCE_ID",
  "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID",
  "targetProject": "PROJECT_ID"
}

Update a single instance to the target maintenance version

To perform a maintenance update on a single instance, use the following procedure. If necessary, you can use this procedure to update a single read replica instance.

gcloud

Before using the following gcloud command, make the following replacements:

  • INSTANCE_ID: The ID of the instance. This instance can be a read replica instance.
  • MAINTENANCE_VERSION: The Cloud SQL maintenance version to apply
  gcloud sql instances patch INSTANCE_ID \
     --maintenance-version=MAINTENANCE_VERSION
  

If you specify a read replica, then a prompt for confirmation appears. Type Y to continue or N to cancel.

  • If you select Y, then the update process applies the specified maintenance version only to the specified read replica.
  • If you select N, then no instances are updated.

After the update of the instance completes, a message similar to the following appears:

  Patching Cloud SQL instance....done.
  Updated [https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID]
  

Terraform

To create an instance, use a Terraform resource.

resource "google_sql_database_instance" "mysql_ssm_instance_name" {
  name                = "mysql-ssm-instance-name"
  region              = "asia-northeast1"
  database_version    = "MYSQL_5_7"
  maintenance_version = "MYSQL_5_7_38.R20220809.02_00"
  settings {
    tier = "db-f1-micro"
  }
  # 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. Launch Cloud 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 (also called a root module).

  1. In Cloud Shell, create a directory and a new file within that directory. The filename must have the .tf extension—for example main.tf. In this tutorial, the file is referred to as main.tf.
    mkdir DIRECTORY && cd DIRECTORY && 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 created main.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 entering yes 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.

Delete the changes

To delete your changes, do the following:

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

    terraform destroy

REST v1

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

  • PROJECT_ID: The project ID
  • PROJECT_ID: The instance ID, which can be a read replica instance
  • MAINTENANCE_VERSION: The Cloud SQL software maintenance version to apply

HTTP method and URL:

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

Request JSON body:

{
  "maintenanceVersion": "MAINTENANCE_VERSION"
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "kind": "sql#operation",
  "targetLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID",
  "status": "PENDING",
  "user": "user@example.com",
  "insertTime": "2020-01-16T02:32:12.281Z",
  "startTime": "08:00",
  "endTime": "08:01",
  "operationType": UPDATE,
  "name": "OPERATION_ID",
  "targetId": "PRIMARY_INSTANCE_ID",
  "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:

  • PROJECT_ID: The project ID
  • INSTANCE_ID: The instance ID, which can be a read replica instance
  • MAINTENANCE_VERSION: The Cloud SQL software maintenance version to apply

HTTP method and URL:

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

Request JSON body:

{
  "maintenanceVersion": "MAINTENANCE_VERSION"
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "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-16T02:32:12.281Z",
  "startTime": "08:00",
  "endTime": "08:01",
  "operationType": UPDATE,
  "name": "OPERATION_ID",
  "targetId": "INSTANCE_ID",
  "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID",
  "targetProject": "PROJECT_ID"
}

Troubleshoot

Issue Troubleshooting
Can't apply maintenance update to a stopped instance. To resolve, start the instance before you perform the self-service maintenance update.
Due to a difference in the database major version, the maintenance update failed.

You can't use self-service maintenance to upgrade the database major version of an instance.

To upgrade the major version of an instance, use one of the following procedures:

Due to a difference in the database minor version, the maintenance update failed.

You can't use self-service maintenance to upgrade the database minor version of an instance.

To upgrade the minor version of an instance, follow the steps in Upgrade the database minor version.

What's next