This page shows you how to connect to Cloud SQL from a local test environment using the Cloud SQL Proxy. Connecting through the 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 proxy for a production environment.
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 a Cloud SQL role other than
Cloud SQL Viewer
. Otherwise, set up the proxy using the instructions outlined in Connecting using the Cloud SQL Proxy. Optionally, install a sqlcmd client.
The client could be SQL Server Management Studio (SSMS), Azure Data Studio, or another client.
Enable the Cloud SQL Admin API
Install and authenticate the gcloud command-line tool
If you haven't already, install the
gcloud
command-line tool.Initialize the
gcloud
tool:gcloud init
Authenticate the
gcloud
tool:gcloud auth login
Install the Cloud SQL Proxy client on your local machine
For installation, see Installing the Cloud SQL Proxy.
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.
<INSTANCE_CONNECTION_NAME>
with the instance connection name you copied
in the previous step.
For Linux environments, use this command to launch the proxy:
./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:1433
In PowerShell on Windows, use this command to launch the proxy:
.\cloud_sql_proxy.exe -instances=<INSTANCE_CONNECTION_NAME>=tcp:1433
A message similar to the following appears:
Listening on 127.0.0.1:1433 for myproject:myregion:myinstance". Ready for new connections
Connect to your database using the sqlcmd client
This section is optional, but is recommended for testing your connection.
If you installed and configured a sqlcmd client, you can connect to your Cloud SQL instance at the IP address127.0.0.1
, which is routed
through the proxy's secure connection to Cloud SQL.
When you connect to the Cloud SQL instance through the SQL Server client, a message similar to the following is in the proxy terminal:
New connection for "myproject:us-central1:myinstance"
Then you can run queries and perform other operations.
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.