# Connect to Cloud SQL for SQL Server using the Cloud SQL Auth Proxy

<br />

This page shows you how to connect to Cloud SQL from a local test
environment using the Cloud SQL Auth Proxy. The [Cloud SQL Auth Proxy](https://docs.cloud.google.com/sql/docs/sqlserver/sql-proxy.md.txt)
provides secure access to your Cloud SQL instance without the need for
authorized networks or for configuring SSL. By using the Cloud SQL Auth Proxy, you can
connect to your Cloud SQL instance securely.

The instructions on this page are for a test environment only and shouldn't be
used for production environments. For more information
on the configuration required for production environments, see
[Use the Cloud SQL Auth Proxy in a production environment](https://docs.cloud.google.com/sql/docs/mysql/sql-proxy.md.txt#production-environment)
and [Connect using the Cloud SQL Auth Proxy](https://docs.cloud.google.com/sql/docs/mysql/connect-auth-proxy.md.txt).

## Before you begin

> [!NOTE]
> **Note:** The name you use for your project must be between 4 and 30 characters. When you type the name, the form suggests a project ID, which you can edit. The project ID must be between 6 and 30 characters, with a lowercase letter as the first character. You can use a dash, lowercase letter, or digit for the remaining characters, but the last character cannot be a dash.

1. Enable the necessary Google Cloud APIs.

   ### Console

   In the Google Cloud console, go to the **APIs** page.

   [Go to APIs](https://console.cloud.google.com/apis)
   Enable the Cloud SQL Admin API.

   ### gcloud

   Click the following button to open Cloud Shell, which provides
   command-line access to your Google Cloud resources directly from the browser.
   Cloud Shell can be used to run the `gcloud` commands presented throughout this quickstart.

   [Open Cloud Shell](https://docs.cloud.google.com/console/sql/instances?cloudshell=true)


   Run the [`gcloud services enable`](https://docs.cloud.google.com/sdk/gcloud/reference/services/enable) command
   as follows using Cloud Shell to enable the APIs required for this quickstart.:

   ```bash
   gcloud services enable sqladmin.googleapis.com
   ```

   This command enables the following APIs:
   - Cloud SQL Admin API
2.

   Make sure that you have the following role or roles on the project:

   Cloud SQL Admin (`roles/cloudsql.admin`)

   #### Check for the roles

   1.
      In the Google Cloud console, go to the **IAM** page.

      [Go to IAM](https://console.cloud.google.com/projectselector/iam-admin/iam?supportedpurview=project)
   2. Select the project.
   3.
      In the **Principal** column, find all rows that identify you or a group that
      you're included in. To learn which groups you're included in, contact your
      administrator.

   4. For all rows that specify or include you, check the **Role** column to see whether the list of roles includes the required roles.

   #### Grant the roles

   1.
      In the Google Cloud console, go to the **IAM** page.

      [Go to IAM](https://console.cloud.google.com/projectselector/iam-admin/iam?supportedpurview=project)
   2. Select the project.
   3. Click **Grant access**.
   4.
      In the **New principals** field, enter your user identifier.

      This is typically the email address for a Google Account.

   5. Click **Select a role**, then search for the role.
   6. To grant additional roles, click **Add
      another role** and add each additional role.
   7. Click **Save**.
3. Install and initialize the [gcloud CLI](https://docs.cloud.google.com/sdk/docs/install).

## Create a Cloud SQL instance

In this quickstart, you use the Google Cloud console. To use the
[gcloud CLI](https://docs.cloud.google.com/sdk/gcloud.md.txt), curl, or PowerShell, see
[Create instances](https://docs.cloud.google.com/sql/docs/sqlserver/create-instance.md.txt).

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

   [Go to Cloud SQL Instances](https://console.cloud.google.com/sql)
2. Click **Create instance**.
3. From the **Create instance** menu, select **New instance**.
4. Click **Choose SQL Server**.
5. In the **Instance ID** field, enter `myinstance`.
6. In the **Password** field, enter a password for the `sqlserver` user.
7. Click **Create instance**.

   You're returned to the instances list. You can click the new instance right
   away to see the details, but it won't be available for other operations until
   it initializes and starts.

   > [!NOTE]
   > **Note:** In this example, the instance is created using default settings, including a public IP address.

### Debian/Ubuntu

For Debian/Ubuntu, install the applicable SQL Server command-line tools using
[these instructions](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-tools?view=sql-server-2017#ubuntu).

### CentOS/RHEL

For CentOS/RHEL, install the applicable SQL Server command-line tools using
[these instructions](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-tools?view=sql-server-2017#install-tools-on-rhel-7).

### openSUSE

For openSUSE, install the applicable SQL Server command-line tools using
[these instructions](https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-suse?view=sql-server-2017#tools).

### Other platforms

See the [landing page](https://docs.microsoft.com/en-us/sql/database-engine/install-windows/install-sql-server-database-engine?view=sql-server-2017) for installing SQL Server, as well as the
[SQL Server downloads page](https://www.microsoft.com/en-us/sql-server/sql-server-downloads).

## Install the Cloud SQL Auth Proxy client

For installation, see
[Installing the Cloud SQL Auth Proxy](https://docs.cloud.google.com/sql/docs/sqlserver/connect-auth-proxy.md.txt#install).

## Get the instance connection name

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

   [Go to Cloud SQL Instances](https://console.cloud.google.com/sql)
2. Click the instance name to open its **Overview** page.
3. In the **Connect to this instance** section, copy the **Connection name** . The connection name is in the format `projectID:region:instanceID`.

## Start the Cloud SQL Auth Proxy

The Cloud SQL Auth Proxy uses
[Application Default Credentials (ADC)](https://docs.cloud.google.com/docs/authentication/application-default-credentials.md.txt)
to authenticate to Google Cloud. For more information, see
[Options for authenticating the Cloud SQL Auth Proxy](https://docs.cloud.google.com/sql/docs/sqlserver/connect-auth-proxy.md.txt#authentication-options).

To authenticate the Cloud SQL Auth Proxy using your user account credentials, run the
following command:

```bash
gcloud auth application-default login
```

Start the Cloud SQL Auth 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.

For Linux or macOS environments, use this command to launch the Cloud SQL Auth Proxy:

```bash
./cloud-sql-proxy INSTANCE_CONNECTION_NAME
```

<br />

In PowerShell on Windows, use this command to launch the Cloud SQL Auth Proxy:

```bash
.\cloud-sql-proxy.exe INSTANCE_CONNECTION_NAME
```

<br />

A message similar to the following appears:

```
Listening on 127.0.0.1:1433 for INSTANCE_CONNECTION_NAME
Ready for new connections
```

Run the following command, replacing `USER_NAME` with your SQL Server username.

<br />

```bash
sqlcmd -S 127.0.0.1 -U USER_NAME
```

Return to the terminal window where you started the Cloud SQL Auth Proxy. You
should see a message similar to the following:

`New connection for *myInstance*`

## 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](https://console.cloud.google.com/sql)
2. Select the `myinstance` instance to open the **Instance details** page.
3. In the icon bar at the top of the page, click **Delete**.
4. In the **Delete instance** window, type your instance's name and then click **Delete**.

### Optional cleanup steps

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

1.

   In the Google Cloud console, go to the **APIs** page.

   [Go to APIs](https://console.cloud.google.com/apis)

   <br />

2. Select the Cloud SQL Admin API and then click the
   **Disable API** button.

## What's next

- See how to [troubleshoot information for the Cloud SQL Auth Proxy](https://docs.cloud.google.com/sql/docs/sqlserver/connect-auth-proxy.md.txt#troubleshooting).

- Learn more [about the Cloud SQL Auth Proxy](https://docs.cloud.google.com/sql/docs/sqlserver/sql-proxy.md.txt).