Connect to Cloud SQL for SQL Server from Compute Engine

MySQL  |  PostgreSQL  |  SQL Server

Learn how to deploy a sample app on your Linux or Windows based Compute Engine VM instanceconnected to a SQL Server instance by using the Google Cloud console and a clientapplication.

Assuming that you complete all the steps in a timely manner, the resourcescreated in this quickstart typically cost less than one dollar (USD).

Before you begin

Note:The name you use for your project must be between 4 and 30 characters. When youtype the name, the form suggests a project ID, which you can edit. Theproject ID must be between 6 and 30 characters, with a lowercase letteras the first character. You can use a dash, lowercase letter, or digit for theremaining characters, but the last character cannot be a dash.
  1. Enable the Cloud APIs necessary to run a Cloud SQL sample app on a Compute Engine VM instance.

    Console

    Click theEnable APIs button to enable the API required for this quickstart.

    Enable APIs

    This enables the following API:

    • Cloud SQL Admin API

    gcloud

    Install thegcloud CLI which provides command-line access to your Google Cloud resources. Thegcloud CLI is used to run thegcloud CLI commands presented throughout this quickstart. All the commands are formatted to be run in a terminal or a Powershell window.

    Run the followinggcloud command:

    gcloudservicesenablesqladmin.googleapis.com

    This command enables the following API:

    • Cloud SQL Admin API

Set up Cloud SQL

Create a Cloud SQL instance

Public IP

Console

Create an instance with a public IP address

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

    Go to Cloud SQL Instances

  2. ClickCreate instance.
  3. ClickSQL Server.
  4. In theInstance ID field, enterquickstart-instance.
  5. In thePassword field, enter a password for the sqlserver user. Save this password for future use.
  6. In theChoose region and zonal availability section, selectSingle zone.
  7. Expand theShow Configurations section.
  8. In theMachine Type drop-down list, selectLightweight.
  9. ClickCreate Instance and then wait until the instance initializes and starts.

gcloud

Create an instance with a public IP address

Before running thegcloud sql instances create command as follows, replaceDB_ROOT_PASSWORD with the password of your database user.

Optionally, modify the values for the following parameters:

  • --database_version: The database engine type and version. If left unspecified, the API default is used. See thegcloud database versions documentation to see the currently available versions.
  • --cpu: The number of cores desired in the machine.
  • --memory: Whole number value indicating how much memory is desired in the machine. A size unit should be provided (for example, 3072MB or 9GB). If no units are specified, GB is assumed.
  • --region: Regional location of the instance (for example asia-east1, us-east1). If left unspecified, the defaultus-central is used. See the full list ofregions.

Run thegcloud sql instances create command to create a Cloud SQL instance.

gcloudsqlinstancescreatequickstart-instance--database-version=SQLSERVER_2019_STANDARD--cpu=1--memory=4GB--region=us-central--root-password=DB_ROOT_PASSWORD

Private IP

Console

Allocate an IP address range and create a private connection to configure private services access for Cloud SQL

  1. In the Google Cloud console, go to theVPC networks page.

    Go to VPC networks

  2. Select thedefault VPC network.
  3. Select thePrivate service connection tab.
  4. Select theAllocated IP ranges for services tab.
  5. ClickAllocate IP range.
  6. For theName of the allocated range, specifygoogle-managed-services-default.
  7. Select theAutomatic option for IP range and specify the prefix length as16.
  8. ClickAllocate to create the allocated range.
  9. Select thePrivate connections to services tab for thedefault VPC network.
  10. ClickCreate connection to create a private connection between your network and a service producer.
  11. For theAssigned allocation, selectgoogle-managed-services-default.
  12. ClickConnect to create the connection.

Create an instance with private IP address and SSL enabled

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

    Go to Cloud SQL Instances

  2. ClickCreate instance.
  3. ClickSQL Server.
  4. Enterquickstart-instance forInstance ID.
  5. Enter a password for the sqlserver user. Save this password for future use.
  6. Click theSingle zone option forChoose region and zonal availability.
  7. Click and expandShow configuration options.
  8. ForMachine Type, selectLightweight.
  9. InConnections, selectPrivate IP.
  10. Selectdefault in theNetwork drop-down menu.
  11. Clear thePublic IP checkbox to create an instance only with a private IP.
  12. ClickCreate instance and then wait for the instance to initialize and start.
  13. ClickConnections.
  14. In theSecurity section, selectAllow only SSL connections to enable SSL connections.
  15. In theAllow only SSL connections dialog, clickSave and restart and then wait for the instance to restart.

gcloud

Allocate an IP address range and create a private connection to configure private services access for Cloud SQL

  1. Run thegcloud compute addresses create command to allocate an IP address range.

    gcloudcomputeaddressescreategoogle-managed-services-default--global--purpose=VPC_PEERING--prefix-length=16--description="peering range for Google"--network=default
  2. Run thegcloud services vpc-peerings connect command to create a private connection to the allocated IP address range. Replace YOUR_PROJECT_ID with your project's project ID.

    gcloudservicesvpc-peeringsconnect--service=servicenetworking.googleapis.com--ranges=google-managed-services-default--network=default--project=YOUR_PROJECT_ID

