יצירה של מסד נתונים ב-AlloyDB ושליחת שאילתות אליו באמצעות מסוף Google Cloud

כדי להתחיל להשתמש ב-AlloyDB ל-PostgreSQL במסוף Google Cloud , אתם יכולים ליצור אשכול ומכונה ראשית, להתחבר למכונה באמצעות AlloyDB Studio, להוסיף נתונים לדוגמה ולשלוח שאילתות. ב-AlloyDB, מסדי הנתונים מאוחסנים באשכולות שמכילים מכונות. מידע נוסף על הארכיטקטורה, האשכולות והמכונות של AlloyDB זמין בסקירה הכללית של AlloyDB.

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

  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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  5. Enable the Compute Engine, AlloyDB, Cloud Resource Manager, and Service Networking APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  6. 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

  7. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  9. Enable the Compute Engine, AlloyDB, Cloud Resource Manager, and Service Networking APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  10. אם אתם מתכננים להגדיר קישוריות רשת ל-AlloyDB באמצעות רשת VPC שנמצאת באותו פרויקט ב- Google Cloud כמו AlloyDB, אתם צריכים את Service Networking API.

    אם אתם מתכננים להגדיר קישוריות רשת ל-AlloyDB באמצעות רשת VPC שנמצאת בפרויקט אחר ב- Google Cloud , אתם צריכים את Compute Engine API ואת Cloud Resource Manager API.

  11. בוחרים אפשרות קישוריות.

התפקידים הנדרשים

כדי לקבל את ההרשאות שדרושות ליצירת מסד נתונים ב-AlloyDB ולהתחברות אליו, בקשו מהאדמין להקצות לכם את התפקידים הבאים ב-IAM בפרויקט:

להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

אלה תפקידים מוגדרים מראש שכוללים את ההרשאות שנדרשות כדי ליצור מסד נתונים ב-AlloyDB ולהתחבר אליו. כדי לראות בדיוק אילו הרשאות נדרשות, אפשר להרחיב את הקטע ההרשאות הנדרשות:

ההרשאות הנדרשות

כדי ליצור מסד נתונים ב-AlloyDB ולהתחבר אליו, צריך את ההרשאות הבאות:

  • compute.networks.list
  • compute.networks.addPeering
  • compute.addresses.create
  • servicenetworking.services.addPeering
  • alloydb.clusters.create
  • alloydb.instances.create
  • alloydb.instances.login

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

יצירת אשכול ומכונה ראשית של AlloyDB

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

    כניסה לדף Clusters

  2. לוחצים על Create cluster.

  3. בקטע Configure your cluster, מבצעים את הפעולות הבאות:

    1. בשדה Cluster ID, מזינים my-cluster.

    2. מזינים סיסמה. שימו לב לסיסמה הזו, כי תצטרכו להשתמש בה במדריך למתחילים.

    3. משאירים את גרסת ברירת המחדל של מסד הנתונים.

    4. בתפריט Region בקטע Location, בוחרים את האזור us-central1 (Iowa).

  4. בקטע Configure your primary instance, פועלים לפי השלבים הבאים:

    1. בשדה Instance ID, מזינים את הערך my-cluster-primary.

    2. בקטע Zonal availability, בוחרים באפשרות Single zone.

    3. בתפריט Machine type, בוחרים את סוג המכונה 2 vCPU, 16 GB.

    4. בתפריט רשת בקטע קישוריות, בוחרים את הרשת default.

      אם הגדרתם חיבור לגישה לשירותים פרטיים, המשיכו לשלב הבא. אם לא, לוחצים על הגדרת חיבור ופועלים לפי השלבים הבאים:

      1. בקטע Allocate an IP range, בוחרים באפשרות Use an automatically allocated IP range.

      2. לוחצים על המשך ואז על יצירת חיבור.

    5. שומרים את הגדרות ברירת המחדל בקטע Network security.

  5. לוחצים על Create cluster. יכול להיות שיחלפו כמה דקות עד ש-AlloyDB ייצור את האשכול ויציג אותו בדף Overview של האשכול הראשי.

התחברות למכונה ויצירת מסד נתונים ב-AlloyDB Studio

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

    כניסה לדף Clusters

  2. בעמודה Resource name, לוחצים על שם האשכול, my-cluster.

  3. בחלונית הניווט, לוחצים על AlloyDB Studio.

  4. בדף Sign in to AlloyDB Studio, פועלים לפי השלבים הבאים:

    1. בוחרים שיטת אימות למסד הנתונים.

    2. בוחרים את מסד הנתונים postgres.

    3. לוחצים על אימות. בחלונית Explorer מוצגת רשימה של האובייקטים במסד הנתונים postgres.

  5. כדי ליצור מסד נתונים, מזינים את ההצהרה הבאה בכרטיסייה Editor 1 ולוחצים על Run:

    CREATE DATABASE guestbook;
    

    מסד הנתונים נוצר כשההודעה Statement executed successfully מופיעה בחלונית Results.

התחברות למסד הנתונים של guestbook

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

    כניסה לדף Clusters

  2. בעמודה Resource name, לוחצים על שם האשכול, my-cluster.

  3. בחלונית הניווט, לוחצים על AlloyDB Studio.

  4. בדף Sign in to AlloyDB Studio, פועלים לפי השלבים הבאים:

    1. בוחרים את מסד הנתונים guestbook.

    2. בוחרים את המשתמש postgres.

    3. מזינים את הסיסמה שיצרתם ביצירת אשכול ומכונה ראשית של AlloyDB.

    4. לוחצים על אימות. בחלונית Explorer מוצגת רשימה של האובייקטים במסד הנתונים guestbook.

אימות החיבור למסד הנתונים

  1. פועלים לפי השלבים שמפורטים במאמר התחברות למסד הנתונים של guestbook.

  2. כדי להוסיף נתונים לדוגמה למסד הנתונים guestbook, מזינים את הקוד הבא בכרטיסייה Editor 1:

    CREATE TABLE entries (guestName VARCHAR(255),
                          content VARCHAR(255),
                          entryID SERIAL PRIMARY KEY);
    INSERT INTO entries (guestName, content) values ('Alex', 'I got here!');
    INSERT INTO entries (guestName, content) values ('Kai', 'Me too!');
    
  3. לוחצים על Run. ההודעה Statement executed successfully בחלונית Results מציינת שהנתונים נוספו למסד הנתונים.

  4. כדי להסיר טקסט מהכרטיסייה Editor 1 כדי להריץ פקודה אחרת, לוחצים על Clear.

  5. כדי לבחור את כל הרשומות במסד הנתונים, מזינים את השאילתה הבאה בכרטיסייה Editor 1:

    SELECT * FROM entries;
    
  6. לוחצים על Run. בחלונית Results יופיע פלט דומה לזה:

        guestname     | content     | entryid
        --------------+-------------+---------
        Alex          | I got here! | 1
        Kai           | Me too!     | 2
    

הסרת המשאבים

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

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

    כניסה לדף Clusters

  2. בעמודה Resource name, לוחצים על שם האשכול, my-cluster.

  3. לוחצים על Delete cluster.

  4. בתיבת הדו-שיח Delete cluster my-cluster, מזינים my-cluster כדי לאשר את מחיקת האשכול.

  5. לוחצים על Delete.

  6. אופציונלי: אם יצרתם חיבור לגישה לשירותים פרטיים כשיצרתם את האשכול, ואתם לא צריכים יותר את טווח כתובות ה-IP שהוקצה, בצעו את השלבים שמפורטים בקטע הסרת טווח שהוקצה לגישה לשירותים פרטיים.

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