שימוש ב-phpMyAdmin עם Cloud SQL בסביבה רגילה של App Engine

איך מתקינים את phpMyAdmin בסביבת ברירת המחדל ב-App Engine אפשר להשתמש ב-phpMyAdmin כדי לנהל את Cloud SQL דרך האינטרנט.

המדריך הזה יעזור לכם אם:

  • הרצת אפליקציות ב-App Engine.
  • משתמשים ב-Cloud SQL כמסד הנתונים.
  • אתם משתמשים ב-phpMyAdmin כממשק ל-MySQL או שאתם מעדיפים ממשק אינטרנט לניהול מסד נתונים.

אם אתם משתמשים ב-Compute Engine, כדאי לכם להשתמש באחד ממוצרי אוסף כלי הפיתוח או המוצרים שזמינים דרך Click to Deploy. פריסות של מחסניות שכוללות MySQL, כמו LAMP ו-LEMP, או מוצרים כמו Drupal, מספקות אפשרות להתקין את phpMyAdmin כחלק מהפריסה.

מטרות

  • פריסת phpMyAdmin בסביבת ברירת המחדל ב-App Engine.

עלויות

במדריך הזה נעשה שימוש ברכיבים של Cloud Platform שחלים עליהם חיובים, כולל:

  • App Engine
  • Cloud SQL

אפשר להשתמש במחשבון עלויות כדי ליצור הערכת עלויות בהתאם לשימוש החזוי.

משתמשים חדשים ב-Cloud Platform עשויים להיות זכאים לתקופת ניסיון בחינם.

לפני שמתחילים

  1. נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloud ליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  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 role (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. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  6. To initialize the gcloud CLI, run the following command:

    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 role (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. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  11. To initialize the gcloud CLI, run the following command:

    gcloud init
  12. יצירת מכונה של Cloud SQL מהדור השני.
  13. (אופציונלי) פורסים אפליקציית App Engine שמשתמשת במופע Cloud SQL או בוחרים אפליקציה קיימת.

    לדוגמה, יוצרים ומפעילים את דוגמת ספר האורחים. אפשר לפרוס את 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 מציין איך נתיבי כתובות ה-URL תואמים לטיפול בבקשות ולקבצים סטטיים. הוא כולל גם מידע על קוד האפליקציה, כמו מזהה האפליקציה ומזהה הגרסה העדכנית. כדי ליצור את הקובץ:

  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'. זה בסדר למטרות המדריך הזה אם אתם מנסים את phpMyAdmin ב-App Engine.

    המדריך הזה מתאים רק לסביבת ברירת המחדל ב-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 ומריצים את הפקודה הבאה כדי לקבל מחרוזת אקראית בשביל blowfish:

    php -r "echo password_hash(uniqid(), PASSWORD_BCRYPT).PHP_EOL;"
    
  4. מדביקים את הסוד החדש במקום {{your_secret}} ב-config.inc.php.

  5. נכנסים אל דף המכונות של Cloud SQL במסוף Google Cloud .

  6. לוחצים על המכונה של Cloud SQL כדי להציג את דף פרטי המכונה.

  7. מחליפים את הערך של {{your_connection_string}} (בתוך המשתנה $host) במאפיין Instance connection name.

  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. מריצים את הפקודה הבאה מתוך הספרייה phpMyAdmin שבה נמצא הקובץ app.yaml כדי לפרוס את האפליקציה:

    gcloud app deploy
    

    הפקודה הזו פורסת את האפליקציה בשירות phpMyAdmin, כפי שצוין בקובץ app.yaml. פריסה לשירות נפרד עוזרת להבטיח ש-phpMyAdmin יפעל באותו מרכז נתונים כמו האפליקציה הראשית, וכך לשפר את הביצועים. מידע נוסף על פריסת האפליקציה משורת הפקודה זמין במאמר בנושא פריסת אפליקציית PHP.

התחברות ל-phpMyAdmin

עכשיו אפשר להיכנס ל-phpMyAdmin.

  1. בדפדפן האינטרנט, מזינים את כתובת ה-URL של phpMyAdmin כדי לפתוח את דף קבלת הפנים, ומשנים את כתובת ה-URL כך שתכלול את מזהה האפליקציה.

        https://phpmyadmin-dot-[YOUR_APP_ID].appspot.com
    
  2. בשדה Username (שם משתמש), מזינים root.

  3. מזינים את סיסמת הבסיס שציינתם כשקבעתם את הגדרות חשבון הבסיס.

  4. לוחצים על Go (מעבר).

במהלך פיתוח אפליקציית App Engine, חשוב לזכור להגן באמצעות סיסמה על כל חשבון משתמש שיוצרים כדי לגשת למסדי נתונים ב-Cloud SQL.

פתרון בעיות

‫App Engine משתמש ב-Cloud SQL Auth Proxy כדי להתחבר למכונות Cloud SQL מהדור השני. מידע נוסף על האופן שבו Cloud SQL Auth Proxy פועל זמין במאמר מידע על Cloud SQL Auth Proxy.

היומנים של App Engine במסוףGoogle Cloud יכולים לספק מידע על שגיאות ב-App Engine.

הסרת המשאבים

אחרי שמסיימים את המדריך, אפשר למחוק את המשאבים שנוצרו, כדי שהם יפסיקו להשתמש במכסה ולצבור חיובים. בסעיפים הבאים מוסבר איך למחוק או להשבית את המשאבים האלו.

מחיקת הפרויקט

הדרך הקלה ביותר לבטל את החיוב היא למחוק את הפרויקט שיצרתם בשביל המדריך הזה.

כדי למחוק את הפרויקט:

  1. במסוף Google Cloud , נכנסים לדף Manage resources.

    כניסה לדף Manage resources

  2. ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
  3. כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.

מחיקת מכונות

כדי למחוק מכונה של Cloud SQL:

  1. נכנסים לדף Instances במסוף Google Cloud .

    כניסה לדף Instances

  2. לוחצים על השם של מכונת ה-SQL שרוצים למחוק.
  3. כדי למחוק את המכונה, לוחצים על Delete ופועלים לפי ההוראות.

המאמרים הבאים