Connecting using a database client from a local machine or Compute Engine

This page describes how to connect a mysql client to your Cloud SQL instance, whether running locally on your client machine, on a Compute Engine VM, or in the Cloud Shell.

Before you begin

Before you can start using a client, you must have:

Using a client on a local machine or a Compute Engine VM

Using a mysql client to connect to your Cloud SQL instance involves three high-level tasks:

  1. Install the client.
  2. Configure access to your Cloud SQL instance.
  3. Connect to your Cloud SQL instance.

Install the client

To install the client:

Debian/Ubuntu

Install the MySQL client from the package manager:

sudo apt-get update
sudo apt-get install mariadb-server-10.3

CentOS/RHEL

Install the MySQL client from the package manager:

sudo yum install mysql

openSUSE

Install the MySQL client from the package manager:

sudo zypper install mysql-server

Other platforms

  1. Download the MySQL Community Server for your platform from the MySQL Community Server download page.
    The Community Server includes the MySQL client.
  2. Install the Community Server, following the directions on the download page.

For more information about installing MySQL, see the MySQL Reference Manual Installing and Upgrading MySQL.

Configure access to your Cloud SQL instance

To configure access to your instance:

  1. From the client machine or Compute Engine VM instance, use What's my IP to see the IP address of the client machine.
  2. Copy that IP address.
  3. Go to the Cloud SQL Instances page in the Google Cloud Console.

    Go to the Cloud SQL Instances page

  4. Click the instance to open its Overview page, and record its IP address.
  5. Select the Connections tab.
  6. Under Authorized networks, click Add network and enter the IP address of the machine where the client is installed.
  7. Click Done. Then click Save at the bottom of the page to save your changes.
  8. Connect to your instance, either with SSL or without SSL.

Connect to your Cloud SQL instance without encryption

To connect to your instance:

  1. Confirm that you have installed the client and configured access to your instance.
  2. Start the mysql client:
    mysql --host=[INSTANCE_IP] --user=root --password
    
  3. Enter your password.
  4. The mysql prompt appears.

Connect to your Cloud SQL instance using SSL

To connect using SSL, you need:

  • A Certificate Authority (CA) certificate in a server-ca.pem file.
  • A client public key certificate in a client-cert.pem file.
  • A client private key in a client-key.pem file.

If you lack a client certificate and a corresponding private key, create a new client certificate.

Before you begin, confirm that you have installed the client and configured access to your instance.

To connect to your instance using SSL:

  1. Start the mysql client:

    mysql --ssl-ca=server-ca.pem --ssl-cert=client-cert.pem --ssl-key=client-key.pem \
                 --host=[INSTANCE_IP] --user=root --password
    

  2. Enter the password.
  3. At the MySQL prompt, enter the \s command to verify that your connection is using SSL.
  4. Look for the output line with SSL:.
    ...
    SSL:                     Cipher in use is DHE-RSA-AES256-SHA
    ...
    

    For information about troubleshooting connection issues over IP, see Diagnosing Issues with Cloud SQL Instances.

Using the client in the Cloud Shell

To connect to a Cloud SQL instance (public IP only):

  1. Go to the Google Cloud Console.

    Go to the Google Cloud Console

  2. Click the Cloud Shell icon towards the right in the tool bar.

    The Cloud Shell takes a few moments to initialize.

  3. At the Cloud Shell prompt, use the built-in client to connect to your Cloud SQL instance:
    gcloud sql connect [INSTANCE_ID] --user=root
    
  4. Enter your password.

The gcloud sql connect command does not support connecting to a Cloud SQL instance using private IP, or using SSL/TLS. To connect with encryption, install and use the proxy in the Cloud Shell:

  1. Install the proxy (Linux 64-bit) in the /home/USER directory.
  2. Start the proxy, using Cloud SDK authentication:

    ./cloud_sql_proxy -instances=INSTANCE_CONNECTION_NAME=tcp:3306 &

  3. Connect to the database, using the TCP connection:

    mysql -u USERNAME -p --host 127.0.0.1

What's next

Try it for yourself

If you're new to Google Cloud, create an account to evaluate how Cloud SQL performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.

Try Cloud SQL free