Powered By GitBook
Set up a MySQL database with Cloud SQL
Set up a MySQL database with Cloud SQL on Google Cloud.
</walkthrough-tutorial-duration>
Take the interactive version of this tutorial, which runs in the Cloud Console:
​​
​
​
</walkthrough-alt>

Overview

In this tutorial, you will do the following:
    1.
    Create a new MySQL instance.
    2.
    Set up and query a database.
    3.
    Clean up the resources used in the tutorial.

Project setup

Google Cloud organizes resources into projects. This allows you to collect all of the related resources for a single application in one place.
Begin by creating a new project or selecting an existing project for this tutorial.
</walkthrough-project-billing-setup>
For details, see Creating a project.

Create a MySQL instance

    1.
    Open the Navigation menu in the upper-left corner of the console, and then select SQL.
    </walkthrough-menu-navigation>
    2.
    Click Create instance</walkthrough-spotlight-pointer>.
    3.
    Click Choose MySQL.
    4.
    Configure the instance.
    The default configuration should be sufficient.
    Make note of these values; you'll use them later:
      Instance ID</walkthrough-spotlight-pointer>
      Root password</walkthrough-spotlight-pointer>
      (or choose
      No password</walkthrough-spotlight-pointer>)
    5.
    Click Create</walkthrough-spotlight-pointer>.

Connect to the SQL instance

In this tutorial, you do much of your work in Cloud Shell, which is a built-in command-line tool for the Cloud Console.
Open Cloud Shell by clicking the </walkthrough-cloud-shell-icon>Activate Cloud Shell button in the navigation bar in the upper-right corner of the console.
Use this command to connect to the instance, replacing [INSTANCE_ID] with the instance ID that you noted in the previous step:
1
gcloud sql connect [INSTANCE_ID] --user=root
Copied!
You should see a prompt similar to the following:
1
MySQL [(none)]
Copied!

Using the SQL instance

At the MySQL prompt, run the following commands:
    1.
    Create the database and table:
    1
    CREATE DATABASE geography;
    2
    USE geography;
    3
    CREATE TABLE cities (city VARCHAR(255), country VARCHAR(255));
    Copied!
    2.
    Insert data:
    1
    INSERT INTO cities (city, country) values ("San Francisco", "USA");
    2
    INSERT INTO cities (city, country) values ("Beijing", "China");
    Copied!
    3.
    Query:
    1
    SELECT * FROM cities;
    Copied!

Cleanup

The last step is to clean up the newly created resources.
    1.
    Open the Navigation menu in the upper-left corner of the console, and then select SQL.
    </walkthrough-menu-navigation>
    2.
    Click the Instance ID</walkthrough-spotlight-pointer> link to go to Instance details page.
    3.
    Click the Delete button at the top of the page.

Congratulations!

</walkthrough-conclusion-trophy>
You've successfully set up a MySQL database!
Here are some next steps that you can take:
Last modified 6mo ago