Microsoft SQL Server 是可在 Windows Server 和部分 Linux 發行版上執行的資料庫系統。您可以將 Compute Engine 上的 SQL Server 用於應用程式的後端,做為靈活的開發和測試環境,或是用於備份和災難復原的內部部署系統。
Compute Engine 會提供已在 Windows Server 上預先安裝 Microsoft SQL Server 的映像檔。對於這些 SQL Server 映像檔,Compute Engine 可管理 Windows Server 和 SQL Server 兩方的授權,並將相關費用加入每月帳單。您可以建立具有 SQL Server 的 VM,並在需要時進行資源調度,擴增為大型的多重節點設定。
Compute Engine 會將資料儲存在耐用的永久磁碟上,磁碟具有靜態資料自動備援和自動加密功能。您可以使用這些永久磁碟儲存 SQL Server 資料,且不用擔憂資料的耐用性或安全性問題。如需額外效能,請建立具有本機 SSD 的 VM 做為快取,以提供更多額外的 IOPS 和效能供 SQL Server 查詢使用。
您可以透過下列方式,建立執行 SQL Server 的 Compute Engine 虛擬機器 (VM) 執行個體:
使用 SQL Server 映像檔建立 VM。這些映像檔會有其中一種在 Windows Server 上執行的 SQL Server 版本。您可使用這些映像檔在 Compute Engine 上執行 SQL Server,而無需自 Microsoft 取得專屬的個別授權。
將現有的 SQL Server 授權遷移至 Compute Engine 上的 Windows Server VM,並自行管理這些授權。
關於已預先安裝 SQL Server 的可用映像檔系列,如需完整清單,請參閱「作業系統詳細資料」。
SQL Server 映像檔的機器類型需求
SQL Server 的最佳機器類型取決於您執行的 SQL Server 版本。
SQL Server Standard
您可以在具有任何機器類型的 VM 上執行 SQL Server Standard,但共用核心機器類型無法達到最佳執行效果。Google 建議您使用至少含有一個 vCPU 的 VM 來執行 SQL Server Standard。
SQL Server Enterprise
執行 SQL Server Enterprise 的 VM 至少必須具有 4 個 vCPU。為獲得最佳效能,Google 建議您在記憶體容量較大的 VM 上執行 SQL Server Enterprise。視工作負載而定,您應使用 highmem
預先定義的機器類型,其中配備 8 個 vCPU 或更多,例如 N2。highmem
預先定義的機器類型可將 Compute Engine 上每個可用的 vCPU 記憶體比率提升至最大,最為適合 SQL Server Enterprise VM。
此外,您可在 Compute Engine 上使用 SQL Server Enterprise 建立 SQL Server 可用性群組。
預設元件
根據預設,SQL Server 映像檔包含數種元件。預設元件的種類取決於您選擇的 SQL Server 版本。
如要瞭解您所用 SQL Server 版本包含的預設元件,請參閱「SQL Server 2019 的版本和支援功能」。
如要瞭解如何修改 SQL Server 元件,請參閱「在 SQL Server 的執行個體中新增功能」。
事前準備
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
Terraform
To use the Terraform samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
建立 SQL Server 執行個體
如要建立 SQL Server 執行個體,請指定您所需之特定 SQL Server 版本的映像檔系列。如需 SQL Server 映像檔系列的清單,請參閱「作業系統詳細資訊」。
為了發揮最佳效能,資料庫伺服器平台需要虛擬 CPU 較多且記憶體容量較大的機器類型。Google 建議您使用搭配至少 2 個 vCPU 和至少 4 GB 記憶體的機器類型,以利執行 SQL Server 執行個體。您可以使用自訂機器類型設定 SQL Server 執行個體,以滿足工作負載的效能需求。此外,您也可以使用較大的永久磁碟和較快的 SSD 永久磁碟,藉此提升應用程式的效能。
此外,您必須設定特定的防火牆規則,在 VM 所屬的虛擬私有雲網路或子網路上允許 SQL Server 流量。詳情請參閱 SQL Server 最佳做法的相關說明。
主控台
如要建立 SQL Server 執行個體,請按照下列步驟操作:
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
指定 VM 詳細資料。
在「Boot disk」(開機磁碟) 部分,按一下「Change」(變更),然後執行下列操作:
- 在「Public images」分頁中,按一下「Operating system」清單,然後選取「SQL Server on Windows Server」。
- 在「Version」清單中選取版本。
- 在「Boot disk type」(開機磁碟類型) 清單中,選取開機磁碟類型。
- 在「Size (GB)」(大小 (GB)) 欄位中,設定開機磁碟大小。
- 選用步驟:如要修改開機磁碟的進階設定,請按一下「顯示進階設定」,然後指定設定。
- 如要儲存開機磁碟設定,請按一下「選取」。
按一下 [建立]。
建立 VM 後,請建立防火牆規則,允許 VM 存取 SQL Server。預設的 SQL Server 通訊埠為 1433。
在 Google Cloud 控制台中,前往「防火牆規則」頁面。
按一下頁面頂端的 [Create firewall rule] (建立防火牆規則),即可開始建立防火牆規則。
指定這個防火牆規則的詳細資料。
- 在「Name」(名稱) 欄位中,指定防火牆規則的名稱。
- 在「Network」(網路) 欄位中,選取 SQL Server 執行個體所在的虛擬私有雲網路。
- 在「Direction of traffic」(流量方向),選取「Ingress」(輸入)。
- 在「Targets」欄位中,選擇防火牆規則的目標,可以是所有執行個體,或是指定的目標標記或服務帳戶。例如,您可以選取「All instances in the network」(網路中的所有執行個體)。如果您為 SQL Server 執行個體設定了標記或服務帳戶,就可以將防火牆規則限制為特定標記或服務帳戶。
- 在 [Source filter] (來源篩選器) 欄位中,選取您要允許存取這個通訊埠的 IP 範圍。針對輸入規則,您可以選取來源 IPv4 範圍、來源 IPv6 範圍、來源標記或服務帳戶。使用「來源篩選器」後方的欄位,提供所選項目的其他資訊。
- 在「Protocols and ports」(通訊協定和通訊埠) 區段中,選擇「Specified protocols and ports」(指定的通訊協定和通訊埠),然後輸入 SQL Server 使用的通訊埠。在本例中,請選擇「TCP」並輸入
1433
,這是預設通訊埠。
按一下 [Create] (建立) 以建立此防火牆規則,並允許透過通訊埠 1433 存取 SQL Server 執行個體。
如果您需要為 VM 新增其他防火牆規則,請參閱防火牆規則說明文件。
gcloud
使用 compute images list
指令查看可用的 SQL Server 映像檔清單:
gcloud compute images list --project windows-sql-cloud --no-standard-images
使用 compute instances create
指令建立新的 VM,並指定其中一個 Windows Server 或 SQL Server 公開映像檔的映像檔系列。
gcloud compute instances create VM_NAME \ --image-project windows-sql-cloud \ --image-family IMAGE_FAMILY \ --machine-type MACHINE_TYPE \ --boot-disk-size BOOT_DISK_SIZE \ --boot-disk-type BOOT_DISK_TYPE
更改下列內容:
- VM_NAME:新執行個體的名稱。
- IMAGE_FAMILY:Windows Server 或 SQL Server 映像檔的其中一個公開映像檔系列。
- MACHINE_TYPE:可用的機器類型之一。
- BOOT_DISK_SIZE:開機磁碟的大小,單位為 GB。永久磁碟越大則總處理量越高。
- BOOT_DISK_TYPE:執行個體的開機磁碟類型。例如:
pd-balanced
。
建立 VM 後,請建立防火牆規則,允許 VM 存取 SQL Server。預設的 SQL Server 通訊埠為 1433。
gcloud compute firewall-rules create sql-server-1433 \ --description "Allow SQL Server access from all sources on port 1433." \ --allow tcp:1433 --network NETWORK
其中 NETWORK 是 VM 所在的虛擬私人雲端網路名稱。
如果您需要為 VM 新增其他防火牆規則,請參閱防火牆規則說明文件。
Terraform
如要建立 SQL Server VM 執行個體,請使用 google_compute_instance
資源。
如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。
REST
如要使用 API 建立 VM,請在 VM 建立要求中加入 initializeParams
屬性,並指定 Windows 映像檔。
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances instance = { "name": "VM_NAME", "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE", "disks": [{ "boot": "true", "type": "PERSISTENT", "initializeParams": { "diskName": "DISK_NAME", "sourceImage": "projects/IMAGE_PROJECT/global/images/family/IMAGE_FAMILY", "diskSizeGb": "BOOT_DISK_SIZE", "diskType": "BOOT_DISK_TYPE", } }], "networkInterfaces": [{ "accessConfigs": [{ "type": "ONE_TO_ONE_NAT", "name": "External NAT" }], "network": "global/networks/default" }], "serviceAccounts": [{ "email": DEFAULT_SERVICE_EMAIL, "scopes": DEFAULT_SCOPES }] }
更改下列內容:
- PROJECT_ID:專案 ID。
- ZONE:這個例項的區域。
- VM_NAME:新 VM 的名稱。
- MACHINE_TYPE:可用的機器類型。
- IMAGE_PROJECT:
windows-cloud
(適用於 Windows Server 映像檔) 或windows-sql-cloud
(適用於預先安裝 SQL Server 的 Windows Server 映像檔)。 - IMAGE_FAMILY:Windows Server 或 SQL Server 映像檔的公開映像檔系列。
- BOOT_DISK_SIZE:開機磁碟的大小,單位為 GB。較大的永久磁碟總處理量較高。
- BOOT_DISK_TYPE:VM 開機磁碟的類型。例如:
pd-ssd
。
建立 VM 後,請使用 firewalls.insert
方法建立防火牆規則,允許存取 VM 上的 SQL Server。預設的 SQL Server 通訊埠為 1433
。
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls { "name": "sql-server-1433", "network": "global/networks/NETWORK", "allowed": [ { "IPProtocol": "tcp", "ports": [ "1433" ] } ] }
更改下列內容:
- PROJECT_ID:專案 ID。
- NETWORK:VM 所在的虛擬私人雲端網路名稱。
建立 SQL Server 執行個體後,請設定 VM 的初始密碼,以便透過 RDP 連線至 VM。接著,以系統管理員身分執行 SQL Server Management Studio,管理 SQL Server 執行個體上的資料庫。
此外,您可以透過下列一或多個項目強化已安裝的 SQL Server 功能:
- 在 SQL Server 執行個體上安裝 SQL Server 資料工具 (SSDT),以便使用 Visual Studio 建立及管理 SQL Server 資料庫。連線至 VM,然後從 Microsoft 網站下載並安裝 SSDT。
- SQL Server 映像檔包含數個預設元件,您可以視需要為系統上安裝的 SQL Server 新增功能。SQL Server 映像檔的安裝程式執行檔一律位於
c:\sql_server_install\setup.exe
。 - 如要在次要永久磁碟中安裝 SQL Server,請在 VM 中新增次要永久磁碟。接著,請連線到 VM,執行
c:\sql_server_install\setup.exe
安裝程式,然後在次要磁碟上選取新的安裝路徑。 - 如果您需要為開機磁碟或任何次要儲存磁碟增加額外儲存空間,請重新調整永久磁碟的大小,即使執行個體正在執行也可調整。
管理 SQL Server 資料庫
以系統管理員身分執行 SQL Server Management Studio,設定 SQL Server 資料庫。您可以在本機工作站上下載並安裝 SQL Server Management Studio,然後在 VM 上使用這套工具遠端連線至資料庫引擎。
如果您無法在本機工作站上安裝 Management Studio,請透過遠端桌面通訊協定連線至 VM,並在 VM 本身上執行 Management Studio。根據預設,SQL Server 2012 和 SQL Server 2014 都包含 SQL Server Management Studio。如果是 SQL Server 2016,您必須從 Microsoft 網站下載 SQL Server Management Studio,並在 VM 上安裝。
根據預設,SQL Server 會使用 Windows 驗證模式控管 SQL Server 本身的遠端存取權。如要使用 SQL Server 驗證模式,請變更為驗證模式。
手動更新 SQL Server 執行個體名稱
如果您重新命名代管 SQL Server 的 VM,則必須更新 SQL Server 執行個體名稱。詳情請參閱「為代管 SQL Server 獨立執行個體的電腦重新命名」。
後續步驟
- 確認 VM 是否已成功啟動。
- 建立並連接次要儲存磁碟至您的 VM,以將資料與開機磁碟分開存放。
- 瞭解在 Google Cloud 上使用 SQL Server 的最佳做法。
- 建立及設定高效能 SQL Server 執行個體。
- 使用 HammerDB 在 SQL Server 上執行負載測試。
- 透過 .NET 應用程式使用 SQL Server。
- 連線至 VM。