Create an instance with private IP address and SSL enabled

  1. Before running the command as follows, replaceDB_ROOT_PASSWORD with the password of your database user.

  2. Optionally, modify the values for the following parameters:

    • --database_version: The database engine type and version. If left unspecified, the API default is used. See thegcloud database versions documentation to see the currently available versions.
    • --cpu: The number of cores in the machine.
    • --memory: A whole number value indicating how much memory to include in the machine. A size unit can be provided (for example, 3072MB or 9GB). If no units are specified, GB is assumed.
    • --region: The regional location of the instance (for example asia-east1, us-east1). If left unspecified, the defaultus-central1 is used. See the full list ofregions.

    Run thegcloud sql instances create command to create a Cloud SQL instance with a Private IP address.

    gcloudbetasqlinstancescreatequickstart-instance--database-version=SQLSERVER_2017_STANDARD--cpu=1--memory=4GB--region=us-central--root-password=DB_ROOT_PASSWORD--no-assign-ip--network=default
  3. Run thegcloud sql instances patch command to enableonly allow SSL connections for the instance.

  4. gcloudsqlinstancespatchquickstart-instance--require-ssl

Create a database

Console

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

    Go to Cloud SQL Instances

  2. Selectquickstart-instance.
  3. Open theDatabases tab.
  4. ClickCreate database.
    1. In theNew database dialog, enterquickstart_db as the name of the database.
    2. ClickCreate.

gcloud

Run thegcloud sql databases create command to create a database.

gcloudsqldatabasescreatequickstart_db--instance=quickstart-instance

Configure a Compute Engine service account

Create and configure a Google Cloud service account that has theCloud SQL Client role with permissions to connect to Cloud SQL. After you create a service account, you might need to wait for 60 seconds or more before you use the service account.

Console

Create a service account

  1. In the Google Cloud console, go to theCreate service account page.
  2. Go to Create service account

  3. Select a Google Cloud project.
  4. Enter aquickstart-service-account as the service account name.
  5. Optional: Enter a description of the service account.
  6. ClickCreate and continue and continue to the next step.
  7. Choose theCloud SQL Client role to grant to the service account on the project.
  8. ClickAdd another role and choose theStorage Object Viewer role to grant to the service account on the project.
  9. ClickContinue.
  10. ClickDone to finish creating the service account.

gcloud

Create a service account

  1. To create the service account, run thegcloud iam service-accounts create command:
  2. gcloudiamservice-accountscreatequickstart-service-account--description="DESCRIPTION"--display-name="quickstart-service-account"

    Replace the following value:

    • DESCRIPTION: an optional description of the service account
  3. To grant your service account theCloud SQL Client role and theStorage Object Viewer role on your project, run thegcloud projects add-iam-policy-binding command. ReplacePROJECT_ID with your Google Cloud project ID:
    gcloudprojectsadd-iam-policy-bindingPROJECT_ID--member="serviceAccount:quickstart-service-account@PROJECT_ID.iam.gserviceaccount.com"--role="roles/cloudsql.client"--role="roles/storage.objectViewer"

Create Compute Engine VM instance

Create a Compute Engine VM Instance to host a sample web app that connects to Cloud SQL.

Create a Linux VM Instance

Console

  1. In the Google Cloud console, go to theVM instances page.

    Go to VM instances

  2. Click theCreate instance button.
  3. For VM instanceName enterquickstart-vm-instance.
  4. ForService accounts selectquickstart-service-account.
  5. ForFirewall select theAllow HTTP traffic option.
  6. ClickCreate to create the VM instance.

gcloud

Before running the following command, replaceYOUR_PROJECT_ID with your project ID.

Run the followinggcloud compute instances create command.

gcloudcomputeinstancescreatequickstart-vm-instance--image-family=debian-10--image-project=debian-cloud--machine-type=e2-medium--service-account=quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com--scopes=https://www.googleapis.com/auth/cloud-platform--tags=http-server--zone=us-central1-a

Create a Windows VM Instance

Console

  1. In the Google Cloud console, go to theVM instances page.

    Go to VM instances

  2. Click theCreate instance button.
  3. For VM instanceName enterquickstart-vm-instance.
  4. ForBoot disc click theChange button.
    1. ForOperating system selectWindows Server.
    2. ForVersion selectWindows Server 2022 Datacenter.
    3. Click theSelect button.
  5. ForService accounts selectquickstart-service-account.
  6. ForFirewall select theAllow HTTP traffic option.
  7. ClickCreate to create the VM instance.
  8. After 2-3 minutes once the VM instance has started, click theSet Windows Password button on the VM instance details page.
  9. Copy and save this password in a secure location as you will be using it to access your VM instance in the next step of this quickstart.

gcloud

Before running the following command, replaceYOUR_PROJECT_ID with your project ID.

Run the followinggcloud compute instances create command in a Terminal Window.

gcloudcomputeinstancescreatequickstart-vm-instance--image-project=windows-cloud--image-family=windows-2022--machine-type=e2-medium--service-account=quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com--scopes=https://www.googleapis.com/auth/cloud-platform--tags=http-server--zone=us-central1-a

After 2-3 minutes once the VM instance has started, run the following command to set the Windows password on the VM instance.

gcloudcomputereset-windows-passwordquickstart-vm-instance

Copy and save this password in a secure location as you will be using it to access your VM instance in the next step of this quickstart.

Access Compute Engine VM instance

Access Linux VM Instance

Console

  • In the Google Cloud console, go to theVM instances page.

    Go to VM instances

  • In the list of virtual machine instances, clickSSH in the row of the instance that you want to connect to.

    SSH button next to instance name.

  • Note: When you connect to VMs using the Google Cloud console, Compute Engine creates an ephemeral SSH key for you. For more information about SSH keys, seeSSH connections to Linux VMs

    gcloud

    Use thegcloud compute ssh command to connect to a Linux VM instance. ReplaceYOUR_PROJECT_ID with your project ID:

    gcloudcomputessh--project=YOUR_PROJECT_ID--zone=us-central1-aquickstart-vm-instance

    Access Windows VM Instance

    Chrome RDP plugin

    Chrome RDP for Google Cloud is a third-party plugin that lets you connect to Windows instances by using the Chrome browser. The plugin is integrated with theGoogle Cloud console. After you install the plugin, connect to any Windows Server instance by using theRDP button in theGoogle Cloud console.

    To connect using the Chrome RDP plugin, do the following:

    1. Install theChrome RDP for Google Cloud extension.
    2. In Google Cloud console, go to theVM instances page and find the Windows instance you want to connect to.
    3. Go to the VM instances page

    4. Click theRDP button for the instance you want to connect to. The Chrome RDP extension opens.
    5. Since your VM instance does not have a domain configured, you can leave theDomain field blank
    6. Enter your username, and password, and clickOK to connect.
    7. If prompted, pressContinue to accept the certificate.

    Other

    SeeConnect to Windows VMs using RDP for more options for accessing a Compute Engine Windows VM instance.

    Setup development environment for programming language

    Set up the Compute Engine VM instance's development environment for your preferred programming language.

    Setup Linux VM Instance development environment

    Go

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Go sample app.

    1. Go to thesetup guide for a Go development environment.
    2. Complete the instructions in theInstall Go section.

    Java

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Java sample app.

    1. Go to thesetup guide for a Java development environment.
    2. Complete the instructions in theInstall a JDK (Java Development Kit) section.
    3. Complete the instructions in theInstall a build automation tool to set up Apache Maven.

    Node.js

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Node.js sample app.

    1. Go tosetup guide for a Node.js development environment.
    2. Complete the instructions in theInstalling Node.js and npm section.

    Python

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Python sample app.

    1. Go tosetup guide for a Python development environment.
    2. Complete the instructions in theInstalling Python section.

    Setup Windows VM Instance development environment

    Go

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Go sample app.

    1. Go to thesetup guide for a Go development environment.
    2. Complete the instructions in theInstall Go section.

    Java

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Java sample app.

    1. Go to thesetup guide for a Java development environment.
    2. Complete the instructions in theInstall a JDK (Java Development Kit) section.
    3. Complete the instructions in theInstall a build automation tool section to set up Apache Maven.

    Node.js

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Node.js sample app.

    1. Go tosetup guide for a Node.js development environment.
    2. Complete the instructions in theInstalling Node.js and npm section.

    Python

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Python sample app.

    1. Install Python and the PIP package manager for Python.
      1. Go tosetup guide for a Python development environment.
      2. Complete the instructions in theInstalling Python section.
        1. The instructions above will have you visit thePython Releases for Windows download page. From that page click theLatest Python 3 Release link.
        2. On the Python 3.X.X page, click theWindows Installer 64-bit link to download the installer file to your Windows Compute Engine VM instance.
        3. Once you've downloaded the Python installer to the VM instance, open the folder containing the downloaded file. Then right click on the installer file and selectRun as administrator.
        4. In theInstall Python dialog that appears, select the option toAdd Python 3.X.X to PATH and click→ Install Now.
    2. Use PIP to install virtualenv.
      1. Open Powershell on the Windows Compute Engine VM instance and run the followingpip install command.
      2. pipinstallvirtualenv

    Install Git

    InstallGit, an open source version control system on to your Compute Engine VM instance.

    Compute Engine Linux VM instance

    On your Compute Engine Linux VM instance, follow the officialGit installation documentation for Linux.

    • Run the suggestedDebian/Ubuntuinstall git command using thesudo command prefix to run the command as an administrator. The full installation command to run in the terminal should be formatted as follows:
    • sudo apt-get install git

    Compute Engine Windows VM instance

    On your Compute Engine Windows VM instance, follow the officialGit installation documentation for Windows to download the64-bit Standalone Installer and run it to install Git.

    Clone sample app

    Clone a sample app to your Compute Engine VM instance using thegit clone command.

    Go

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Go sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      gitclonehttps://github.com/GoogleCloudPlatform/golang-samples
    2. Change directory to the directory containing the sample app.
      cdgolang-samples/cloudsql/sqlserver/database-sql

    Java

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Java sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      gitclonehttps://github.com/GoogleCloudPlatform/java-docs-samples
    2. Change directory to the directory containing the sample app.
      cdjava-docs-samples/cloud-sql/sqlserver/servlet

    Node.js

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Node.js sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      gitclonehttps://github.com/GoogleCloudPlatform/nodejs-docs-samples
    2. Change directory to the directory containing the sample app.
      cdnodejs-docs-samples/cloud-sql/sqlserver/mssql

    Python

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Python sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      gitclonehttps://github.com/GoogleCloudPlatform/python-docs-samples
    2. Change directory to the directory containing the sample app.
      cdpython-docs-samples/cloud-sql/sql-server/sqlalchemy

    Configure and run a Cloud SQL sample app

    With a Cloud SQL instance, database, and service account with client permissions, you can now configure a sample application running on your Compute Engine VM instance to connect to your Cloud SQL instance.

    Public IP Cloud SQL Instance and Linux based Compute Engine VM

    Go

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_CONNECTION_NAME with your instance'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of thequickstart-user that you created in the previousCreate a user quickstart step.
    exportINSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME'exportDB_PORT='1433'exportDB_NAME='quickstart_db'exportDB_USER='sqlserver'exportDB_PASS='YOUR_DB_PASSWORD'

    On the Compute Engine VM instance in the open terminal, run the following commands to get the Go sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    1. Get the dependencies required to run to sample app.
      goget./...
    2. Run the sample app.
      goruncmd/app/main.go

    On your local computer get the Compute Engine VM instance's external IP address where the sample app is running and view it in a browser.

    Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.
    1. In a terminal or in Powershell on your local computer, get the Compute Engine VM instance's external IP address by running the followinggcloud compute instances describe command:
    2. gcloudcomputeinstancesdescribequickstart-vm-instance--format="value(networkInterfaces[0].accessConfigs[].natIP)"
    3. View the running sample app. Open a browser on your local computer and go to the Compute Engine VM instance's external IP address and port :8080.
      http://COMPUTE_ENGINE_VM_EXTERNAL_IP_ADDRESS:8080

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.

    Java

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_CONNECTION_NAME with your instance'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of thequickstart-user that you created in the previousCreate a user quickstart step.
    exportINSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME'exportDB_PORT='1433'exportDB_NAME='quickstart_db'exportDB_USER='sqlserver'exportDB_PASS='YOUR_DB_PASSWORD'

    On the Compute Engine VM instance in the open terminal, run the following command to get the Java sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    mvnjetty:run

    On your local computer get the Compute Engine VM instance's external IP address where the sample app is running and view it in a browser.

    Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.
    1. In a terminal or in Powershell on your local computer, get the Compute Engine VM instance's external IP address by running the followinggcloud compute instances describe command:
    2. gcloudcomputeinstancesdescribequickstart-vm-instance--format="value(networkInterfaces[0].accessConfigs[].natIP)"
    3. View the running sample app. Open a browser on your local computer and go to the Compute Engine VM instance's external IP address and port :8080.
      http://COMPUTE_ENGINE_VM_EXTERNAL_IP_ADDRESS:8080

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.

    Node.js

    1. On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacement:

      • YOUR_DB_PASSWORD with the password of thequickstart-user that you created in the previousCreate a user quickstart step.
      exportINSTANCE_HOST='127.0.0.1'exportDB_PORT='1433'exportDB_NAME='quickstart_db'exportDB_USER='sqlserver'exportDB_PASS='YOUR_DB_PASSWORD'
    2. Download the Cloud SQL Auth Proxy.
      curl-ocloud-sql-proxy\https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.0.0/cloud-sql-proxy.linux.amd64
    3. Set permissions to make the Cloud SQL Auth Proxy file executable.
      chmod+xcloud-sql-proxy
    4. Run the Cloud SQL Auth Proxy as a background process. ReplaceINSTANCE_CONNECTION_NAME with your instance'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
      ./cloud-sql-proxyINSTANCE_CONNECTION_NAME&
    Note: The process running the Cloud SQL Auth Proxy in the background can be stopped using thefg command in your Terminal window where you started the sample app. This should bring the running Cloud SQL Auth Proxy job to the terminal foreground. Then press theCtrl +C keys to stop the proxy job.

    On the Compute Engine VM instance in the open terminal, run the following commands to get the required Node.js packages on to your Compute Engine VM instance and run the sample app.

    1. Install the Node.js packages necessary to run the app locally.
      npminstall
    2. Run the sample app.
      npmstart

    On your local computer get the Compute Engine VM instance's external IP address where the sample app is running and view it in a browser.

    Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.
    1. In a terminal or in Powershell on your local computer, get the Compute Engine VM instance's external IP address by running the followinggcloud compute instances describe command:
    2. gcloudcomputeinstancesdescribequickstart-vm-instance--format="value(networkInterfaces[0].accessConfigs[].natIP)"
    3. View the running sample app. Open a browser on your local computer and go to the Compute Engine VM instance's external IP address and port :8080.
      http://COMPUTE_ENGINE_VM_EXTERNAL_IP_ADDRESS:8080

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.

    Python

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_CONNECTION_NAME with your instance'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of thequickstart-user that you created in the previousCreate a user quickstart step.
    exportINSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME'exportDB_PORT='1433'exportDB_NAME='quickstart_db'exportDB_USER='sqlserver'exportDB_PASS='YOUR_DB_PASSWORD'

    On the Compute Engine VM instance in the open terminal, run the following commands to get the Python sample app's requirements on to your Compute Engine VM instance and run the sample app.

    1. Initialize a virtual environment and install the requirements to run to sample app.
      python3-mvenvenvsourceenv/bin/activatepipinstall-rrequirements.txt
    2. Run the sample app.
      pythonapp.py

    On your local computer, set upport forwarding over SSH by performing the instructions in next quickstart step. This enables you to use a browser on your local computer to view the app running on your Compute Engine VM instance.

    Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.
    1. In a terminal or in Powershell on your local computer, run the followinggcloud compute ssh command to setup port forwarding over SSH. Before running the command, replaceYOUR_PROJECT_ID with your project ID.
    2. gcloudcomputesshquickstart-vm-instance--project=YOUR_PROJECT_ID--zone=us-central1-a--ssh-flag='-L 8000:127.0.0.1:8080'
    3. View the running sample app. With port forwarding actively running, open a browser on your local computer and enterhttp://127.0.0.1:8000 in the address bar of your browser..

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.

    Public IP Cloud SQL Instance and Windows based Compute Engine VM

    Go

    On the Compute Engine VM instance in the open Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_CONNECTION_NAME with your instance'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of thequickstart-user that you created in the previousCreate a user quickstart step.
    $env:INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME"$env:DB_PORT="1433"$env:DB_NAME="quickstart_db"$env:DB_USER="sqlserver"$env:DB_PASS="YOUR_DB_PASSWORD"

    On the Compute Engine VM instance in the open Powershell window, run the following commands to get the Go sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    1. Get the dependencies required to run to sample app.
      goget./...
    2. Run the sample app.
      goruncmd\app\main.go
    3. View the running sample app. Open a browser on the Compute Engine VM instance and go tohttp://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Java

    On the Compute Engine VM instance in the open Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_CONNECTION_NAME with your instance'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of thequickstart-user that you created in the previousCreate a user quickstart step.
    $env:INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME"$env:DB_PORT="1433"$env:DB_NAME="quickstart_db"$env:DB_USER="sqlserver"$env:DB_PASS="YOUR_DB_PASSWORD"
    1. On the Compute Engine VM instance in the open Powershell window, run the following command to get the Java sample app's dependencies on to your Compute Engine VM instance and run the sample app.

      mvnjetty:run
    2. View the running sample app. Open a browser on the Compute Engine VM instance and go tohttp://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Node.js

    1. On the Compute Engine VM instance in the open Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacement:

      • YOUR_DB_PASSWORD with the password of thequickstart-user that you created in the previousCreate a user quickstart step.
      $env:INSTANCE_HOST="127.0.0.1"$env:DB_PORT="1433"$env:DB_NAME="quickstart_db"$env:DB_USER="sqlserver"$env:DB_PASS="YOUR_DB_PASSWORD"
    2. Download the Cloud SQL Auth Proxy.
      wgethttps://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.0.0/cloud-sql-proxy.x64.exe`-Ocloud-sql-proxy.exe
    3. Run the Cloud SQL Auth Proxy as a background process. ReplaceINSTANCE_CONNECTION_NAME with your instance'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
      Start-Process-filepath".\cloud-sql-proxy.exe"-ArgumentList`"INSTANCE_CONNECTION_NAME"
    Note: The process running the Cloud SQL Auth Proxy in the background can be stopped by selecting thecmd.exe window that was spawned by theStart-Process command. Then close the window to stop the proxy job.

    On the Compute Engine VM instance in the open Powershell window, run the following commands to get the required Node.js packages on to your Compute Engine VM instance and run the sample app.

    1. Install the Node.js packages necessary to run the app locally.
      npminstall
    2. Run the sample app.
      npmstart
    3. View the running sample app. Open a browser on the Compute Engine VM instance and go tohttp://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Python

    On the Compute Engine VM instance in the open Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    $env:INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME"$env:DB_PORT="1433"$env:DB_NAME="quickstart_db"$env:DB_USER="sqlserver"$env:DB_PASS="YOUR_DB_PASSWORD"

    On the Compute Engine VM instance in the open Powershell window, run the following commands to get the Python sample app's requirements on to your Compute Engine VM instance and run the sample app.

    1. Initialize a virtual environment and install the requirements to run to sample app.
      virtualenv--pythonpython3env.\env\Scripts\activatepipinstall-rrequirements.txt
    2. Run the sample app.
      pythonapp.py
    3. View the running sample app. Open a browser on the Compute Engine VM instance and go tohttp://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Private IP Cloud SQL Instance and Linux based Compute Engine VM

    Create and download SSL server certificate on to your local computer

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

      Go to Cloud SQL Instances

    2. Click thequickstart-instance to see itsOverview page.
    3. Click theConnections tab.
    4. In theSecurity section, clickDownload certificates to download the SSL server certificate.

    Upload SSL certificate to Cloud Storage bucket

    Console

    In a browser on your local computer, create a Cloud Storage bucket and upload the SSL certificate to the bucket where it can then be accessed from the Compute Engine VM instance.

    1. Create a Cloud Storage bucket.
      1. ForName of your bucket, enter the following name. ReplaceYOUR_PROJECT_ID with your project ID:
        YOUR_PROJECT_ID-quickstart-certs
      2. Click theCreate button to create the bucket.
    2. Click theUpload Files button to upload files to the newly created Cloud Storage bucket.
    3. Select the following file to be uploaded from your local computer to Cloud Storage:
      • server-ca.pem

    gcloud

    On your local computer in a terminal or Powershell window open to the directory where you downloaded the SSL certificate, you can now create a Cloud Storage bucket and upload the SSL certificate to bucket where it can then be accessed from the Compute Engine VM instance. Thegcloud CLI will be used to upload the file.

    1. Run the followinggcloud storage buckts create command to make a new Cloud Storage bucket:
    2. gcloudstoragebucketscreategs://YOUR_PROJECT_ID-quickstart-certs--location=us-central1
    3. From the directory where you downloaded the certificate on your local computer, run the followinggcloud storage cp command to copy the SSL certificates to the newly created Cloud Storage bucket:
    4. gcloudstoragecpserver-ca.pemgs://YOUR_PROJECT_ID-quickstart-certs/

    Go

    Download SSL certificate using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a terminal open to thegolang-samples/cloudsql/sqlserver/database-sql directory, run the followinggcloud storage cp command to download the SSL certificates from Cloud Storage to thecerts directory.

    gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pemcerts/.

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    exportINSTANCE_HOST='INSTANCE_HOST'exportDB_PORT='1433'exportDB_NAME='quickstart_db'exportDB_USER='sqlserver'exportDB_PASS='YOUR_DB_PASSWORD'exportDB_ROOT_CERT='certs/server-ca.pem'exportPRIVATE_IP='TRUE'

    On the Compute Engine VM instance in the open terminal, run the following commands to get the Go sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    1. Get the dependencies required to run to sample app.
      goget./...
    2. Run the sample app.
      goruncmd/app/main.go

    On your local computer get the Compute Engine VM instance's external IP address where the sample app is running and view it in a browser.

    Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.
    1. In a terminal or in Powershell on your local computer, get the Compute Engine VM instance's external IP address by running the followinggcloud compute instances describe command:
    2. gcloudcomputeinstancesdescribequickstart-vm-instance--format="value(networkInterfaces[0].accessConfigs[].natIP)"
    3. View the running sample app. Open a browser on your local computer and go to the Compute Engine VM instance's external IP address and port :8080.
      http://COMPUTE_ENGINE_VM_EXTERNAL_IP_ADDRESS:8080

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.

    Java

    Download SSL certificate using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a terminal open to thejava-docs-samples/cloud-sql/sqlserver/servlet/ directory, run the followinggcloud storage cp command to download the SSL certificates from Cloud Storage to the current directory.

    gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem.

    Configure the SSL certificates for use in Java on the Compute Engine VM instance

    In the terminal on the Compute Engine VM instance, run the following command to import the server certificate into a custom Java truststore using keytool. Before running the following command, replaceTRUST_CERT_KEYSTORE_PASSWD with your own custom keystore password to be used to create the Java truststore.

    keytool-importcert-aliasMySQLCACert-fileserver-ca.pem\-keystorequickstart-truststore-storepassTRUST_CERT_KEYSTORE_PASSWD

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    exportINSTANCE_HOST='INSTANCE_HOST'exportDB_PORT='1433'exportDB_NAME='quickstart_db'exportDB_USER='sqlserver'exportDB_PASS='YOUR_DB_PASSWORD'exportTRUST_CERT_KEYSTORE_PATH='quickstart-truststore'exportTRUST_CERT_KEYSTORE_PASSWD='TRUST_CERT_KEYSTORE_PASSWD'exportPRIVATE_IP='TRUE'

    On the Compute Engine VM instance in the open terminal, run the following command to get the Java sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    mvnjetty:run

    On your local computer get the Compute Engine VM instance's external IP address where the sample app is running and view it in a browser.

    Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.
    1. In a terminal or in Powershell on your local computer, get the Compute Engine VM instance's external IP address by running the followinggcloud compute instances describe command:
    2. gcloudcomputeinstancesdescribequickstart-vm-instance--format="value(networkInterfaces[0].accessConfigs[].natIP)"
    3. View the running sample app. Open a browser on your local computer and go to the Compute Engine VM instance's external IP address and port :8080.
      http://COMPUTE_ENGINE_VM_EXTERNAL_IP_ADDRESS:8080

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.

    Python

    Download SSL certificate using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a terminal open to thepython-docs-samples/cloud-sql/sql-server/sqlalchemy directory, run the followinggcloud storage cp command to download the SSL certificates from Cloud Storage to thecerts directory.

    gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pemcerts/.

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    exportINSTANCE_HOST='INSTANCE_HOST'exportDB_PORT='1433'exportDB_NAME='quickstart_db'exportDB_USER='sqlserver'exportDB_PASS='YOUR_DB_PASSWORD'exportDB_ROOT_CERT='certs/server-ca.pem'exportPRIVATE_IP='TRUE'

    On the Compute Engine VM instance in the open terminal, run the following commands to get the Python sample app's requirements on to your Compute Engine VM instance and run the sample app.

    1. Initialize a virtual environment and install the requirements to run to sample app.
      python3-mvenvenvsourceenv/bin/activatepipinstall-rrequirements.txt
    2. Run the sample app.
      pythonapp.py

    On your local computer, set upport forwarding over SSH by performing the instructions in next quickstart step. This enables you to use a browser on your local computer to view the app running on your Compute Engine VM instance.

    Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.
    1. In a terminal or in Powershell on your local computer, run the followinggcloud compute ssh command to setup port forwarding over SSH. Before running the command, replaceYOUR_PROJECT_ID with your project ID.
    2. gcloudcomputesshquickstart-vm-instance--project=YOUR_PROJECT_ID--zone=us-central1-a--ssh-flag='-L 8000:127.0.0.1:8080'
    3. View the running sample app. With port forwarding actively running, open a browser on your local computer and enterhttp://127.0.0.1:8000 in the address bar of your browser..

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.

    Private IP Cloud SQL Instance and Windows based Compute Engine VM

    Create and download SSL server certificate on to your local computer

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

      Go to Cloud SQL Instances

    2. Click thequickstart-instance to see itsOverview page.
    3. Click theConnections tab.
    4. In theSecurity section, clickDownload certificates to download the SSL server certificate.

    Upload SSL certificate to Cloud Storage bucket

    Console

    In a browser on your local computer, create a Cloud Storage bucket and upload the SSL certificate to the bucket where it can then be accessed from the Compute Engine VM instance.

    1. Create a Cloud Storage bucket.
      1. ForName of your bucket, enter the following name. ReplaceYOUR_PROJECT_ID with your project ID:
        YOUR_PROJECT_ID-quickstart-certs
      2. Click theCreate button to create the bucket.
    2. Click theUpload Files button to upload files to the newly created Cloud Storage bucket.
    3. Select the following file to be uploaded from your local computer to Cloud Storage:
      • server-ca.pem

    gcloud

    On your local computer in a terminal or Powershell window open to the directory where you downloaded the SSL certificate, you can now create a Cloud Storage bucket and upload the SSL certificate to bucket where it can then be accessed from the Compute Engine VM instance. Thegcloud CLI will be used to upload the file.

    1. Run the followinggcloud storage buckts create command to make a new Cloud Storage bucket:
    2. gcloudstoragebucketscreategs://YOUR_PROJECT_ID-quickstart-certs--location=us-central1
    3. From the directory where you downloaded the certificate on your local computer, run the followinggcloud storage cp command to copy the SSL certificates to the newly created Cloud Storage bucket:
    4. gcloudstoragecpserver-ca.pemgs://YOUR_PROJECT_ID-quickstart-certs/

    Go

    Download SSL certificate using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a Powershell window open to thegolang-samples/cloudsql/sqlserver/database-sql directory, run the followinggcloud storage cp command to download the SSL certificate from Cloud Storage to thecerts directory.

    gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pemcerts/.

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    $env:INSTANCE_HOST="INSTANCE_HOST"$env:DB_PORT="1433"$env:DB_NAME="quickstart_db"$env:DB_USER="sqlserver"$env:DB_PASS="YOUR_DB_PASSWORD"$env:DB_ROOT_CERT="certs/server-ca.pem"$env:PRIVATE_IP="TRUE"

    On the Compute Engine VM instance in the open Powershell window, run the following commands to get the Go sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    1. Get the dependencies required to run to sample app.
      goget./...
    2. Run the sample app.
      goruncmd\app\main.go
    3. View the running sample app. Open a browser on the Compute Engine VM instance and go tohttp://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Java

    Download SSL certificate using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a Powershell window open to thejava-docs-samples/cloud-sql/sqlserver/servlet/ directory, run the followinggcloud storage cp command to download the SSL certificate from Cloud Storage to the current directory.

    gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem.

    Configure the SSL certificates for use in Java on the Compute Engine VM instance

    In the terminal on the Compute Engine VM instance, run the following command to import the server certificate into a custom Java truststore using keytool. Before running the following command, replaceTRUST_CERT_KEYSTORE_PASSWD with your own custom keystore password to be used to create the Java truststore.

    keytool-importcert-aliasSQLServerCACert-fileserver-ca.pem`-keystorequickstart-truststore-storepassTRUST_CERT_KEYSTORE_PASSWD

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    $env:INSTANCE_HOST="INSTANCE_HOST"$env:DB_PORT="1433"$env:DB_NAME="quickstart_db"$env:DB_USER="sqlserver"$env:DB_PASS="YOUR_DB_PASSWORD"$env:TRUST_CERT_KEYSTORE_PATH="quickstart-truststore"$env:TRUST_CERT_KEYSTORE_PASSWD="TRUST_CERT_KEYSTORE_PASSWD"$env:PRIVATE_IP="TRUE"
    1. On the Compute Engine VM instance in the open Powershell window, run the following command to get the Java sample app's dependencies on to your Compute Engine VM instance and run the sample app.

      mvnjetty:run
    2. View the running sample app. Open a browser on the Compute Engine VM instance and go tohttp://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Python

    Download SSL certificate using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a Powershell window open to thepython-docs-samples/cloud-sql/sql-server/sqlalchemy directory, run the followinggcloud storage cp command to download the SSL certificate from Cloud Storage to thecerts directory.

    gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pemcerts/.

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    $env:INSTANCE_HOST="INSTANCE_HOST"$env:DB_PORT="1433"$env:DB_NAME="quickstart_db"$env:DB_USER="sqlserver"$env:DB_PASS="YOUR_DB_PASSWORD"$env:DB_ROOT_CERT="certs/server-ca.pem"$env:PRIVATE_IP="TRUE"

    On the Compute Engine VM instance in the open Powershell window, run the following commands to get the Python sample app's requirements on to your Compute Engine VM instance and run the sample app.

    1. Initialize a virtual environment and install the requirements to run to sample app.
      virtualenv--pythonpython3env.\env\Scripts\activatepipinstall-rrequirements.txt
    2. Run the sample app.
      pythonapp.py
    3. View the running sample app. Open a browser on the Compute Engine VM instance and go tohttp://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Clean up

    To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

    Delete Cloud SQL instance

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

      Go to Cloud SQL Instances

    2. Select thequickstart-instance instance to open theInstance detailspage.
    3. In the icon bar at the top of the page, clickDelete.
    4. In theDelete instance dialog box, typequickstart-instance, and thenclickDelete to delete the instance.

    Delete Compute Engine VM instance

    1. In the Google Cloud console, go to theVM instances page.

      Go to VM instances

    2. Select thequickstart-vm-instance instance to open theInstance detailspage.
    3. In the icon bar at the top of the page, clickDelete.
    4. ClickDelete to delete the instance.

    Optional cleanup steps

    If you're not using theCloud SQL client role that you assigned to theCompute Engine default service account, you can remove it.

    1. In the Google Cloud console, go to theIAM page.

      Go to IAM

    2. Click the edit icon (which looks like a pencil) for the IAM account namedCompute Engine default service account.
    3. Delete theCloud SQL client role.
    4. ClickSave.

    If you're not using the API that was enabled as part of this quickstart, youcan disable it.

    • API that was enabled within this quickstart:
      • Cloud SQL Admin API
    1. In the Google Cloud console, go to theAPIs page.

      Go to APIs

    2. Select any API that you would like to disable and then click theDisable API button.

    What's next

    Based on your needs, you can learn more aboutcreating Cloud SQL instances.

    You also can learn about creatingSQL Server users anddatabases for your Cloud SQL instance.

    For more information about pricing, seeCloud SQL for SQL Server pricing.

    Learn more about:

    • Configuring your Cloud SQL instance with apublic IP address.
    • Configuring your Cloud SQL instance with aprivate IP address.

    Additionally, you can learn about connecting to a Cloud SQL instance fromother Google Cloud applications:

    Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

    Last updated 2025-07-14 UTC.