Quickstart for Cloud SQL for MySQL

This page shows you how to create and connect to a MySQL instance and perform basic SQL operations using the Google Cloud Console and a client. The resources created in this Quickstart typically cost less than a dollar, assuming you complete the steps, including the clean up, in a timely manner.

Set up your project

Before performing the steps in this quickstart, complete the following tasks:

  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 Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Make sure you have the Cloud SQL Client role on your user account.

    Go to the IAM page

  5. Enable the Cloud SQL Admin API.

    Enable the API

Create a Cloud SQL instance

In this procedure, you use the Google Cloud Console. To use the gcloud command-line tool, cURL, or PowerShell, see Creating instances.

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

    Go to Cloud SQL Instances

  2. Click Create Instance.
  3. Click MySQL.
  4. If you're prompted to enable the Compute API, click the Enable API button.
  5. Enter myinstance for Instance ID.
  6. Enter a password for the root user.
  7. Use the default values for the other fields.
  8. Click Create.

    You are returned to the instances list; you can click into the new instance right away to see the details, but it won't be available until it initializes and starts.

    Note: In this example, the instance is created using default settings, including a public IP address.

Connect to your instance using the mysql client in Cloud Shell

  1. In the Google Cloud Console, click the Cloud Shell icon (Cloud Shell icon) in the upper right corner.

    When Cloud Shell finishes initializing, the following appears:

    Welcome to Cloud Shell! Type "help" to get started.
    username@example-id:~$
    
  2. At the Cloud Shell prompt, connect to your Cloud SQL instance:

    gcloud sql connect myinstance --user=root
    
  3. Enter your root password.

    The mysql prompt appears.

Create a database and upload data

  1. Create a SQL database on your Cloud SQL instance:
    CREATE DATABASE guestbook;
    
  2. Insert sample data into the guestbook database:
    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", "I got here!");
        INSERT INTO entries (guestName, content) values ("second guest", "Me too!");
    
  3. Retrieve the data:
    SELECT * FROM entries;
    
    The result is:
    +--------------+-------------------+---------+
    | guestName    | content           | entryID |
    +--------------+-------------------+---------+
    | first guest  | I got here!       |       1 |
    | second guest | Me too!           |       2 |
    +--------------+-------------------+---------+
    2 rows in set (0.00 sec)
    mysql>
    

Cleanup

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

    Go to Cloud SQL Instances

  2. Select the your instance's name to open the Overview page.
  3. In the icon bar at the top of the page, click Delete.
  4. In the Delete instance window, type your instance's name, then click Delete to delete the instance.

    You cannot reuse an instance name for about 7 days after the instance is deleted.

What's next

Based on your needs, you can learn more about creating Cloud SQL instances.

You also can learn about creating MySQL users and databases for your Cloud SQL instance.

Also see the Cloud SQL pricing information.

In this quickstart you connected to the instance using Cloud Shell. In most use cases, how you connect depends on your networking configuration, such as:

  • If your Cloud SQL instance has a public or private IP address.
  • If you're connecting from inside Google Cloud (from the same project or from a different project).
  • If you're connecting from outside of Google Cloud.

Learn more about:

Additionally, you can learn about connecting to a Cloud SQL instance from other Google Cloud applications: