使用備份檔案在 SQL Server 2017 和 SQL Server 適用的 Cloud SQL 間遷移資料


在本教學課程中,您將從在 Compute Engine 上執行的 SQL Server 2017 Enterprise 遷移資料,至 SQL Server 2017 Enterprise 適用的 Cloud SQL。本教學課程將說明如何從 SQL Server 2017 匯出資料、將資料匯入 SQL Server 2017 適用的 Cloud SQL,並驗證資料是否已成功匯入。

如要從 SQL Server 2008 遷移資料,請參閱「使用備份檔案在 SQL Server 2008 和 SQL Server 適用的 Cloud SQL 間遷移資料」。

如果您是系統管理員、開發人員、工程師、資料庫管理員或 DevOps 工程師,而且要將資料從 SQL Server 2017 遷移至 SQL Server 適用的 Cloud SQL,本教學課程將會非常實用。

本教學課程假設您熟悉下列項目:

目標

  • 在 Compute Engine 上建立 SQL Server 虛擬機器 (VM) 執行個體,以便代管範例資料庫。
  • 建立 Cloud SQL for SQL Server 執行個體。
  • 建立 Cloud Storage bucket。
  • 備份範例資料庫。
  • 將資料庫匯入 SQL Server 適用的 Cloud SQL。
  • 驗證匯入的資料。

費用

In this document, you use the following billable components of Google Cloud:

To generate a cost estimate based on your projected usage, use the pricing calculator. New Google Cloud users might be eligible for a free trial.

When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up.

事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Storage, Cloud SQL Admin, and Compute Engine APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Storage, Cloud SQL Admin, and Compute Engine APIs.

    Enable the APIs

  8. In the Google Cloud console, activate Cloud Shell.

    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 執行個體。

  1. 在 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 的資料表。接著,指令碼會下載 馬里蘭州公開郵遞區號登錄資料庫,並將登錄資料庫匯入資料表。

  2. 為 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 值區

  1. 在 Cloud Shell 中建立 Cloud SQL for SQL Server 2017 Enterprise 執行個體,稍後將資料庫遷移至該執行個體:

    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 服務據點

  2. 在將資料匯入 Cloud SQL 之前,請建立 Cloud Storage 值區來儲存備份檔案:

    gcloud storage buckets create "gs://${DEVSHELL_PROJECT_ID}-sql-backups" --location=US
    

備份範例資料庫

在本節中,您將連線至 SQL Server 虛擬機器 (VM)、建立資料庫備份,並將備份資料庫上傳至 Cloud Storage。

  1. 在 Cloud Shell 中,重設 SQL Server VM 執行個體的密碼:

    gcloud compute reset-windows-password sql-server --zone=us-central1-f
    

    請記下使用者的新密碼。

  2. 安裝遠端桌面通訊協定 (RDP) 用戶端。詳情請參閱 Microsoft 遠端桌面用戶端

  3. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往「VM INSTANCES」(VM 執行個體)頁面

  4. 在 Google Cloud 控制台的「Compute Engine」部分,按一下「RDP」下拉式選單,然後選取「Download the RDP file」選項,即可為 SQL Server VM 執行個體下載 RDP 檔案。

    使用此檔案即可透過遠端桌面協定用戶端連線至執行個體。詳情請參閱 Microsoft 遠端桌面用戶端

  5. 在瀏覽器中,將所有視窗最小化,然後啟動 PowerShell 指令列工具。

  6. 建立備份資料夾:

    mkdir c:\backup
    
  7. 在瀏覽器中,在備份資料夾中建立資料庫備份:

    osql -E -Q "BACKUP DATABASE testdb TO DISK='c:\backup\testdb.bak'"
    
  8. 在執行個體的殼層中,將備份檔案複製到 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

  1. 在 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 執行個體的存取權授予所需資源。

  2. 授予服務帳戶寫入 Cloud Storage 值區的權限:

    gcloud storage buckets add-iam-policy-binding gs://${DEVSHELL_PROJECT_ID}-sql-backups \
        --member=serviceAccount:${SVC_EMAIL_ADDRESS} \
        --role=roles/storage.legacyBucketWriter
    
  3. 授予服務帳戶讀取 Cloud Storage 值區中檔案的權限:

    gcloud storage buckets add-iam-policy-binding gs://${DEVSHELL_PROJECT_ID}-sql-backups/testdb.bak \
        --member=serviceAccount:${SVC_EMAIL_ADDRESS} \
        --role=roles/storage.legacyObjectReader
    
  4. 將備份檔案匯入 Cloud SQL 資料庫:

    gcloud beta sql import bak target \
        gs://${DEVSHELL_PROJECT_ID}-sql-backups/testdb.bak --database testdb
    

驗證資料匯入

在本節中,您將檢查是否已成功匯入範例資料。

  1. 在 Cloud Shell 中安裝 SQL Server 工具包

    sudo apt-get install -y mssql-tools
    

    如果您接受授權條款,請在系統提示時輸入 yes

  2. 如要安全存取 Cloud SQL 執行個體,請下載 Cloud SQL Proxy

    wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
    
  3. 啟動 Cloud SQL Proxy:

    CONNECTION_NAME=$(gcloud sql instances describe target --format='value(connectionName)')
    ./cloud_sql_proxy -instances=${CONNECTION_NAME}=tcp:1433 &
    
  4. 讀取 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 專案。

刪除專案

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

後續步驟