Connect to Cloud SQL for MySQL from your local computer

Learn how to deploy a sample app on your Linux, macOS, or Windows-based local computer connected to a MySQL instance by using the Google Cloud console and a client application.

Assuming that you complete all the steps in a timely manner, the resources created in this quickstart typically cost less than one dollar (USD).

Before you begin

  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.

    Roles required to select or create a project

    • Select a project : Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project : To create a project, you need the Project Creator role ( roles/resourcemanager.projectCreator ), which contains the resourcemanager.projects.create permission. Learn how to grant roles .

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project .

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project : Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project : To create a project, you need the Project Creator role ( roles/resourcemanager.projectCreator ), which contains the resourcemanager.projects.create permission. Learn how to grant roles .

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project .

  6. Verify that you have the permissions required to complete this quickstart .
  7. Enable the Cloud APIs necessary to run a Cloud SQL sample app on your local computer.

    Console

    Click the Enable APIs button to enable the APIs required for this quickstart.

    Enable APIs

    This enables the following APIs:

    • Cloud SQL Admin API
    • IAM API

    gcloud

    Install the gcloud CLI which provides command-line access to your Google Cloud resources. The gcloud CLI is used to run the gcloud CLI commands presented throughout this quickstart. All the commands are formatted to be run in a Linux/macOS terminal or in Windows Powershell.

    Open the terminal and run the following gcloud command:

    gcloud  
    services  
     enable 
      
    sqladmin.googleapis.com  
    iam.googleapis.com

    This command enables the following APIs:

    • Cloud SQL Admin API
    • IAM API

Required roles

To get the permissions that you need to set up Cloud SQL from your local computer, ask your administrator to grant you the following IAM roles on the project that you want to create and connect to:

  • Create or delete an instance, database, and user: Cloud SQL administrator role ( roles/cloudsql.admin ).
  • Create or delete an IAM service account: IAM Service Account administrator role ( roles/iam.serviceAccountAdmin ).
  • Add or remove an IAM policy binding: Project IAM administrator role ( roles/resourcemanager.projectIamAdmin ).
  • Create, manage and rotate service account keys: Service Account Key Admin (roles/iam.serviceAccountKeyAdmin).

For more information about granting roles, see Manage access to projects, folders, and organizations .

You might also be able to get the required permissions through custom roles or other predefined roles .

Set up Cloud SQL

To create an Cloud SQL instance, do the following:

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

    Go to Cloud SQL Instances

  2. Select the MySQL tab and click Create Sandbox instance in the Sandbox box. Alternatively, you can click Create Instance and complete the following steps:
    1. Click Choose MySQL .
    2. Select the Enterprise edition.
    3. Choose the Sandbox preset.
  3. If you're prompted to enable the Compute API , click the Enable API button.
  4. In the Instance info section, enter quickstart-instance as the Instance ID .
  5. Enter a password for the root user. Take note of the password you create, because you'll need it later.
  6. In the Choose region and zonal availability section, select the Single zone option.
  7. Click Create instance .

Create a database

Console

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

    Go to Cloud SQL Instances

  2. Select quickstart-instance .
  3. Open the Databases tab.
  4. Click Create database .
    1. In the Create a database dialog box, enter quickstart_db as the name of the database, and optionally the character set and collation.
    2. Click Create .

gcloud

Run the gcloud sql databases create command to create a database.

gcloud  
sql  
databases  
create  
 quickstart_db 
  
--instance = 
 quickstart-instance 

Create a user

Console

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

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. Select Users from the SQL navigation menu.
  4. Click Add user account .
    • In the Add a user account to instance instance_name page, add the following information:
      • Username : Set to quickstart-user
      • Password : Specify a password for your database user. Make a note of this for use in a later step of this quickstart.
      • In the Host name section, the default is Allow any host , which means that the user can connect from any IP address.

        Optionally, select Restrict host by IP address or address range and enter an IP address or address range in the Host section. The user can then connect only from the IP address or addresses specified.

  5. Click Add .

gcloud

Replace the following:

  1. PASSWORD with a password for your database user. Make a note of this for use in a later step of this quickstart.

Run the gcloud sql users create command to create the user.

