Configure new and existing instances for IAM database authentication

This page has procedures for creating or editing Cloud SQL instances to allow users or service accounts that are configured to use Cloud SQL IAM database authentication. To learn more about the Cloud SQL IAM integration, see IAM authentication.

A newly-created instance has four system databases:

  • information_schema: Provides access to database metadata, information about the MySQL server.
  • mysql: The system schema. It contains tables that store information required by the MySQL server as it runs.
  • performance_schema: A feature for monitoring MySQL Server execution at a low level.
  • sys: Contains a set of objects that helps DBAs and developers interpret data collected by the performance schema.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Install the Google Cloud CLI.
  5. To initialize the gcloud CLI, run the following command:

    gcloud init
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. Make sure you have the Cloud SQL Admin and Compute Viewer roles on your user account.

    Go to the IAM page

    Learn more about roles and permissions.

Configure new instances for IAM database authentication

Cloud SQL uses a flag to enable and disable IAM user connections on an instance. In this procedure, you enable that flag.

To configure a new instance that uses Cloud SQL IAM database authentication:

Console

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

    Go to Cloud SQL Instances

  2. Click Create Instance.
  3. Click Choose MySQL.
  4. Enter a name for the Instance ID. Don't include sensitive or personally identifiable information in your instance name; it's externally visible. You don't need to include the project ID in the instance name. The project ID is included automatically where appropriate (for example, in the log files).
  5. Enter a password for the root user.
  6. From the Database version drop-down menu, select a database version.
  7. In the Choose region and zonal availability section, select the region and zone for your instance. Place your instance in the same region as the resources that access it. The region you select can't be modified in the future. Usually, you don't need to specify a zone.
  8. In the Customize your instance section, click Show Configuration Options, and then expand Flags.
  9. Click Add Flag.
  10. From the Choose a flag drop-down menu, select the cloudsql_iam_authentication flag. Make sure that On is selected as the value for this flag, and then click Done.
  11. Configure other instance settings, as needed. For more information about settings, see Settings.
  12. Click Create Instance.

gcloud

Run gcloud sql instances create with the --database-flags parameter set to cloudsql_iam_authentication=on.

Replace the following:

  • INSTANCE_NAME: The name of the new instance.
  • MYSQL_VERSION: The MySQL version (such as MYSQL_5_7 or MYSQL_8_0).
  • NUMBER_OF_CORES: The number of cores in the machine.
  • AMOUNT_OF_MEMORY: The amount of memory in the machine. A size unit should be provided (such as, 3072MiB or 9GiB).
  • ZONE: Preferred Compute Engine zone (such as us-central1-a, or us-central1-b).
  • PASSWORD: Create a password for the root user.
gcloud sql instances create INSTANCE_NAME \
--database-version=MYSQL_VERSION \
--cpu=NUMBER_OF_CORES \
--memory=AMOUNT_OF_MEMORY \
--zone=ZONE_NAME \
--root-password=PASSWORD \
--database-flags=cloudsql_iam_authentication=on

Terraform

To create an instance with IAM database authentication enabled, use a Terraform resource.

resource "google_sql_database_instance" "default" {
  name             = "mysql-db-auth-instance-name-test"
  region           = "us-west4"
  database_version = "MYSQL_8_0"
  settings {
    tier = "db-f1-micro"
    database_flags {
      name  = "cloudsql_iam_authentication"
      value = "on"
    }
  }
  # 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

Do not include sensitive or personally identifiable information (PII) in your instance name; it is externally visible.

You do not need to include the project ID in the instance name. The project ID is included automatically where needed (for example, in the log files).

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

  • INSTANCE_ID: The desired instance ID
  • REGION: The desired region, such as us-east-1
  • PROJECT_ID: Your project ID
  • LOCATION_ID: The location ID
  • DATABASE_VERSION: Enum string of the database version. For example: MYSQL_8_0
  • PASSWORD: The password for the root user
  • MACHINE_TYPE: Enum string of the machine (tier) type, as: db-custom-[CPUS]-[MEMORY_MBS]

HTTP method and URL:

POST https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/locations/LOCATION_ID/instances

Request JSON body:

{
  "name": "INSTANCE_ID",
  "region": "REGION",
  "databaseVersion": "DATABASE_VERSION",
  "rootPassword": "PASSWORD",
  "settings": {
    "tier": "MACHINE_TYPE",
    "backupConfiguration": {
      "enabled": true
    },
    "databaseFlags": [
      {
        "name": "cloudsql_iam_authentication",
        "value": "on"
      }
    ]
  }
}

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/v1/projects/PROJECT_ID/instances/INSTANCE_ID",
  "status": "PENDING",
  "user": "user@example.com",
  "insertTime": "2020-01-01T19:13:21.834Z",
  "operationType": "CREATE",
  "name": "OPERATION_ID",
  "targetId": "INSTANCE_ID",
  "selfLink": "https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/operations/OPERATION_ID",
  "targetProject": "PROJECT_ID"
}
To see how the underlying REST API request is constructed for this task, see the APIs Explorer on the instances:insert page.

