יצירה של מסד נתונים ב-AlloyDB ושליחת שאילתות אליו באמצעות מסוף Google Cloud
כדי להתחיל להשתמש ב-AlloyDB ל-PostgreSQL במסוף Google Cloud , אתם יכולים ליצור אשכול ומכונה ראשית, להתחבר למכונה באמצעות AlloyDB Studio, להוסיף נתונים לדוגמה ולשלוח שאילתות. ב-AlloyDB, מסדי הנתונים מאוחסנים באשכולות שמכילים מכונות. מידע נוסף על הארכיטקטורה, האשכולות והמכונות של AlloyDB זמין בסקירה הכללית של AlloyDB.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloud ליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
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.enablepermission. 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.-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
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.enablepermission. 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.-
אם אתם מתכננים להגדיר קישוריות רשת ל-AlloyDB באמצעות רשת VPC שנמצאת באותו פרויקט ב- Google Cloud כמו AlloyDB, אתם צריכים את Service Networking API.
אם אתם מתכננים להגדיר קישוריות רשת ל-AlloyDB באמצעות רשת VPC שנמצאת בפרויקט אחר ב- Google Cloud , אתם צריכים את Compute Engine API ואת Cloud Resource Manager API.
- בוחרים אפשרות קישוריות.
התפקידים הנדרשים
כדי לקבל את ההרשאות שדרושות ליצירת מסד נתונים ב-AlloyDB ולהתחברות אליו, בקשו מהאדמין להקצות לכם את התפקידים הבאים ב-IAM בפרויקט:
- אדמין של רשת Compute (
roles/compute.networkAdmin) - אדמין ב-AlloyDB (
roles/alloydb.admin)
להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.
אלה תפקידים מוגדרים מראש שכוללים את ההרשאות שנדרשות כדי ליצור מסד נתונים ב-AlloyDB ולהתחבר אליו. כדי לראות בדיוק אילו הרשאות נדרשות, אפשר להרחיב את הקטע ההרשאות הנדרשות:
ההרשאות הנדרשות
כדי ליצור מסד נתונים ב-AlloyDB ולהתחבר אליו, צריך את ההרשאות הבאות:
-
compute.networks.list -
compute.networks.addPeering -
compute.addresses.create -
servicenetworking.services.addPeering -
alloydb.clusters.create -
alloydb.instances.create -
alloydb.instances.login
יכול להיות שתקבלו את ההרשאות האלה באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש אחרים.
יצירת אשכול ומכונה ראשית של AlloyDB
נכנסים לדף Clusters במסוף Google Cloud .
לוחצים על Create cluster.
בקטע Configure your cluster, מבצעים את הפעולות הבאות:
בשדה Cluster ID, מזינים
my-cluster.מזינים סיסמה. שימו לב לסיסמה הזו, כי תצטרכו להשתמש בה במדריך למתחילים.
משאירים את גרסת ברירת המחדל של מסד הנתונים.
בתפריט Region בקטע Location, בוחרים את האזור
us-central1 (Iowa).
בקטע Configure your primary instance, פועלים לפי השלבים הבאים:
בשדה Instance ID, מזינים את הערך
my-cluster-primary.בקטע Zonal availability, בוחרים באפשרות Single zone.
בתפריט Machine type, בוחרים את סוג המכונה
2 vCPU, 16 GB.בתפריט רשת בקטע קישוריות, בוחרים את הרשת
default.אם הגדרתם חיבור לגישה לשירותים פרטיים, המשיכו לשלב הבא. אם לא, לוחצים על הגדרת חיבור ופועלים לפי השלבים הבאים:
בקטע Allocate an IP range, בוחרים באפשרות Use an automatically allocated IP range.
לוחצים על המשך ואז על יצירת חיבור.
שומרים את הגדרות ברירת המחדל בקטע Network security.
לוחצים על Create cluster. יכול להיות שיחלפו כמה דקות עד ש-AlloyDB ייצור את האשכול ויציג אותו בדף Overview של האשכול הראשי.
התחברות למכונה ויצירת מסד נתונים ב-AlloyDB Studio
נכנסים לדף Clusters במסוף Google Cloud .
בעמודה Resource name, לוחצים על שם האשכול,
my-cluster.בחלונית הניווט, לוחצים על AlloyDB Studio.
בדף Sign in to AlloyDB Studio, פועלים לפי השלבים הבאים:
בוחרים שיטת אימות למסד הנתונים.
בוחרים את מסד הנתונים
postgres.לוחצים על אימות. בחלונית Explorer מוצגת רשימה של האובייקטים במסד הנתונים
postgres.
כדי ליצור מסד נתונים, מזינים את ההצהרה הבאה בכרטיסייה Editor 1 ולוחצים על Run:
CREATE DATABASE guestbook;מסד הנתונים נוצר כשההודעה
Statement executed successfullyמופיעה בחלונית Results.
התחברות למסד הנתונים של guestbook
נכנסים לדף Clusters במסוף Google Cloud .
בעמודה Resource name, לוחצים על שם האשכול,
my-cluster.בחלונית הניווט, לוחצים על AlloyDB Studio.
בדף Sign in to AlloyDB Studio, פועלים לפי השלבים הבאים:
בוחרים את מסד הנתונים
guestbook.בוחרים את המשתמש
postgres.מזינים את הסיסמה שיצרתם ביצירת אשכול ומכונה ראשית של AlloyDB.
לוחצים על אימות. בחלונית Explorer מוצגת רשימה של האובייקטים במסד הנתונים
guestbook.
אימות החיבור למסד הנתונים
פועלים לפי השלבים שמפורטים במאמר התחברות למסד הנתונים של
guestbook.כדי להוסיף נתונים לדוגמה למסד הנתונים
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!');לוחצים על Run. ההודעה
Statement executed successfullyבחלונית Results מציינת שהנתונים נוספו למסד הנתונים.כדי להסיר טקסט מהכרטיסייה Editor 1 כדי להריץ פקודה אחרת, לוחצים על Clear.
כדי לבחור את כל הרשומות במסד הנתונים, מזינים את השאילתה הבאה בכרטיסייה Editor 1:
SELECT * FROM entries;לוחצים על Run. בחלונית Results יופיע פלט דומה לזה:
guestname | content | entryid --------------+-------------+--------- Alex | I got here! | 1 Kai | Me too! | 2
הסרת המשאבים
כדי לא לצבור חיובים בחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, מבצעים את הפעולות הבאות.
נכנסים לדף Clusters במסוף Google Cloud .
בעמודה Resource name, לוחצים על שם האשכול,
my-cluster.לוחצים על delete Delete cluster.
בתיבת הדו-שיח Delete cluster my-cluster, מזינים
my-clusterכדי לאשר את מחיקת האשכול.לוחצים על Delete.
אופציונלי: אם יצרתם חיבור לגישה לשירותים פרטיים כשיצרתם את האשכול, ואתם לא צריכים יותר את טווח כתובות ה-IP שהוקצה, בצעו את השלבים שמפורטים בקטע הסרת טווח שהוקצה לגישה לשירותים פרטיים.