將工作負載從其他平台遷移至 MySQL 適用的 Cloud SQL 時,通常會使用 Google Cloud 控制台,從先前環境匯出的 SQL 轉儲檔案匯入資料。
本教學課程說明如何建立所需的 Google Cloud 資源,然後將 SQL 資料庫匯入 MySQL 適用的 Cloud SQL 執行個體。本教學課程示範遷移至 MySQL 適用的 Cloud SQL 時的最佳做法,包括使用具備私人服務存取權的虛擬私有雲 (VPC) 網路,以及為 Cloud SQL 執行個體啟用私人 IP。
執行步驟時,除非另有指定,否則請保留設定的預設值。
目標
- 下載 SQL dump 檔案範例。
- 建立新的虛擬私有雲網路,並啟用私人服務存取權。
- 建立 Cloud Storage bucket,並將 SQL dump 檔案上傳至該 bucket。
- 建立 MySQL 適用的 Cloud SQL 執行個體,並設定私人 IP。
- 建立使用者。
- 將 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, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). 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, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
必要的角色
如要取得完成本教學課程所需的權限,請要求管理員在專案中授予您下列 IAM 角色:
- Cloud SQL 管理員 (
roles/cloudsql.admin) - 儲存空間管理員 (
roles/storage.admin) - Compute 網路管理員 (
roles/compute.networkAdmin)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
取得範例資料庫 dump 檔案
在本教學課程中,您將使用包含國家/地區代碼和世界首都的小型範例資料庫。
將 countries 資料庫的 SQL 檔案下載到本機環境:countries-mysql.sql。
建立具備私人服務存取權的網路
建立其餘資源之前,請先建立虛擬私有雲網路,以便在當中執行服務。私人服務存取權可讓您使用內部 IPv4 位址,在外部網路和 MySQL 適用的 Cloud SQL 之間建立私人連線,限制 Cloud SQL 資料庫的存取權。
前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。
按一下「建立虛擬私有雲網路」。
在「名稱」欄位中輸入
tutorial-network。在「子網路建立模式」部分,選取「自訂」。
在「名稱」欄位中輸入
tutorial-subnet。選取鄰近區域。
在「IPv4 範圍」部分,輸入
10.0.0.0/24。在「Private Google Access」部分,選取「啟用」。
按一下「完成」。
按一下頁面底部的 [Create] (建立)。
虛擬私有雲網路建立程序完成後,您可以為網路設定私人服務存取權。
- 在「虛擬私有雲網路」畫面中,按一下「tutorial-network」。
- 在「tutorial-network」的選單列中,按一下「私人服務存取權」。
- 按一下「分配的服務 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 bucket
接著,建立 Cloud Storage bucket,用於儲存先前下載的 SQL dump 檔案。Cloud SQL 匯入工具預期 dump 檔案位於 bucket 中。Cloud Storage bucket 必須設定全域不重複名稱。
- 前往 Google Cloud 控制台的「Cloud Storage bucket」頁面。
- 點選「建立」。
- 建立全域不重複的 bucket 名稱,只能使用小寫英文字母、數字和連字號。建議您使用隨機字串產生器 (例如 random.org/strings) 來產生名稱。請記下您選擇的名稱。
- 按一下「繼續」。
- 在「位置類型」下方,選取「區域」。這是費用最低的選項。
- 選擇與子網路相同的區域。將Google Cloud 資源放在相同區域,可縮短延遲時間、提高速度、降低資料移轉費用,並簡化網路。
- 按一下「繼續」。
- 點選「建立」。
- 如果出現對話方塊,請保留「Enforce public access prevention on this bucket」(強制禁止公開存取這個值區) 的選取狀態,然後按一下「Confirm」(確認)。
新 bucket 的「Bucket details」(bucket 詳細資料) 頁面隨即開啟,並選取「Objects」(物件) 窗格。
將 dump 檔案上傳至 bucket
將先前下載的範例 SQL dump 檔案上傳至 bucket。
- 在「物件」分頁中,依序點選「上傳」和「上傳檔案」。
- 找出並選取
countries-mysql.sql檔案。 - 按一下 [開啟]。Cloud Storage 會將 dump 檔案上傳至 bucket。
建立 Cloud SQL 執行個體
使用下列設定,在 Google Cloud 控制台建立 Cloud SQL 執行個體。其他設定則保留預設值。雖然您不需要為匯入程序啟用私人 IP,但建議您在正式環境工作負載中使用私人 IP。
- 前往 Google Cloud 控制台的「Cloud SQL 執行個體」頁面。
前往 Cloud SQL 執行個體頁面 - 點選「建立執行個體」。
- 按一下「Choose MySQL」(選擇 MySQL)。
- 在「選擇 Cloud SQL 版本」部分,選取「Enterprise」。
- 在「版本預設設定」部分,選取「沙箱」。
- 在「Instance ID」(執行個體 ID) 中輸入
tutorial-instance。 - 選擇並輸入預設使用者帳戶的密碼,然後儲存密碼以供日後使用。
- 選擇與子網路和 bucket 相同的區域。
- 「可用區可用性」請選取「單一可用區」。
- 展開「顯示設定選項」。
- 展開「連線」。
- 清除「公開 IP」。
- 選取「私人 IP」。
- 在「網路」下拉式選單中,選取「tutorial-network」。這會將新的 Cloud SQL 執行個體,放在您先前建立的私人網路中。
- 按一下「Create instance」(建立執行個體),然後等待執行個體初始化並啟動。初始化程序可能需要超過五分鐘。
新增使用者
您必須先建立與超級使用者不同的資料庫使用者,才能讀取或寫入資料庫。
- 在 SQL 導覽選單中,按一下「Users」(使用者)。
- 按一下「Add user account」(新增使用者帳戶)。
- 在開啟的窗格中,選取「內建驗證」。
- 在「User name」(使用者名稱) 欄位中輸入
tutorial-user。 - 輸入新使用者的密碼。請儲存這個密碼,以便日後使用。
- 選取「允許任何主機」。
- 按一下「新增」。
從 dump 檔案匯入
現在可以匯入 countries 資料庫,方法是使用上傳至 Cloud Storage bucket 的 countries-mysql.sql dump 檔案。
- 在 SQL 導覽選單中,按一下「Overview」(總覽)。
- 在總覽頁面中,按一下「匯入」。
- 在「檔案格式」下方,選取「SQL」。
- 在「Select source file」(選取來源檔案) 底下,選擇「Select file from Google Cloud Storage」(從 Google Cloud Storage 選取檔案)。
- 按一下「瀏覽」。
- 展開稍早建立的儲存空間 bucket。
- 點選「countries-mysql.sql」。
- 按一下「選取」。
- 確認「Destination」(目的地) 區段的「Database」(資料庫) 欄位已選取「Specified in SQL file」(在 SQL 檔案中指定)。
- 按一下「匯入」。
匯入程序完成,且 countries 資料庫已匯入 MySQL 適用的 Cloud SQL 後,系統會顯示成功訊息。
在 MySQL 適用的 Cloud SQL 驗證匯入的資料
匯入作業完成後,您可以使用 Cloud SQL Studio 連線至資料庫、檢查資料表清單,並對資料執行測試查詢,確認資料庫已匯入。
向 Cloud SQL Studio 進行驗證
使用先前建立的使用者帳戶,透過 Cloud SQL Studio 連線至新資料庫。
- 在 SQL 導覽選單中,按一下「Cloud SQL Studio」。系統會顯示登入對話方塊。
- 在「資料庫」下拉式選單中,選擇「國家/地區」。
- 選取「內建資料庫驗證」。
- 在「User」(使用者) 下拉式選單中,選取「tutorial-user」。
- 在「Password」(密碼) 欄位中,輸入您在「新增使用者」一節中為使用者選擇的密碼。
- 按一下「Authenticate」(驗證)。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 導覽選單中,按一下「Overview」(總覽)。
- 按一下 [編輯]。
- 展開「資料保護」部分。
- 在「執行個體防刪除功能」中,取消選取所有選項。
- 按一下 [儲存]。作業完成後,即可選取「刪除」。
- 按一下「Delete」(刪除)。對話方塊隨即出現。
- 在「Instance ID」(執行個體 ID) 欄位中輸入
tutorial-instance。 - 點選「刪除」。
刪除 Cloud Storage bucket
接著,刪除儲存空間 bucket 和當中內容。
- 在 Google Cloud 控制台的主導覽選單中,依序前往「Cloud Storage」>「Buckets」(值區)。
- 找出您稍早建立的 bucket,然後勾選名稱旁的方塊。
- 按一下「刪除」圖示 。
- 在對話方塊中輸入
DELETE確認刪除,然後按一下「Delete」(刪除)。
刪除虛擬私有雲網路
刪除虛擬私有雲網路中的資源後,即可刪除網路。您必須先刪除建立虛擬私有雲網路時自動建立的對接連線,才能刪除虛擬私有雲網路。
- 在主要導覽選單中,依序前往「VPC networks」(虛擬私有雲網路) >「VPC network peering」(虛擬私有雲網路對接)。
- 找出與虛擬私有雲網路 tutorial-network 的對接連線,然後勾選旁邊的方塊。
- 點選「刪除」。
- 在對話方塊中,按一下「刪除」確認刪除。
移除對接連線後,即可刪除虛擬私有雲網路。
- 在「虛擬私有雲網路」選單中,點選「虛擬私有雲網路」。
- 按一下「tutorial-network」,開啟詳細資料頁面。
- 按一下「 刪除虛擬私有雲網路」。
- 在對話方塊中,輸入
tutorial-network並點選「Delete」(刪除),確認刪除。
後續步驟
- 匯入及匯出資料的最佳做法。
- 使用 SQL dump 檔案匯出及匯入。
- 虛擬私人雲端網路
- 探索 Google Cloud 的參考架構、圖表和最佳做法。歡迎瀏覽 Cloud Architecture Center。