REST v1beta4

Do not include sensitive or personally identifiable information (PII) in your instance name; it is externally visible.

You do not need to include the project ID in the instance name. The project ID is included automatically where needed (for example, in the log files).

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

  • INSTANCE_ID: The desired instance ID
  • REGION: The desired region, such as us-east-1
  • PROJECT_ID: Your project ID
  • LOCATION_ID: The location ID
  • DATABASE_VERSION: Enum string of the database version. For example: MYSQL_8_0
  • PASSWORD: The password for the root user
  • MACHINE_TYPE: Enum string of the machine (tier) type, as: db-custom-[CPUS]-[MEMORY_MBS]

HTTP method and URL:

POST https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/locations/LOCATION_ID/instances

Request JSON body:

{
  "name": "INSTANCE_ID",
  "region": "REGION",
  "databaseVersion": "DATABASE_VERSION",
  "rootPassword": "PASSWORD",
  "settings": {
    "tier": "MACHINE_TYPE",
    "backupConfiguration": {
      "enabled": true
    },
    "databaseFlags": [
      {
        "name": "cloudsql_iam_authentication",
        "value": "on"
      }
    ]
  }
}

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-01T19:13:21.834Z",
  "operationType": "CREATE",
  "name": "OPERATION_ID",
  "targetId": "INSTANCE_ID",
  "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID",
  "targetProject": "PROJECT_ID"
}
To see how the underlying REST API request is constructed for this task, see the APIs Explorer on the instances:insert page.

Configure existing instances for Cloud SQL IAM database authentication

To configure IAM database authentication on an existing instance:

Console

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

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. Click Edit.
  4. In the Customize your instance section, expand Flags.
  5. Click Add Flag.
  6. From the Choose a flag drop-down menu, select the cloudsql_iam_authentication flag. Make sure that On is selected as the value for this flag, and then click Done.
  7. Configure other instance settings, as needed. For more information about settings, see Settings.
  8. Click Save.

gcloud

For information about installing and getting started with the gcloud CLI, see Install the gcloud CLI. For information about starting Cloud Shell, see Use Cloud Shell.

For this procedure, use gcloud sql instances patch.

Replace the following:

  • INSTANCE_NAME: The name of the new instance.
gcloud sql instances patch INSTANCE_NAME \
--database-flags=cloudsql_iam_authentication=on

This resets all other existing database flag settings. For further guidance on setting database flags, see Set a database flag.


REST v1

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

  • PROJECT_ID: Your project ID
  • LOCATION_ID: The location ID
  • INSTANCE_ID: The desired instance ID
  • REGION: The desired region
  • DATABASE_VERSION: Enum string of the database version. For example: MYSQL_8_0
  • PASSWORD: The password for the root user
  • MACHINE_TYPE: Enum string of the machine (tier) type, as: db-custom-[CPUS]-[MEMORY_MBS]

HTTP method and URL:

POST https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/locations/LOCATION_ID/instances

Request JSON body:

{
  "name": "INSTANCE_ID",
  "region": "REGION",
  "databaseVersion": "DATABASE_VERSION",
  "rootPassword": "PASSWORD",
  "settings": {
    "tier": "MACHINE_TYPE",
    "backupConfiguration": {
      "enabled": true
    }
    "databaseFlags":
    [
      {
        "name": "cloudsql_iam_authentication",
        "value": "on"
      }
    ]
  }
}

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/v1/projects/PROJECT_ID/instances/INSTANCE_ID",
  "status": "PENDING",
  "user": "user@example.com",
  "insertTime": "2020-01-01T19:13:21.834Z",
  "operationType": "CREATE",
  "name": "OPERATION_ID",
  "targetId": "INSTANCE_ID",
  "selfLink": "https://sqladmin.googleapis.com/sql/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: Your project ID
  • LOCATION_ID: The location ID
  • INSTANCE_ID: The desired instance ID
  • REGION: The desired region
  • DATABASE_VERSION: Enum string of the database version. For example: MYSQL_8_0
  • PASSWORD: The password for the root user
  • MACHINE_TYPE: Enum string of the machine (tier) type, as: db-custom-[CPUS]-[MEMORY_MBS]

HTTP method and URL:

POST https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/locations/LOCATION_ID/instances

Request JSON body:

{
  "name": "INSTANCE_ID",
  "region": "REGION",
  "databaseVersion": "DATABASE_VERSION",
  "rootPassword": "PASSWORD",
  "settings": {
    "tier": "MACHINE_TYPE",
    "backupConfiguration": {
      "enabled": true
    }
    "databaseFlags":
    [
      {
        "name": "cloudsql_iam_authentication",
        "value": "on"
      }
    ]
  }
}

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-01T19:13:21.834Z",
  "operationType": "CREATE",
  "name": "OPERATION_ID",
  "targetId": "INSTANCE_ID",
  "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID",
  "targetProject": "PROJECT_ID"
}

What's next