Connect to Cloud SQL for SQL Server from SSMS
This page shows you how to create and connect to a SQL Server instance and perform basic SQL operations by using the Google Cloud console and a client. The resources created in this quickstart typically cost less than a dollar, assuming you complete the steps, including the cleanup, in a timely manner.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project .
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project .
- Enable the necessary Google Cloud APIs.
Console
In the Google Cloud console, go to the APIs page.
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.Run the
gcloud services enable
command as follows using Cloud Shell to enable the APIs required for this quickstart.:gcloud services enable sqladmin.googleapis.com
This command enables the following APIs:
- Cloud SQL Admin API
-
Make sure that you have the following role or roles on the project: Cloud SQL Admin (
roles/cloudsql.admin
), Cloud SQL Viewer (roles/cloudsql.viewer
)Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
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.
- 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
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access .
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- In the Select a role list, select a role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save .
-
Create a Cloud SQL instance
In this quickstart, you use the Google Cloud console. To use the gcloud CLI , cURL, or PowerShell, see Create instances .
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Click Create instance.
- Click Choose SQL Server.
- In the Instance IDfield, enter
myinstance
. - In the Passwordfield, enter a password for the
sqlserver
user. -
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.
Connect to your instance by using SQL Server Management Studio
- Optional: If you're running a local instance of SQL Server, stop it before
connecting to your Cloud SQL instance. Otherwise, you might encounter
errors such as
address already in use
. - Install the gcloud CLI . The gcloud CLI provides the gcloud CLI to interact with Cloud SQL and other Google Cloud services. The gcloud CLI uses the Admin API to access Cloud SQL, so you must Enable the Admin API before using the gcloud CLI to access Cloud SQL.
- In a bash shell command prompt or in Windows PowerShell,
run the following command to initialize the gcloud CLI:
gcloud init
- Run the following command to authenticate the gcloud CLI:
gcloud auth login
- Download and install the Cloud SQL Auth Proxy (see Installing the Cloud SQL Auth Proxy ). Note the location of the Cloud SQL Auth Proxy because you will run the Cloud SQL Auth Proxy in next step.
- Run the Cloud SQL Auth Proxy by using a bash shell command prompt
(or by using Windows PowerShell). Specifically, run the following command,
replacing
Instance-connection-name
with the corresponding value from the Google Cloud console's Overview tab (for your instance):./cloud-sql-proxy INSTANCE_CONNECTION_NAME
For more information about installing and using the Cloud SQL Auth Proxy, see About the Cloud SQL Auth Proxy .
As described in the next section, now you can connect to your SQL Server instance by using SSMS and the localhost IP address.
Connect using the SSMS Object Explorer
-
In SSMS, select Connect Object Explorerfrom the Filemenu.
-
Enter the following values in the Connectiondialog:
- For Server Type, enter Database Engine.
- For Server Name, enter 127.0.0.1 as the IP address of your SQL Server instance.
- For Authentication, enter SQL Server Authentication.
- For Login, enter sqlserver.
- For Password, enter the password used when the instance was created.
- Click the Connectbutton.
Create a database and upload data
-
In the SSMS Object Explorer window, right-click the Databasesnode under your instance and select New Database.
-
Enter testdbfor the Database nameand click the OKbutton.
-
Under the newly created testdbdatabase, right-click the Tablesnode and select New > Table.
-
Enter the following values in the Create tabledialog:
- In the Propertieswindow, for Identity > Name, enter guestbook.
- For the first Column Name, enter entryID, set its Data Type to int, and clear the Allow Nullscheckbox.
- In the Column Propertieswindow, expand the Identity Specificationitem and set (Is Identity)to Yes.
- For the second Column Name, enter guestnameand set its Data Type to varchar(255).
- For the third Column Name, enter contentand set its Data Type to varchar(255).
-
Click the Filemenu and select Save guestbook.
-
Right-click the testdbtable under Databasesand select New Query.
-
Enter the following two INSERT statements into the SQL querytext window and click the Executebutton.
INSERT INTO guestbook ( guestName , content ) values ( 'first guest' , 'I got here!' ); INSERT INTO guestbook ( guestName , content ) values ( 'second guest' , 'Me too!' );
As an example:
-
Expand the Tablesitem under the Databases > testdbitem in the Object Explorerwindow. Right-click the dbo.guestbooktable and choose Select Top 1000 Rows.
The two records you inserted are displayed as Results, along with the SQL SELECT
statement used to query the records.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select the
myinstance
instance to open the Instance detailspage. - In the icon bar at the top of the page, click Delete.
- In the Delete instancewindow, 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
-
In the Google Cloud console, go to the APIs page.
-
Select the Cloud SQL Admin API and then click the Disable APIbutton.
What's next
-
Learn about creating Cloud SQL instances .
-
Learn about creating SQL Server users and databases for your Cloud SQL instance.
-
See the Cloud SQL pricing information .
-
In this quickstart you connected to the instance by using Cloud Shell. Learn about all of the connectivity options in Cloud SQL. How you connect depends on your networking configuration, such as if your Cloud SQL instance has a public or private IP address. See how to configure your Cloud SQL instance with a public IP and a private IP address.
-
Learn about connecting to a Cloud SQL instance from other Google Cloud applications: