Hide
Google Cloud SQL

Using phpMyAdmin with Google Cloud SQL on Google App Engine

Learn how to install phpMyAdmin on Google App Engine. You can use phpMyAdmin to administer Google Cloud SQL over the web.

You will find this tutorial helpful if you:

  • Run an application on Google App Engine for its platform-as-a-service (PaaS) features.
  • Use Google Cloud SQL as your database.
  • Prefer to use a web interface for database administration or you are already familiar with using phpMyAdmin as an interface for MySQL.

If you use Google Compute Engine for its infrastructure-as-a-service (IaaS) features, consider using one of the development stacks or products available through Click to Deploy. Deployments of stacks that include MySQL, such as LAMP and LEMP, or products such as Drupal, provide an option to install phpMyAdmin as part of the deployment.

Objectives

  • Deploy phpMyAdmin on Google App Engine.
  • Set user passwords for access to Cloud SQL.

Time required

After meeting the prerequisties, this tutorial takes approximately 15 minutes to complete.

Prerequisites

Download the phpMyAdmin source code

You'll deploy phpMyAdmin as a module of your App Engine application, so you must download the source code for phpMyAdmin. Follow these steps:

  1. In a terminal window, enter the following command to download the source code for phpMyAdmin version 4.3.9:

    $ wget http://downloads.sourceforge.net/project/phpmyadmin/phpMyAdmin/4.3.9/phpMyAdmin-4.3.9-all-languages.tar.bz2
    

    If you want to use a different version of phpMyAdmin, you can find the links to available versions on the phpMyAdmin Downloads page.

  2. Make a new directory. You will extract the files into this directory.

    $ mkdir phpMyAdmin
    
  3. Extract the files from the archive into the new directory.

    $ tar -xvf phpMyAdmin-4.3.9-all-languages.tar.bz2 -C phpMyAdmin --strip-components=1
    

Prepare the files for deployment

Deploying phpMyAdmin requires that you create three files: app.yaml, which contains the configuration information for Google App Engine; config.inc.php, which contains the configuration information for phpMyAdmin; and php.ini, which contains application-specific configuration for PHP.

Create app.yaml

The App Engine configuration file specifies how URL paths correspond to request handlers and static files. It also contains information about the application code, such as the application ID and the latest version identifier. Follow these steps to create the file:

  1. In the directory that you created, named phpMyAdmin, create a new file named app.yaml.

    $ cd phpMyAdmin
    $ touch app.yaml
    
  2. Using your favorite editor, paste the following text into app.yaml.

    application: <your-project-id>
    version: 1
    module: 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
    
    - url: /.*
      script: index.php
      login: admin
    
  3. Change the value for application to match your project ID.

  4. If you are deploying phpMyAdmin as the first and only application in App Engine, change the value for module from phpmyadmin to default.

    Normally, you would deploy phpMyAdmin as a module of an existing application and provide a name for the module. However, if you haven't yet deployed an application, then you are required to use the module name "default". That's fine for the purposes of this tutorial if you're just trying out phpMyAdmin on App Engine.

  5. Save the file.

Create config.inc.php

Follow these steps to create the phpMyAdmin configuration file.

  1. Create a new file named config.inc.php.

    $ touch config.inc.php
    
  2. Using your favorite editor, paste the following text into config.inc.php.

    <?php
    /* vim: set expandtab sw=4 ts=4 sts=4: */
    
    /*
     * 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-project-id>:<your-cloudsql-instance>';
    $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. In your web browser, open the blowfish secret generator.

  4. Copy the unique blowfish secret that the page generated. Be careful to copy the generated secret, not the example secret.
  5. Paste the new secret in place of <your-secret> in config.inc.php.
  6. Change the value for $host to use the project ID and name for your Google Cloud SQL instance. To find the instance name in the Developers Console, see the Cloud SQL instances page.
  7. Save the file.

Create php.ini

In its code, phpMyAdmin uses functions that are disabled by default in Google App Engine. Follow these steps to add a php.ini file so that App Engine enables the functions again:

  1. In the phpMyAdmin directory, create the file.

    $ touch php.ini
    
  2. Edit the file and add the following line:

    google_app_engine.enable_functions = "php_uname, getmypid"
    
  3. Save the file.

Deploy the application

Use the following commands to deploy your application to Google App Engine.

  1. Check for updates for your gcloud components. This will ensure that you're using the latest version.

    $ gcloud components update
    
  2. Deploy the application. Replace <your-app-id> with the correct app ID.

    $ appcfg.py -A <your-app-id> update .
    

This command deploys phpMyAdmin as a module of your application as specified in app.yaml. Deploying as a module helps to ensure that phpMyAdmin runs in the same data center as your main application, which makes for better performance.

Log in to phpMyAdmin

You can now log in to phpMyAdmin. The config.inc.php file that you deployed allowed for logging in without a password. For now, that's how you'll log in, until you set passwords in the next section.

  1. In your web browser, enter the URL for phpMyAdmin to open the welcome page. Be sure to change the URL to use your app ID.

    https://phpmyadmin-dot-<your-app-id>.appspot.com
    
  2. For Username, enter root.

  3. Leave Password blank.

  4. Click Go.

Set passwords

It's important to password-protect your Cloud SQL instance.

When you created your instance, Cloud SQL created the following four host combinations for root users and granted all global privileges to these users:

  • %
  • 127.0.0.1
  • ::1
  • localhost

The first host (%) represents remote access, such as when you connect to the database by using the MySQL command-line client with an IP address. You may have already set a password for this type of access in the Google Developers Console.

The other three hosts are various ways of representing a local connection, such as when phpMyAdmin connects to the database from App Engine. By default, connecting through localhost or its variants requires no password in Cloud SQL.

Until now, allowing connections as a local user with no password has not been an issue because it couldn't be done from outside of App Engine. Deploying phpMyAdmin changes that because it exposed a web interface from your App Engine application to Cloud SQL.

Follow these steps to add passwords for the root users:

  1. In phpMyAdmin, select the Users tab.
  2. For the first user, root %, click Edit Privileges. (If you have already set a password for root %, you can instead start editing the root 127.0.0.1 entry.)
  3. Click Change password.
  4. In the Password and Re-type boxes, enter the password.
  5. Click Go.
  6. Repeat these steps for the remaining localhost users.

You will now need to enter the password when you log in as root using phpMyAdmin. You can set or change the root % password at any time in the Google Developers Console, but doing so won't change the passwords for the localhost root users. Note that the user named Any has USAGE privileges, which is equivalent to no privileges. This is the appropriate setting and requires no passwords to be set.

As you develop your App Engine app, remember to password protect any user accounts that you create to access databases in Cloud SQL.

Next steps

Learn more about phpMyAdmin