Hide
Google Cloud SQL

Managing Instances Using the Cloud SDK

This page shows you how to use the Google Cloud SDK to manage your Cloud SQL instances. The Cloud SDK provides tools that wrap the Google Cloud SQL API and simplify common operations for managing Cloud SQL instances.

You can also use the Google Developers Console to manage your instances. For more information, see Setting Up Instances.

Getting started with the Cloud SDK command line

To start with the Google Cloud SDK, you should review the Cloud SDK Getting Started Documentation. After installing the Cloud SDK, you should check that the Cloud SQL command-line is installed. You can do that with this command:

$ gcloud components list

If the Cloud SQL is not installed, the following command will install it. If it is installed, the following command will check for updates.

$ gcloud components update sql

Once the Cloud SQL is installed, you can get help for the tool, resources, and commands by using the --help flag:

$ gcloud sql --help
$ gcloud sql instances --help
$ gcloud sql instances create --help

The help displayed with the `--help` flag is also available in the Google Cloud SDK reference for gcloud sql.

Finally, you should configure a project as a your default project with two commands. The first command below lists the configuration for gcloud, including any default project already set. The second command sets the default project.

$ gcloud config list
$ gcloud config set project YOUR_PROJECT_ID
Back to top

Basic instance tasks

Creating an instance

The following commands show how to create a D2 instance, and then retrieve information about the instance.

$ gcloud sql instances create YOUR_INSTANCE_NAME --tier D2
$ gcloud sql instances describe YOUR_INSTANCE_NAME

Editing an instance

The following example commands show you how to display what options are available for editing an instance, and how to edit an instance YOUR_INSTANCE_NAME. In this example, the instance backup start time, pricing plan, and tier are changed.

$ gcloud sql instances patch --help
$ gcloud sql instances patch YOUR_INSTANCE_NAME --backup-start-time 16:00 --pricing-plan PACKAGE --tier D2

The following command sets the activation policy for an instance to ALWAYS, which means that the instance will always be active. For the Per Use Billing Plan, setting the activation policy of an instance to ALWAYS will impact how the usage is charged for the instance. For more information, see How is use of my instance calculated?.

$ gcloud sql instances patch YOUR_INSTANCE_NAME --activation-policy ALWAYS

Restarting an instance

The following commands restart an instance, YOUR_INSTANCE_NAME, and then query to check the instance state. When the instance state is "RUNNABLE", the instance is ready to use again.

$ gcloud sql instances restart YOUR_INSTANCE_NAME
$ gcloud sql instances describe YOUR_INSTANCE_NAME

Deleting an instance

The following command shows how to delete an instance.

$ gcloud sql instances delete YOUR_INSTANCE_NAME
Back to top

Getting information about tiers and instances

Listing instances in a project

The following commands set the default project and then list the instances for the default project. If you already have set the default project, you can skip the first command.

$ gcloud config set project YOUR_PROJECT_ID
$ gcloud sql instances list

The output from the gcloud sql instances list command provides a basic instance listing that may be changed in future releases of gcloud sql. If you are scripting output from gcloud sql instances list, use the machine-readable output formats, which are returned by using --format json, --format text, or --format yaml.

Getting information about one instance

To get information about one instance, use the following command:

$ gcloud sql instances describe YOUR_INSTANCE_NAME

For an instance in another project to which you have access, you can get information about it by using the --project flag.

$ gcloud sql instances describe --project OTHER_PROJECT_ID INSTANCE_NAME

Similar to listing instances, if you are scripting output from gcloud sql instances describe, use the machine-readable output formats, which are returned by using --format json, --format text, or --format yaml.

Listing the available tiers

The following command lists the available tiers. The tiers returned (for example, "D2", "D4") can be used with the --tier flag for creating or editing an instance.

$ gcloud sql tiers list
Back to top

Importing and exporting data

Importing data into an instance

The Cloud SQL command line supports importing a SQL dump file from a Google Cloud Storage bucket into an instance. The following command imports a dump file from the bucket location gs://yourbucket/sqldumpfile.gz into the instance YOUR_INSTANCE_NAME.