gcloud  
sql  
users  
create  
 quickstart-user 
  
--instance = 
 quickstart-instance 
  
--password = 
 PASSWORD 

Username length limits are the same for Cloud SQL as for on-premises MySQL; 32 characters for MySQL 8.0 and later, 16 characters for earlier versions.

Create and configure a Google Cloud service account that has the Cloud SQL Clientrole with permissions to connect to Cloud SQL. After you create a service account, you might need to wait for 60 seconds or more before you use the service account.

Setup development environment for programming language

Set up your local computer's development environment for your preferred programming language.

Go

Complete the following steps to set up your development environment to run the Go sample app.

  1. Go to the setup guide for a Go development environment .
  2. Complete the instructions in the Install Gosection.

Java

Complete the following steps to set up your development environment to run the Java sample app.

  1. Go to the setup guide for a Java development environment .
  2. Complete the instructions in the Install a JDK (Java Development Kit)section.
  3. Complete the instructions in the Install a build automation toolto set up Apache Maven.

Node.js

Complete the following steps to set up your development environment to run the Node.js sample app.

  1. Go to setup guide for a Node.js development environment .
  2. Complete the instructions in the Installing Node.js and npmsection.

Python

Complete the following steps to set up your development environment to run the Python sample app.

  1. Go to setup guide for a Python development environment .
  2. Complete the instructions in the Installing Pythonsection.

Install Git

Install Git , an open source version control system.

Clone a sample app

Clone a sample app to your local computer using the git clone command.

Go

Run the following commands to clone the Go sample app to your local computer and change directory to the directory containing the sample app.

  1. Clone the sample app.
    git  
    clone  
    https://github.com/GoogleCloudPlatform/golang-samples
  2. Change directory to the directory containing the sample app.
     cd 
      
    golang-samples/cloudsql/mysql/database-sql

Java

Run the following commands to clone the Java sample app to your local computer and change directory to the directory containing the sample app.

  1. Clone the sample app.
    git  
    clone  
    https://github.com/GoogleCloudPlatform/java-docs-samples
  2. Change directory to the directory containing the sample app.
     cd 
      
    java-docs-samples/cloud-sql/mysql/servlet

Node.js

Run the following commands to clone the Node.js sample app to your local computer and change directory to the directory containing the sample app.

  1. Clone the sample app.
    git  
    clone  
    https://github.com/GoogleCloudPlatform/nodejs-docs-samples
  2. Change directory to the directory containing the sample app.
     cd 
      
    nodejs-docs-samples/cloud-sql/mysql/mysql

Python

Run the following commands to clone the Python sample app to your local computer and change directory to the directory containing the sample app.

  1. Clone the sample app.
    git  
    clone  
    https://github.com/GoogleCloudPlatform/python-docs-samples
  2. Change directory to the directory containing the sample app.
     cd 
      
    python-docs-samples/cloud-sql/mysql/sqlalchemy

Configure and run sample app

Configure and run the sample app.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

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

    Go to Cloud SQL Instances

  2. Select the quickstart-instance instance to open the Instance detailspage.
  3. In the icon bar at the top of the page, click Delete.
  4. In the Delete instancedialog box, type quickstart-instance , and then click Deleteto delete the instance.

Optional cleanup steps

If you're not using the Cloud SQL clientrole that you assigned to the quickstart-service-account service account, you can remove it.

  1. In the Google Cloud console, go to the Service accounts page.

    Go to IAM

  2. Click the edit icon (which looks like a pencil) for the IAM account named quickstart-service-account.
  3. Delete the Cloud SQL clientrole.
  4. Click Save.

If you're not using the APIs that were enabled as part of this quickstart, you can disable them.

  • APIs that were enabled within this quickstart:
    • Cloud SQL Admin API
    • Identity and Access Management API
  1. In the Google Cloud console, go to the APIs page.

    Go to APIs

  2. Select any API that you would like to disable and then click the Disable APIbutton.

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.

For more information about pricing, see Cloud SQL for MySQL pricing .

Learn more about:

  • Configuring your Cloud SQL instance with a public IP address.
  • Configuring your Cloud SQL instance with a private IP address.

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

Design a Mobile Site
View Site in Mobile | Classic
Share by: