Share via


搭配適用於 MySQL 的 Azure 資料庫 - 單一伺服器一起使用的 Azure Pipelines

適用於: 適用於 MySQL 的 Azure 資料庫 - 單一伺服器

使用 Azure Pipelines 部署資料庫更新,以開始使用適用於 MySQL 的 Azure 資料庫。 Azure Pipelines 可讓您使用 Azure DevOps 建置、測試及部署持續整合 (CI) 與持續傳遞 (CD)。

您將使用適用於 MySQL 的 Azure 資料庫部署工作。 適用於 MySQL 的 Azure 資料庫部署工作只能與適用於 MySQL 單一伺服器的 Azure 資料庫搭配使用。

必要條件

在開始之前,您需要:

本快速入門使用在以下任一指南中建立的資源作為起點︰

建立管線

您將使用基本入門管線,作為管線的基礎。

  1. 登入您的 Azure DevOps 組織並前往您的專案。

  2. 在您的專案中,瀏覽至 Pipelines 頁面。 然後,選擇用來建立新管線的動作。

  3. 先選取 [GitHub] 作為原始程式碼的位置,以逐步完成精靈的步驟。

  4. 系統可能會將您重新導向至 GitHub 以進行登入。 若是如此,請輸入 GitHub 認證。

  5. 在存放庫清單出現時,選取您所需的存放庫。

  6. Azure Pipelines 會分析您的存放庫並提供組態選項。 選取 [入門管線]。

    Screenshot of Select Starter pipeline.

建立祕密

您必須知道資料庫伺服器名稱、SQL 使用者名稱和 SQL 密碼,才能與適用於 MySQL 的 Azure 資料庫部署工作搭配使用。

基於安全考量,您會想要將 SQL 密碼儲存為管線設定 UI 中的秘密變數。

  1. 前往 [管線] 頁面,選取適當的管線,然後選取 [編輯]

  2. 選取變數

  3. 新增名為 SQLpass 的新變數,然後選取 [保留此值秘密] 來加密並儲存變數

    Screenshot of adding a secret variable.

  4. 選取 [確定] 和 [儲存] 即可新增變數。

驗證資料庫的權限

若要使用 Azure Pipelines 存取 MySQL 資料庫,您必須將資料庫設定為接受所有 Azure 資源的連線。

  1. 在 Azure 入口網站中,開啟您的資料庫資源。

  2. 選取 [連線安全性]

  3. 將 [允許存取 Azure 服務] 設定為 [是]。

    Screenshot of setting MySQL to allow Azure connections.

新增適用於 MySQL 的 Azure 資料庫部署工作

在此範例中,我們將建立名為 quickstartdb 的新資料庫,並新增詳細目錄資料表。 內嵌 SQL 指令碼將會:

  • 刪除 quickstartdb (如果存在的話),並建立新的 quickstartdb 資料庫。
  • 刪除資料表 inventory (如果存在的話),並建立新的 inventory 資料表。
  • 將三個列插入 inventory
  • 顯示所有資料列。
  • 更新 inventory 中第一個資料列的值。
  • 刪除 inventory 中的第二個資料列。

您必須取代部署工作中的下列值。

輸入 描述 範例
azureSubscription 使用服務連線,向 Azure 訂閱進行驗證。 My Subscription
ServerName 適用於 MySQL 伺服器的 Azure 資料庫名稱。 fabrikam.mysql.database.azure.com
SqlUsername 適用於 MySQL 的 Azure 資料庫使用者名稱。 mysqladmin@fabrikam
SqlPassword 使用者名稱的密碼。 這應該定義為秘密變數。 $(SQLpass)

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- task: AzureMysqlDeployment@1
  inputs:
    azureSubscription: '<your-subscription>
    ServerName: '<db>.mysql.database.azure.com'
    SqlUsername: '<username>@<db>'
    SqlPassword: '$(SQLpass)'
    TaskNameSelector: 'InlineSqlTask'
    SqlInline: |
      DROP DATABASE IF EXISTS quickstartdb;
      CREATE DATABASE quickstartdb;
      USE quickstartdb;
      
      -- Create a table and insert rows
      DROP TABLE IF EXISTS inventory;
      CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);
      INSERT INTO inventory (name, quantity) VALUES ('banana', 150);
      INSERT INTO inventory (name, quantity) VALUES ('orange', 154);
      INSERT INTO inventory (name, quantity) VALUES ('apple', 100);
      
      -- Read
      SELECT * FROM inventory;
      
      -- Update
      UPDATE inventory SET quantity = 200 WHERE id = 1;
      SELECT * FROM inventory;
      
      -- Delete
      DELETE FROM inventory WHERE id = 2;
      SELECT * FROM inventory;
    IpDetectionMethod: 'AutoDetect'

部署及驗證資源

選取 [儲存並執行] 以部署管線。 管線作業將會啟動,在幾分鐘後,作業狀態應該會表示 Success

您可以確認管線在 AzureMysqlDeployment 工作內已成功執行。

開啟工作,並確認最後兩個項目在 inventory 中會顯示兩個資料列。 因為已刪除第二個資料列,所以會有兩個資料列。

Screenshot to show reviewing final table results.

清除資源

當您完成使用管線時,請在適用於 MySQL 的 Azure 資料庫中刪除 quickstartdb。 您也可以刪除您所建立的部署管線。

下一步