在本教學課程中,您會將資料從 Compute Engine 上執行的 SQL Server 2017 Enterprise 遷移至 SQL Server 2017 Enterprise 適用的 Cloud SQL。本教學課程說明如何從 SQL Server 2017 匯出資料、將資料匯入 Cloud SQL for SQL Server 2017,以及驗證資料是否已成功匯入。
如要從 SQL Server 2008 遷移資料,請參閱使用備份檔案在 SQL Server 2008 和 SQL Server 適用的 Cloud SQL 間遷移資料。
如果您是系統管理員、開發人員、工程師、資料庫管理員或 DevOps 工程師,而且要將資料從 SQL Server 2017 遷移至 SQL Server 適用的 Cloud SQL,本教學課程將有所幫助。
本教學課程假設您熟悉下列項目:
- Microsoft SQL Server
- Microsoft PowerShell
- Compute Engine
- Cloud Storage
- Cloud SQL
目標
- 在 Compute Engine 上建立 SQL Server 虛擬機器 (VM) 執行個體,以代管範例資料庫。
- 建立 SQL Server 適用的 Cloud SQL 執行個體。
- 建立 Cloud Storage bucket。
- 備份範例資料庫。
- 將資料庫匯入 Cloud SQL for SQL Server。
- 驗證匯入的資料。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
- Compute Engine
- Cloud SQL
- Cloud Storage
- SQL Server (premium with Compute Engine)
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。
事前準備
- 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Storage, Cloud SQL Admin, and Compute Engine APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Storage, Cloud SQL Admin, and Compute Engine APIs.
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
建立範例資料庫的指令碼
在本節中,您將建立指令碼,填入預先載入的範例資料庫。接著,您會在 Compute Engine 上建立使用這個資料庫的 SQL Server 2017 執行個體。
在 Cloud Shell 中,建立要部署為啟動指令碼的 PowerShell 指令碼:
cat << 'EOF' > startup.ps1 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $dataseturl = "https://opendata.maryland.gov/api/views/ryxx-aeaf/rows.csv?accessType=DOWNLOAD" Invoke-WebRequest -Uri $dataseturl -OutFile c:\dataset.csv Invoke-Sqlcmd -Query "CREATE DATABASE testdb;" Invoke-Sqlcmd -Query "CREATE TABLE testdb.dbo.MarylandZipCodes ( zipcode int NOT NULL PRIMARY KEY, city varchar(255), county varchar(255))" Invoke-Sqlcmd -Query "BULK INSERT testdb.dbo.MarylandZipCodes FROM 'c:\dataset.csv' WITH (DATAFILETYPE='char',FIELDTERMINATOR=',' ,ROWTERMINATOR = '0x0a', FIRSTROW=2);" EOF
這個啟動指令碼會建立名為
testdb
的本機資料庫,並在其中建立名為MarylandZipCodes
的資料表。接著,指令碼會下載馬里蘭州公開郵遞區號登錄資料,並將登錄資料匯入表格。為 Windows Server 2016 建立 SQL Server 2017 Enterprise 執行個體:
gcloud compute instances create sql-server --machine-type n1-highmem-4 \ --boot-disk-size 50GB \ --image-project windows-sql-cloud --image-family sql-ent-2017-win-2016 \ --zone us-central1-f \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --metadata-from-file windows-startup-script-ps1=startup.ps1
在本教學課程中,您會在
us-central1-f
可用區建立執行個體,開機磁碟大小為 50 GB。如要進一步瞭解區域,請參閱「Cloud 據點」一文。--metadata-from-file
旗標會將 PowerShell 指令碼設為執行個體的開機指令碼。
建立 Cloud SQL 執行個體和 Cloud Storage 值區
在 Cloud Shell 中,建立 SQL Server 2017 Enterprise 適用的 Cloud SQL 執行個體,稍後您會將資料庫遷移至該執行個體:
gcloud beta sql instances create target \ --database-version=SQLSERVER_2017_ENTERPRISE \ --cpu=2 \ --memory=5GB \ --root-password=sqlserver12@ \ --zone=us-central1-f
建立執行個體需要幾分鐘。預設根使用者名稱為
sqlserver
,預設密碼為sqlserver12@
。在本教學課程中,您會在us-central1-f
可用區建立執行個體。如要進一步瞭解可用區,請參閱「Cloud 服務據點」一文。建立 Cloud Storage bucket,在資料匯入 Cloud SQL 前儲存備份檔案:
gcloud storage buckets create "gs://${DEVSHELL_PROJECT_ID}-sql-backups" --location=US
備份範例資料庫
在本節中,您將連線至 SQL Server 虛擬機器 (VM)、建立資料庫備份,並將備份資料庫上傳至 Cloud Storage。
在 Cloud Shell 中,重設 SQL Server VM 執行個體的密碼:
gcloud compute reset-windows-password sql-server --zone=us-central1-f
記下使用者的新密碼。
安裝遠端桌面通訊協定 (RDP) 用戶端。詳情請參閱「Microsoft 遠端桌面用戶端」。
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
在 Google Cloud 控制台的「Compute Engine」部分,按一下「RDP」下拉式選單,然後選取「Download the RDP file」(下載遠端桌面協定檔案) 選項,下載 SQL Server VM 執行個體的遠端桌面協定檔案。
使用此檔案即可利用遠端桌面通訊協定用戶端連線至執行個體。詳情請參閱「Microsoft 遠端桌面用戶端」。
在瀏覽器中,將所有視窗最小化,然後啟動 PowerShell 命令列工具。
建立備份資料夾:
mkdir c:\backup
在瀏覽器中,在備份資料夾中建立資料庫備份:
osql -E -Q "BACKUP DATABASE testdb TO DISK='c:\backup\testdb.bak'"
在執行個體的殼層中,將備份檔案複製到 Cloud Storage 值區:
$PROJECT_ID=(gcloud config get-value core/project) gcloud storage cp c:\backup\testdb.bak gs://$PROJECT_ID-sql-backups --no-clobber
將備份檔案匯入 Cloud SQL
在 Cloud Shell 中,擷取與 Cloud SQL 執行個體相關聯的服務帳戶,並儲存至變數:
SVC_EMAIL_ADDRESS=$(gcloud sql instances describe target \ --format='value(serviceAccountEmailAddress)') echo $SVC_EMAIL_ADDRESS
Google Cloud 在您建立 Cloud SQL 執行個體時建立服務帳戶。您可以使用服務帳戶,授予 Cloud SQL 執行個體存取所需資源的權限。
授予服務帳戶 Cloud Storage bucket 的寫入權限:
gcloud storage buckets add-iam-policy-binding gs://${DEVSHELL_PROJECT_ID}-sql-backups \ --member=serviceAccount:${SVC_EMAIL_ADDRESS} \ --role=roles/storage.legacyBucketWriter
授予服務帳戶讀取 Cloud Storage bucket 中檔案的權限:
gcloud storage buckets add-iam-policy-binding gs://${DEVSHELL_PROJECT_ID}-sql-backups/testdb.bak \ --member=serviceAccount:${SVC_EMAIL_ADDRESS} \ --role=roles/storage.legacyObjectReader
將備份檔案匯入 Cloud SQL 資料庫:
gcloud beta sql import bak target \ gs://${DEVSHELL_PROJECT_ID}-sql-backups/testdb.bak --database testdb
驗證資料匯入
在本節中,您將檢查範例資料是否成功匯入。
在 Cloud Shell 中,安裝 SQL Server 工具包:
sudo apt-get install -y mssql-tools
如果接受授權條款,請在系統提示時輸入
yes
。如要安全存取 Cloud SQL 執行個體,請下載 Cloud SQL Proxy:
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
啟動 Cloud SQL Proxy:
CONNECTION_NAME=$(gcloud sql instances describe target --format='value(connectionName)') ./cloud_sql_proxy -instances=${CONNECTION_NAME}=tcp:1433 &
讀取 Cloud SQL 資料表中的資料列數:
/opt/mssql-tools/bin/sqlcmd -U sqlserver -S 127.0.0.1 -Q "select count(*) from testdb.dbo.MarylandZipCodes"
系統提示您輸入密碼時,請輸入
sqlserver12@
密碼。在輸出內容中,確認結果為619
。
清除所用資源
如要避免付費,最簡單的方法就是刪除您針對教學課程建立的專案。 Google Cloud刪除專案
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
後續步驟
- 請參閱這篇文章,瞭解 SQL Server 適用的 Cloud SQL 客戶自行管理加密金鑰 (CMEK)。
- 瞭解如何為 SQL Server 適用的 Cloud SQL 設定私人 IP 連線。
- 探索 Google Cloud 的參考架構、圖表和最佳做法。 歡迎瀏覽我們的雲端架構中心。