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 Managing 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-idBack 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 get 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 get your-instance-name
Deleting an instance
The following command shows how to delete an instance.
$ gcloud sql instances delete your-instance-nameBack to top
Getting information about tiers and instances
Requesting 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
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
Getting information about one instance
To get information about one instance, use the following command:
$ gcloud sql instances get your-instance-name
For an instance in another project to which you have access, you can get information about it by prepending the project identifier with a colon (":") before the instance name.
$ gcloud sql instances get other-project-id:instance-nameBack 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 --uri 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 --uri 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.
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 create --instance your-instance-name cert-name
To retrieve the server public key certificate you just created, use the following command:
$ gcloud sql ssl-certs get --instance your-instance-name cert-name
To get the server certificate, use the following command:
$ gcloud sql instances get 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 .
Setting the root password
The following command sets the MySQL root account password. For a newly created instance, the password is not set. You must set it to access the instance with tools like MySQL Client . The following command enables you to enter the password directly from the commandline:
$ gcloud sql instances set-root-password your-instance-name --password password
The following command enables you to specify a file that contains the password:
$ gcloud sql instances set-root-password your-instance-name --password-file path-to-file
Working with operations and backups
Operations
To list all operations for an instance:
$ gcloud sql operations list --instance your-instance-name
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 get --instance your-instance-name operation-id
Backups
To list all backup runs for an instance:
$ gcloud sql backups list --instance your-instance-name
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 get --instance your-instance-name dueTime
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