This page shows you how to connect to Cloud SQL from a local test environment using the Cloud SQL Proxy. Connecting through the Cloud SQL Proxy enables you to test an App Engine application in your local environment, or establish a secure connection for database administration.
Do not use these instructions to set up the Cloud SQL Proxy for a production environment. For more connection options, see Connecting using the Cloud SQL Proxy.
Before you begin
Before performing the steps in this quickstart, complete the following tasks:
- Create a Google Cloud project and a Cloud SQL instance.
- If you are using a pre-existing project, ensure that your Google Cloud
user is an owner of your project, or has an IAM role other than
Cloud SQL Viewer
. Otherwise, set up the Cloud SQL Proxy using the instructions outlined in Connecting using the Cloud SQL Proxy. -
The client enables you to test your connection to your instance. The
mysql
client can be installed with the server package. Some operating systems support a client-only package. See the MySQL installation instructions. Install and authenticate the gcloud command-line tool.
- If you haven't already, install the
gcloud
command-line tool. See thegcloud
installation instructions. - Initialize the
gcloud
tool:gcloud init
- Authenticate the
gcloud
tool:gcloud auth login
- If you haven't already, install the
Install the Cloud SQL Proxy client on your local machine
The Cloud SQL Proxy binary you download depends on your operating system, and whether
it uses a 32-bit or 64-bit kernel.
Most newer hardware uses a 64-bit kernel. If you are unsure whether your
machine is running a 64-bit kernel, use the
uname -a
command for Linux or macOS, or click Computer > Properties in
the Start Menu for Windows.
Linux 64-bit
- Download the Cloud SQL Proxy:
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
- Make the Cloud SQL Proxy executable:
chmod +x cloud_sql_proxy
Linux 32-bit
- Download the Cloud SQL Proxy:
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.386 -O cloud_sql_proxy
- Make the Cloud SQL Proxy executable:
chmod +x cloud_sql_proxy
macOS 64-bit
- Download the Cloud SQL Proxy:
curl -o cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.amd64
- Make the Cloud SQL Proxy executable:
chmod +x cloud_sql_proxy
macOS 32-bit
- Download the Cloud SQL Proxy:
curl -o cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.386
- Make the Cloud SQL Proxy executable:
chmod +x cloud_sql_proxy
Windows 64-bit
Right-click https://dl.google.com/cloudsql/cloud_sql_proxy_x64.exe and select Save Link As to download the Cloud SQL Proxy. Rename the file tocloud_sql_proxy.exe
.
Windows 32-bit
Right-click https://dl.google.com/cloudsql/cloud_sql_proxy_x86.exe and select Save Link As to download the Cloud SQL Proxy. Rename the file tocloud_sql_proxy.exe
.
Cloud SQL Proxy Docker image
For convenience, the Cloud SQL team maintains several container images that contain the Cloud SQL Proxy for use by our customers. For more information about these images, see the Cloud SQL Proxy repo on GitHub. You can pull the latest image to your local machine using Docker with the following command:docker pull gcr.io/cloudsql-docker/gce-proxy:1.19.1
Other OS
For other operating systems not included here, you can compile the Cloud SQL Proxy from source.Get the instance connection name
Go to the Cloud SQL Instances page in the Google Cloud Console.
Click the instance name to open its Instance details page.
Under Connect to this instance, note the Instance connection name.
Start the Cloud SQL Proxy
Start the proxy in its own terminal so you can monitor its output. Replace
<INSTANCE_CONNECTION_NAME>
with the instance connection name you copied
in the previous step.
./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306
A message similar to the following appears:
Listening on 127.0.0.1:3306 for myproject:us-central1:myinstance". Ready for new connections
Connect to your database using the mysql client
This section is optional, but is recommended for testing your connection. See the MySQL installation instructions.
In a different terminal window from where you started the Cloud SQL Proxy, run the following command, replacing<USERNAME>
with your MySQL username.
mysql -u <USERNAME> -p --host 127.0.0.1 --port 3306
The mysql prompt appears, and a message similar to the following is in the Cloud SQL Proxy terminal:
New connection for "myproject:us-central1:myinstance"
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, follow these steps.
- Go to the Cloud SQL Instances page in the Google Cloud Console.
Go to the Cloud SQL Instances page - Select the
myinstance
instance to open the Instance details page. - In the icon bar at the top of the page, click Delete.
In the Delete instance window, type
myinstance
, then click Delete to delete the instance.You cannot reuse an instance name for about 7 days after an instance is deleted.