如果您符合以下条件,本教程将对您有所帮助:
- 在 Cloud Run 上托管应用。
- 使用 Cloud SQL 作为数据库。
- 希望使用 phpMyAdmin 作为 MySQL 的界面,或者更倾向于使用网页界面执行数据库管理。
前提条件
- 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.
-
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 theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Install the gcloud CLI.
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
-
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 theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Install the gcloud CLI.
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
- 创建一个使用专用 IP 的 Cloud SQL 第二代实例。
- 创建数据库用户。
在 Google Cloud 控制台中,前往 Cloud Run:
选择创建服务。
- 在容器映像网址字段中,输入
phpmyadmin
。Cloud Run 会从公共 Docker Hub 中拉取映像。 - 在服务名称字段中,输入
phpmyadmin
。 - 勾选
Require authentication
。 - 点击容器、卷、网络、安全性以设置以下选项:
- 在端口字段中,输入
80
作为容器端口的值。 - 在环境变量中,添加
PMA_HOST
并将值设置为 Cloud SQL 实例的内部 IP 地址。
- 在端口字段中,输入
在网络部分,选择连接到 VPC 以接收出站流量和将流量直接发送到 VPC。选择与 Cloud SQL 实例相同的网络。
- 在容器映像网址字段中,输入
如需在之前创建的服务上启动代理,请使用以下命令:
gcloud run services proxy phpmyadmin --port=8080
如果 gcloud CLI 提示您安装
cloud-run-proxy
组件,请输入Y
。如需测试设置,请输入网址和端口号(例如
http://127.0.0.1:8080/
),然后输入用户名(管理员名称)和密码。由于您使用本地 Cloud Run 开发者代理安全地访问服务,因此您可能会看到错误消息,该消息可以忽略:There is a mismatch between HTTPS indicated on the server and client
。
使用 phpMyAdmin 映像创建服务
控制台
使用 Cloud Run 代理连接到服务
为了安全地访问受 IAM 保护的服务,请使用 Cloud Run 开发者代理。
问题排查
如需获取有关 Cloud Run 错误的信息,请访问Google Cloud 控制台中的 Cloud Run 日志。