將工作負載從其他平台遷移至 SQL Server 適用的 Cloud SQL 時,通常需要使用 Google Cloud 控制台,從您從先前環境匯出的 SQL dump 檔案匯入資料。
本教學課程將說明如何建立所需 Google Cloud 資源,然後將 SQL 資料庫匯入 SQL Server 適用的 Cloud SQL 執行個體。本教學課程會說明遷移至 SQL Server 適用的 Cloud SQL 時的最佳做法,包括使用具備私人服務連線的虛擬私有雲 (VPC) 網路,以及為 Cloud SQL 執行個體啟用私人 IP。
除非另有指定,否則請在完成步驟時保留設定的預設值。
目標
- 下載範例 SQL dump 檔案。
- 建立新的虛擬私有雲網路,並啟用私人服務連線。
- 建立 Cloud Storage bucket,並將 SQL dump 檔案上傳至該 bucket。
- 建立設定為使用私人 IP 的 SQL Server 適用的 Cloud SQL 執行個體。
- 建立目的地資料庫。
- 從 dump 檔案匯入新資料庫。
- 查看結構並執行查詢,確認資料庫已成功匯入。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud SQL, Cloud SQL Admin, Compute Engine, Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud SQL, Cloud SQL Admin, Compute Engine, Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
必要的角色
如要取得完成本教學課程所需的權限,請要求系統管理員在專案中授予您下列 IAM 角色:
-
Cloud SQL 管理員 (
roles/cloudsql.admin) - 儲存空間管理員 (
roles/storage.admin) -
Compute 網路管理員 (
roles/compute.networkAdmin)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
取得範例資料庫傾印檔案
在本教學課程中,您將使用包含國家/地區代碼和世界首都的小型範例資料庫。
將 countries 資料庫的 SQL 檔案下載到本機環境:countries-sqlserver.sql。
建立具有私人服務連線的網路
建立其餘資源前,請先建立虛擬私有雲網路,以便在該網路上執行服務。私人服務連線可讓您使用內部 IPv4 位址,在外部網路與 SQL Server 適用的 Cloud SQL 之間建立私人連線,藉此限制對 Cloud SQL 資料庫的存取權。
前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。
按一下「建立虛擬私有雲網路」。
在名稱欄位中輸入
tutorial-network。在「子網路建立模式」部分,選取「自訂」。
在名稱欄位中輸入
tutorial-subnet。選取您附近的區域。
在「IPv4 range」(IPv4 範圍) 中,輸入
10.0.0.0/24。在「Private Google Access」部分,選取「啟用」。
按一下 [完成]。
按一下頁面底部的 [Create] (建立)。
虛擬私有雲網路建立完成後,即可為該網路設定私人服務存取權。
- 在「虛擬私有雲網路」畫面中,按一下「tutorial-network」。
- 在「tutorial-network」的選單列中,按一下「Private services access」(私人服務連線)。
- 按一下「分配的服務 IP 範圍」。
- 按一下「Allocate IP range」(分配 IP 範圍)。
- 在「Name」(名稱) 中輸入
tutorial-range。 - 在「IP address range」(IP 位址範圍) 中,選取「Custom」(自訂)。
- 在「Range」(範圍) 欄位中輸入
192.168.0.0/20。 - 按一下「Allocate」(分配)。
- 在子選單中,按一下「Private connections to services」(私人服務連線)。
- 點選「建立連線」。
- 在「Assigned allocation」(已指派的分配範圍) 下拉式選單中,選取「tutorial-range」。
- 按一下 [連線]。一兩分鐘後, Google Cloud 控制台會顯示訊息,通知您已成功建立私人連線。
建立 Cloud Storage 值區
接著,建立 Cloud Storage bucket,儲存您先前下載的 SQL dump 檔案。Cloud SQL 匯入工具預期傾印檔案會放在值區中。Cloud Storage bucket 必須設定全域不重複名稱。
- 前往 Google Cloud 控制台的「Cloud Storage bucket」頁面。
- 點選「建立」。
- 建立 bucket 名稱時,請使用小寫英文字母、數字和連字號,並確保名稱全域不重複。建議使用隨機字串產生器 (例如 random.org/strings) 產生名稱。請記下您選擇的名稱。
- 按一下「繼續」。
- 在「位置類型」下方,選取「區域」。這是費用最低的選項。
- 選擇與子網路相同的區域。將Google Cloud 資源放在相同區域可縮短延遲時間、提升速度、降低資料傳輸成本,並簡化網路。
- 按一下「繼續」。
- 點選「建立」。
- 如果系統顯示對話方塊,請保留選取的「強制禁止公開存取這個 bucket」,然後按一下「確認」。
系統會開啟新 bucket 的「Bucket details」(bucket 詳細資料) 頁面,並選取「Objects」(物件) 窗格。
將傾印檔案上傳至 bucket
將先前下載的範例 SQL dump 檔案上傳至 bucket。
- 在「物件」分頁中,依序點選「上傳」和「上傳檔案」。
- 找到並選取
countries-sqlserver.sql檔案。 - 按一下 [開啟]。Cloud Storage 會將 dump 檔案上傳至 bucket。
建立 Cloud SQL 執行個體
在 Google Cloud 控制台中,使用下列設定建立 Cloud SQL 執行個體。其他設定則保留預設值。雖然匯入程序不需要啟用私人 IP,但建議您為正式版工作負載使用私人 IP。
- 前往 Google Cloud 控制台的 Cloud SQL 執行個體頁面。
前往 Cloud SQL 執行個體頁面 - 點選「建立執行個體」。
- 按一下「Choose SQL Server」(選取 SQL Server)。
- 在「選擇 Cloud SQL 版本」部分,選擇「Enterprise」。
- 在「版本預設設定」部分,選取「沙箱」。
- 在「Instance ID」(執行個體 ID) 中輸入
tutorial-instance。 - 選擇並輸入預設使用者帳戶的密碼,然後儲存以供日後使用。
- 選擇與子網路和 bucket 相同的區域。
- 「可用區可用性」請選取「單一可用區」。
- 展開「顯示設定選項」。
- 展開「連線」。
- 清除「公開 IP」。
- 選取「私人 IP」。
- 從「網路」下拉式選單中,選取「tutorial-network」。這會將新的 Cloud SQL 執行個體放在您先前建立的私人網路中。
- 按一下「建立執行個體」,然後等待執行個體初始化並啟動。初始化程序可能需要五分鐘以上的時間。
建立目的地資料庫
匯入工作流程需要您選取匯入目的地資料庫,因此您必須建立空白資料庫。
- 在 SQL 導覽選單中,按一下「資料庫」。
- 按一下 [Create database] (建立資料庫)。
- 在「Database Name」(資料庫名稱) 中輸入
countries。 - 點選「建立」。
從傾印檔案匯入
現在,您可以使用上傳至 Cloud Storage bucket 的 countries dump 檔案,匯入 countries-sqlserver.sql 資料庫。
- 在 SQL 導覽選單中,按一下「總覽」。
- 在總覽頁面上,按一下「匯入」。
- 在「檔案格式」下方,選取「SQL」。
- 在「選取來源檔案」下方,選擇「從 Google Cloud Storage 選取檔案」。
- 按一下「瀏覽」。
- 展開您稍早建立的儲存空間 bucket。
- 按一下「countries-sqlserver.sql」。
- 按一下「選取」。
- 在「目的地」部分,按一下「資料庫」下拉式選單,然後選取「國家/地區」。
- 按一下「匯入」。
匯入程序完成後,系統會顯示成功訊息,表示 countries 資料庫已匯入 SQL Server 適用的 Cloud SQL。
驗證 SQL Server 適用的 Cloud SQL 中匯入的資料
匯入作業完成後,您可以透過 Cloud SQL Studio 連線至資料庫、檢查資料表清單,並對資料執行測試查詢,確認資料庫已匯入。
向 Cloud SQL Studio 進行驗證
使用先前建立的使用者帳戶,透過 Cloud SQL Studio 連線至新資料庫。
- 在 SQL 導覽選單中,按一下「Cloud SQL Studio」。系統會顯示登入對話方塊。
- 在「資料庫」下拉式選單中,選擇「國家/地區」。
- 選取「內建資料庫驗證」。
- 在「User」(使用者) 下拉式選單中,選取「sqlserver」。
- 在「Password」(密碼) 欄位中,輸入您在建立執行個體時指定的密碼。
- 按一下「驗證」。Cloud SQL Studio 隨即開啟。
查看及查詢資料表
- 在「Explorer」窗格中檢查
countries資料庫,並確認資料庫有兩個資料表:capitals和country_codes。 - 按一下「未命名的查詢」開啟查詢編輯器。
將下列程式碼貼到查詢編輯器:
SELECT [capitals].[country_capital], [country_codes].[country_name] FROM [capitals] JOIN [country_codes] ON [capitals].[alpha_2_code] = [country_codes].[alpha_2_code] ORDER BY [capitals].[country_capital];按一下「執行」。
結果窗格會依字母順序顯示世界各國首都和所屬國家/地區的清單。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取這個教學課程所用資源的費用,請執行下列其中一項操作:
- 刪除內含資源的專案
- 保留專案並刪除個別資源。
刪除專案
刪除 Google Cloud 專案:
gcloud projects delete PROJECT_ID
刪除個別資源
如要保留專案但避免產生費用,請刪除您在教學課程期間建立的 Cloud SQL 執行個體、Cloud Storage bucket 和虛擬私有雲網路。
刪除 Cloud SQL 執行個體
請先停用防刪除功能,然後刪除教學課程 Cloud SQL 執行個體:
- 在 SQL 導覽選單中,按一下「總覽」。
- 按一下 [編輯]。
- 展開「資料保護」部分。
- 在「執行個體防刪除功能」中,取消選取所有選項。
- 按一下 [儲存]。作業完成後,即可選取「刪除」。
- 按一下「Delete」(刪除)。系統隨即會顯示對話方塊。
- 在「Instance ID」(執行個體 ID) 欄位中輸入
tutorial-instance。 - 點選「刪除」。
刪除 Cloud Storage 值區
接著,刪除儲存空間 bucket 和當中的內容。
- 在主 Google Cloud 控制台導覽選單中,依序前往「Cloud Storage」>「Buckets」。
- 找出您先前建立的值區,然後選取名稱旁的方塊。
- 按一下「刪除」圖示 。
- 在對話方塊中輸入
DELETE確認刪除,然後按一下「刪除」。
刪除虛擬私人雲端網路
您已刪除虛擬私有雲網路中的資源,現在可以刪除網路。如要刪除虛擬私有雲網路,請先刪除建立虛擬私有雲網路時自動建立的對等互連連線。
- 在主導覽選單中,依序前往「虛擬私有雲網路」 >「虛擬私有雲網路對等互連」。
- 選取與 VPC 網路「tutorial-network」對接的連線旁的方塊。
- 點選「刪除」。
- 在對話方塊中按一下「刪除」,確認刪除。
對等互連連線移除後,您就可以刪除 VPC 網路。
- 在「虛擬私有雲網路」選單中,按一下「虛擬私有雲網路」。
- 按一下「tutorial-network」,開啟詳細資料頁面。
- 按一下「刪除虛擬私有雲網路」。
- 在對話方塊中輸入
tutorial-network,然後按一下「刪除」,確認刪除。
後續步驟
- 匯入及匯出資料的最佳做法。
- 使用 SQL 傾印檔案匯出及匯入。
- 虛擬私人雲端網路
- 查看 Google Cloud 的參考架構、圖表和最佳做法。歡迎瀏覽我們的 Cloud Architecture Center。