This page shows you how to use MySQL Client, installed on a Google Compute Engine (GCE), to Google Cloud SQL using the standard MySQL wire protocol. At a high level, this scenario involves:
- Granting the GCE instance IP access to your Google Cloud SQL instance.
- Installing the MySQL client on your GCE instance.
- Connecting to your Google Cloud SQL instance.
- Setting the TCP keepalive of the GCE instance.
In the steps below, we assume that:
- You have created and configured a GCE instance. For more information, see Accessing Google Compute Engine .
- You know the external (public) IP address of your GCE instance.
To use MySQL Client from a GCE instance:
-
Grant the GCE instance access to your Google Cloud SQL instance.
You will do this by authorizing the IP address of the GCE instance as a network that can connect to your Cloud SQL instance. For more information, see Configuring access control for non-App Engine applications .
-
Connect to your GCE instance using the
gcutil
tool.
gcutil --project=<project-id> ssh <instance-name>
-
Install MySQL client if it is not already installed.
The command below shows how to install the MySQL client.
sudo apt-get update sudo apt-get install mysql-client
-
Connect with the MySQL client.
mysql --host=<instance-ip-address> --user=<user-name> --password Enter password: Welcome to the MySQL monitor. ... mysql>
For an example of how to connect using SSL, see Connecting Using MySQL Client .
-
Run a MySQL command.
SHOW PROCESSLIST;
-
If you expect that connections between your GCE instance and your Cloud SQL instance will include long-lived unused connections, then you should be aware that connections with a GCE instance time out after 10 minutes of inactivity. For more information, see Networking and Firewalls in the Google Compute Engine documentation.
To keep long-lived unused connections alive, you can set the TCP keepalive . The following command sets the TCP keepalive to one minute.
sudo bash -c 'echo 60 > /proc/sys/net/ipv4/tcp_keepalive_time'
If you do not plan to continue connecting from your GCE instance, consider performing any of the following cleanup steps.
- If you assigned a static IP address to the GCE instance, release it (see Reserved IP addresses ).
- Delete the instance (see Shutting down an instance ).
- Revoke access to the Cloud SQL instance you granted to the GCE instance (see Configuring access control for non-App Engine applications ).