$ gcloud sql instances import YOUR_INSTANCE_NAME gs://yourbucket/sqldumpfile.gz

See the import command help (gcloud sql instances import --help) for information about importing data from a specific database in the dump file.

Exporting data from an instance

The Cloud SQL command line supports exporting a SQL dump file from an instance to a Google Cloud Storage bucket. The following command exports a dump file from the instance YOUR_INSTANCE_NAME to the bucket location gs://yourbucket/sqldumpfile.gz.

$ gcloud sql instances export YOUR_INSTANCE_NAME gs://yourbucket/sqldumpfile.gz

See the export command help (gcloud sql instances export --help) for information about exporting data from a specific database or table.

Back to top

Security-related tasks

Creating a SSL cert

Google Cloud SQL supports connecting to an instance using the Secure Socket Layer (SSL) protocol. After you create an instance, we recommend that you configure it so that you can connect to it using SSL.

The following command creates an SSL certificate:

$ gcloud sql ssl-certs --instance YOUR_INSTANCE_NAME create CERT_NAME CERT_FILE

To retrieve the server public key certificate you just created, use the following command:

$ gcloud sql ssl-certs --instance YOUR_INSTANCE_NAME describe CERT_NAME

To get the server certificate, use the following command:

$ gcloud sql instances describe YOUR_INSTANCE_NAME

After executing these commands, you have:

  • A server private key.
  • A server public key certificate.
  • A server certificate.

With these three items, you can connect to your instance using SSL. For examples, see Connecting Using MySQL Client and Admin and Reporting Tools.

Assigning an IPv4 address

The following command assigns an IPv4 address to an instance. All newly created instances have an IPv6 address assigned by default. If you need to connect to the instance with an IPv4 address, you must assign one. For more information, see Configuring access control for IP connections.

$ gcloud sql instances patch YOUR_INSTANCE_NAME --assignip

Working with operations and backups

Operations

To list all operations for an instance:

$ gcloud sql operations --instance YOUR_INSTANCE_NAME list

To list a specific operation, you must provide its operation identifier. The operation identifier will look like similar to this "6b4d166e-90c7-4b49-a616-cc9e9d71b14c" and can be found from the operations list command.

$ gcloud sql operations --instance YOUR_INSTANCE_NAME describe OPERATION_ID

To wait for one or more operations, you can use the operations wait command.

$ gcloud sql operations --instance YOUR_INSTANCE_NAME wait OPERATION_ID

Backups

To list all backup runs for an instance:

$ gcloud sql backups --instance YOUR_INSTANCE_NAME list

To list a specific backup run, specify its dueTime. The dueTime will look similar to this "2013-10-25T01:00:00.397Z" and can be found from the backups list command.

$ gcloud sql backups --instance YOUR_INSTANCE_NAME describe DUE_TIME

For more information, see Backups.

Point-in-time recovery

To recover an instance to point-in-time, you must enable backups and binary logging. In addition, there must be at least one successful backup for the instance after enabling binary logging before you can recover to a point-in-time. For more information, see Point-in-Time Recovery.

Enable binary logging for an instance.

$ gcloud sql instances patch --enable-bin-log YOUR_INSTANCE_NAME

Restore from the instance up to the latest state.

$ gcloud sql instances clone SOURCE_INSTANCE TARGET_INSTANCE

Read replicas

To configure a Cloud SQL instance as a replica of another Cloud SQL instance, you must enable the binary log for the master instance and have at least one successful backup of the master instance after binary logging was enabled. For more information, including how to set up external read replicas, see Configuring Replication.

Create a Cloud SQL that replicates from a Cloud SQL master instance.

$ gcloud sql instances create READ_REPLICA_NAME --master-instance-name MASTER_INSTANCE_NAME

Return information about the read replica, including replication state.

$ gcloud sql instances describe READ_REPLICA_NAME

Return information about a master instance, including a list of replicas.

$ gcloud sql instances describe MASTER_INSTANCE_NAME