在 Cloud Run 上將 phpMyAdmin 與 Cloud SQL 搭配使用

本指南說明如何在 Cloud Run 上安裝 phpMyAdmin。您可以使用 phpMyAdmin 來透過網路管理 Cloud SQL。

如果您執行下列操作,會發現這份教學課程非常有幫助:

  • 在 Cloud Run 上託管應用程式。
  • 使用 Cloud SQL 做為您的資料庫。
  • 想使用 phpMyAdmin 做為 MySQL 的介面,或者偏好使用網路介面執行資料庫管理。

必要條件

  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.

    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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Install the gcloud CLI.

  5. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  6. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  7. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  8. Verify that billing is enabled for your Google Cloud project.

  9. Install the gcloud CLI.

  10. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  11. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  12. 建立採用私人 IPCloud SQL 第二代執行個體
  13. 建立資料庫使用者
  14. 使用 phpMyAdmin 映像檔建立服務

    控制台

    1. 前往 Google Cloud 控制台的 Cloud Run:

      前往 Cloud Run

    2. 選取「建立服務」

      1. 在「Container image」(容器映像檔) 網址欄位中,輸入 phpmyadmin。Cloud Run 會從公開的 Docker Hub 提取映像檔。
      2. 在「Service name」(服務名稱) 欄位中輸入 phpmyadmin
      3. 請檢查 Require authentication
      4. 按一下「容器、磁碟區、網路與安全性」,設定下列選項:
        1. 在「Port」(通訊埠) 欄位中,輸入 80 做為容器通訊埠的值。
        2. 在「環境變數」中新增 PMA_HOST,並將值設為 Cloud SQL 執行個體的內部 IP 位址。
      5. 在「Networking」(網路) 中,選取「Connect to a VPC for outbound traffic」(連線至虛擬私有雲,以傳出流量) 和「Send traffic directly to a VPC」(直接將流量傳送至虛擬私有雲)。選擇與 Cloud SQL 執行個體相同的網路。

    使用 Cloud Run Proxy 連線至服務

    如要安全存取受 IAM 保護的服務,請使用 Cloud Run 開發人員 Proxy。

    1. 如要在先前建立的服務上啟動 Proxy,請使用下列指令:

      gcloud run services proxy phpmyadmin --port=8080
      

      如果 gcloud CLI 提示您安裝 cloud-run-proxy 元件,請輸入 Y

    2. 如要測試設定,請輸入網址和通訊埠號碼 (例如 http://127.0.0.1:8080/), 然後輸入使用者名稱 (管理員名稱) 和密碼。由於您使用本機 Cloud Run 開發人員 Proxy 安全地存取服務,因此可能會看到錯誤訊息,但可以忽略:There is a mismatch between HTTPS indicated on the server and client

    疑難排解

    如要取得 Cloud Run 錯誤的相關資訊,請在 Google Cloud 控制台中存取 Cloud Run 記錄