Google Cloud Platform
Cloud SQL

Getting Started with Google Cloud SQL

This page describes how to get started with Google Cloud SQL by:

Creating a Google Cloud SQL instance

Console (2nd Gen)


  1. Go to the Google Cloud Platform Console.
  2. Select an existing project and select Continue, or create a new project.
  3. Click CREATE INSTANCE.
  4. Click Choose Second Generation.
  5. Enter a Name.
  6. Select values for the following:
    • Zone
    • Machine type
    • Storage capacity
    • Backups and binary logging
    • Maintenance window
  7. To specify one or more authorized networks for the instance, click add Bulk add. and specify their IP addresses.

    You can also configure a proxy, which does not require granting access to specific IP addresses. For more information, see Configuring a Proxy.

  8. In the advanced options section, you can set specific MySQL flags for this instance. Learn more.
  9. After the instance is created, select it from the instance list and click Access Control then Users to set the password for 'root'@'%'.

gcloud (2nd Gen)


  1. Install the Cloud SDK if you haven't already.
  2. Authenticate with Google Cloud Platform:
    gcloud auth login
  3. Set your default project:
    gcloud config set project YOUR_PROJECT_ID
  4. Use the following command to create an instance:
    gcloud sql instances create \
      --activation-policy=ALWAYS \
      --tier=TIER \
      --pricing-plan=PACKAGE \
      --region=PREFERRED_REGION \
      --gce-zone=PREFERRED_GCE_ZONE \
      --database-version=MYSQL_5_6 \
      --backup-start-time= \
      YOUR_INSTANCE_ID
    

    Where:

    • TIER is one of the values from gcloud sql tiers list. You can use any value that starts with db-n1_.
    • PREFERRED_REGION is asia-east1, europe-west1, or us-central.
    • PREFERRED_GCE_ZONE is one of the values from gcloud compute zones list.

    YOUR_INSTANCE_ID will be automatically combined with your project name, and if applicable, a domain name as follows:

    • Non-domain: YOUR_PROJECT_NAME:YOUR_INSTANCE_ID
    • Domain-specific (older project): YOUR_DOMAIN:YOUR_PROJECT_NAME:INSTANCE_NAME
  5. Modify the instance and grant access to your IP address:
    gcloud sql instances patch YOUR_INSTANCE_NAME --authorized-networks YOUR_IP_ADDRESS
    
  6. Set the root password for the password for "root@%" user account:
    gcloud sql instances set-root-password YOUR_INSTANCE_NAME --password-file PASSWORD_FILE
    
  7. An IPv4 address is automatically assigned to the instance upon creation. You can retrieve it with:
    gcloud sql instances describe YOUR_INSTANCE_NAME
    

    In the output, find the "ipAddress" field. Use this as the IP address your applications or tools use to connect to the instance.

Console (1st Gen)

  1. Go to the Google Cloud Platform Console.
  2. Select an existing project and select Continue, or create a new project.
  3. Click CREATE INSTANCE.
  4. Click Choose First Generation to create a new Cloud SQL instance in your project.
  5. Enter a name for the instance. The instance name will be automatically combined with your project name, and if applicable, a domain name as follows:
    • Non-domain: YOUR_PROJECT_NAME:YOUR_INSTANCE_NAME
    • Domain-specific (older projects): YOUR_DOMAIN:YOUR_PROJECT_NAME:YOUR_INSTANCE_NAME
  6. Click Create.
  7. After the instance is running, select the instance, and then click Access Control to finish configuring the instance.
    1. In the IP Address pane, click Request IPv4 address and use this as the IP address your applications or tools use to connect to the instance.
    2. In the Users pane, click New User and create a user with name root and specify a password. This will create the MySQL account 'root'@'%'. For more information see Creating Users.

      You must create one account before you can connect to the instance using the standard MySQL protocol.

    3. In the Authorization pane, in the Authorized Networks section, click add Bulk add. and add your IP address.

gcloud (1st Gen)

  1. Install the Cloud SDK or use the Cloud SDK installed with Google Cloud Shell, a command line environment you can use in the Google Cloud Platform Console.
  2. Use the following command to create an instance:

    $ gcloud sql instances create YOUR_INSTANCE_NAME
    

    The instance name will be automatically combined with your project name, and if applicable, a domain name as follows:

    • Non-domain: YOUR_PROJECT_NAME:YOUR_INSTANCE_NAME
    • Domain-specific (older project): YOUR_DOMAIN:YOUR_PROJECT_NAME:YOUR_INSTANCE_NAME
  3. Modify the instance and grant access to your IP address:
    $ gcloud sql instances patch YOUR_INSTANCE_NAME --authorized-networks YOUR_IP_ADDRESS
    
  4. Set the root password for the instance:
    $ gcloud sql instances set-root-password YOUR_INSTANCE_NAME --password-file PASSWORD_FILE
    

    You must set a password before you can connect to the instance using the standard MySQL protocol.

  5. Assign an IP address to the instance and retrieve it:
    $ gcloud sql instances patch YOUR_INSTANCE_NAME --assign-ip
    $ gcloud sql instances describe YOUR_INSTANCE_NAME
    

    In the output, find the "ipAddress" field. Use this as the IP address your applications or tools use to connect to the instance.

That's it! You have created your Google Cloud SQL instance and you configured it with an IP address so you can connect to it. For instructions on migrating existing instances to use MySQL 5.6, see Migrating from MySQL 5.5 to MySQL 5.6.

Connecting to an instance

MySQL Client

  1. Install the MySQL Client.

  2. Connect to the instance and use the IP address you created for it.

    $ mysql --host=INSTANCE_IP --user=root --password

  3. Create a database and table, enter some data, and query the data.

    CREATE DATABASE guestbook;
    USE guestbook;

    CREATE TABLE entries (guestName VARCHAR(255), content VARCHAR(255), entryID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(entryID)); INSERT INTO entries (guestName, content) values ("first guest", "hello world!");

    SELECT * FROM entries;

    For more information about connecting using the MySQL Client, including how to connect with SSL, see Using MySQL Client.

gcloud

  1. Start Google Cloud Shell from the Google Cloud Platform Console.

    For more information, see the Cloud Shell Quickstart.

  2. Use the gcloud beta sql connect command to connect to your instance.

    $ gcloud beta sql connect YOUR_INSTANCE_NAME --user root
    This command authorizes the IP that the Cloud Shell connects from for a short period of time, which should be sufficient for trying out the commands below.

  3. Create a database and table, enter some data, and query the data.

    CREATE DATABASE guestbook;
    USE guestbook;

    CREATE TABLE entries (guestName VARCHAR(255), content VARCHAR(255), entryID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(entryID)); INSERT INTO entries (guestName, content) values ("first guest", "hello world!");

    SELECT * FROM entries;

Configuring an instance

For information about managing your Google Cloud SQL instances, see:

Working with your instance

You can access and work with your Google Cloud SQL instances in the following ways: