在 App Engine 标准环境中将 phpMyAdmin 与 Cloud SQL 配合使用


了解如何在 App Engine 标准环境中安装 phpMyAdmin。您可以使用 phpMyAdmin 通过网络来管理 Cloud SQL。

如果您符合以下条件,本教程将对您有所帮助:

  • 在 App Engine 上运行应用。
  • 使用 Cloud SQL 作为数据库。
  • 使用 phpMyAdmin 作为 MySQL 的界面,或者您更倾向于使用网页界面执行数据库管理。

如果您使用 Compute Engine,请考虑使用一键部署中提供的某种开发技术栈或产品。部署包含 MySQL 的堆栈(例如 LAMPLEMP)或 Drupal 等产品时,您可以选择在部署过程中安装 phpMyAdmin。

目标

  • 在 App Engine 标准环境中部署 phpMyAdmin。

费用

本教程使用 Cloud Platform 的可计费组件,包括:

  • App Engine
  • Cloud SQL

请使用价格计算器根据您的预计使用情况来估算费用。Cloud Platform 新用户可能有资格免费试用

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 安装 Google Cloud CLI。
  5. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  6. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  7. 确保您的 Google Cloud 项目已启用结算功能

  8. 安装 Google Cloud CLI。
  9. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  10. 创建一个 Cloud SQL 第二代实例
  11. (可选)部署一个使用您的 Cloud SQL 实例的 App Engine 应用,或者选择一个现有应用。

    例如,创建和部署留言板示例。 尽管您可以单独部署 phpMyAdmin,但在现实场景中,您或许希望将其与某个 App Engine 应用配合使用。

下载 phpMyAdmin 源代码

您将把 phpMyAdmin 作为 App Engine 应用的一项服务而部署,因此您必须下载 phpMyAdmin 的源代码。请按照以下步骤操作:

  1. 在 Cloud Shell 终端中,输入以下命令以下载 phpMyAdmin 版本 4.9.5 的源代码:

    wget https://files.phpmyadmin.net/phpMyAdmin/4.9.5/phpMyAdmin-4.9.5-all-languages.tar.gz
    

    要使用其他版本的 phpMyAdmin,请使用 phpMyAdmin 下载页面上的可用版本链接。

  2. 新建一个目录。您会将文件解压缩到此目录中。

    mkdir phpMyAdmin
    
  3. 将文件从归档解压缩到新目录中。

    tar -xzvf phpMyAdmin-4.9.5-all-languages.tar.gz -C phpMyAdmin --strip-components=1
    

准备文件以便部署

部署 phpMyAdmin 要求您创建三个文件:app.yaml,其中包含 App Engine 的配置信息;config.inc.php,其中包含 phpMyAdmin 的配置信息;以及 php.ini,其中包含 PHP 的应用特定配置。

创建 app.yaml

App Engine 配置文件指定了网址路径与请求处理程序和静态文件的对应关系。它还包含有关应用代码的信息,例如,应用 ID 和最新版本标识符。请按照以下步骤创建文件:

  1. 在您创建的目录(名为 phpMyAdmin)中,新建一个名为 app.yaml 的新文件。

    cd phpMyAdmin
    touch app.yaml
    
  2. 使用您喜爱的编辑器,将以下文字粘贴到 app.yaml 中。

    service: phpmyadmin
    runtime: php55
    api_version: 1
    
    handlers:
    
    - url: /(.+\.(ico|jpg|png|gif))$
      static_files: \1
      upload: (.+\.(ico|jpg|png|gif))$
      application_readable: true
    
    - url: /(.+\.(htm|html|css|js))$
      static_files: \1
      upload: (.+\.(htm|html|css|js))$
      application_readable: true
    
    - url: /(.+\.php)$
      script: \1
      login: admin
    
    - url: /.*
      script: index.php
      login: admin
    
  3. 如果您要将 phpMyAdmin 部署为 App Engine 中的第一个也是唯一一个应用,请将 service 的值从 phpmyadmin 更改为 default

    通常,您会将 phpMyAdmin 部署为现有应用的一项服务,并为该服务提供一个名称。但是,如果您尚未部署应用,则需要使用服务名称“default”。如果您要在 App Engine 上试用 phpMyAdmin,则本教程非常适合您。

    本教程仅适用于 App Engine 标准环境。

  4. 保存文件。

创建 config.inc.php

按照以下步骤创建 phpMyAdmin 配置文件。

  1. 创建名为 config.inc.php 的新文件:

    touch config.inc.php
    
  2. 使用您喜爱的编辑器,将以下文字粘贴到 config.inc.php 中。

    <?php
    /**
     * Copyright 2016 Google Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    /*
     * This is needed for cookie based authentication to encrypt password in
     * cookie
     * http://www.question-defense.com/tools/phpmyadmin-blowfish-secret-generator
     */
    $cfg['blowfish_secret'] = '{{your_secret}}'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
    
    /*
     * Servers configuration
     */
    $i = 0;
    
    // Change this to use the project and instance that you've created.
    $host = '/cloudsql/{{your_connection_string}}';
    $type = 'socket';
    
    /*
    * First server
    */
    $i++;
    /* Authentication type */
    $cfg['Servers'][$i]['auth_type'] = 'cookie';
    /* Server parameters */
    $cfg['Servers'][$i]['socket'] = $host;
    $cfg['Servers'][$i]['connect_type'] = $type;
    $cfg['Servers'][$i]['compress'] = false;
    /* Select mysql if your server does not have mysqli */
    $cfg['Servers'][$i]['extension'] = 'mysqli';
    $cfg['Servers'][$i]['AllowNoPassword'] = true;
    /*
     * End of servers configuration
     */
    
    /*
     * Directories for saving/loading files from server
     */
    $cfg['UploadDir'] = '';
    $cfg['SaveDir'] = '';
    
    /*
    * Other settings
    */
    $cfg['PmaNoRelation_DisableWarning'] = true;
    $cfg['ExecTimeLimit'] = 60;
    $cfg['CheckConfigurationPermissions'] = false;
    
  3. 打开 Google Cloud Shell 并执行以下操作,为 burfish 获取一个随机字符串:

    php -r "echo password_hash(uniqid(), PASSWORD_BCRYPT).PHP_EOL;"
    
  4. 将新 Secret 粘贴到 config.inc.php 中,替换掉 {{your_secret}}

  5. 转到 Google Cloud 控制台中的“Cloud SQL 实例”页面

  6. 点击 Cloud SQL 实例,以显示其“实例详情”页面。

  7. 使用实例连接名称属性替换掉 {{your_connection_string}}(在 $host 变量中)的值。

  8. 保存文件。

创建 php.ini

在 phpMyAdmin 的代码中,使用了默认情况下在 App Engine 中被禁用的函数。 请按照以下步骤添加 php.ini 文件,以便 App Engine 重新启用这些函数:

  1. phpMyAdmin 目录中,创建该文件。

    touch php.ini
    
  2. 修改该文件并添加以下行:

    google_app_engine.enable_functions = "php_uname, getmypid"
    
  3. 保存文件。

部署应用

使用以下命令将您的应用部署到 App Engine。

  1. 检查您的 gcloud 组件是否有更新。

    gcloud components update
    
  2. 从您的 app.yaml 文件所在的 phpMyAdmin 目录内运行以下命令,以部署应用:

    gcloud app deploy
    

    此命令会按照您的 app.yaml 文件所指定的方式,将应用部署到 phpMyAdmin 服务。部署到单独的服务有助于确保 phpMyAdmin 与主应用在同一数据中心内运行,这能够提升性能。如需详细了解如何从命令行部署应用,请参阅部署 PHP 应用

登录 phpMyAdmin

您现在可以登录 phpMyAdmin。

  1. 在网络浏览器中,输入下面的 phpMyAdmin 的网址打开欢迎页面,请使用您的应用 ID 更改网址。

        https://phpmyadmin-dot-[YOUR_APP_ID].appspot.com
    
  2. 对于用户名,请输入 root

  3. 输入您在配置 root 账号时提供的 root 密码。

  4. 点击前往

在开发 App Engine 应用时,切记设置密码,以保护您为了访问 Cloud SQL 中的数据库而创建的任何用户账号。

问题排查

App Engine 使用 Cloud SQL Auth 代理连接到 Cloud SQL 第二代实例。 如需详细了解 Cloud SQL Auth 代理的工作原理,请参阅 Cloud SQL Auth 代理简介

Google Cloud 控制台中的 App Engine 日志可以提供有关 App Engine 错误的信息。

清理

完成本教程后,您可以清理您创建的资源,让它们停止使用配额,以免产生费用。以下部分介绍如何删除或关闭这些资源。

删除项目

为了避免产生费用,最简单的方法是删除您为本教程创建的项目。

如需删除项目,请执行以下操作:

  1. 在 Google Cloud 控制台中,进入管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

删除多个实例

要删除 Cloud SQL 实例,请执行以下操作:

  1. 在 Google Cloud 控制台中,转到实例页面。

    转到“实例”

  2. 点击要删除的 SQL 实例。
  3. 如需删除实例,请点击删除,然后按照说明操作。

后续步骤