1. บทนำ
ในโค้ดแล็บนี้ คุณจะได้เรียนรู้วิธีทำให้การเชื่อมต่อบริการส่วนตัวสำหรับ Cloud SQL พร้อมใช้งาน และวิธีเข้าถึงบริการ Cloud SQL โดยใช้ Private Service Connect ที่ติดตั้งใช้งาน .
ดูข้อมูลเพิ่มเติมเกี่ยวกับ Private Service Connect ได้ที่นี่
ข้อกำหนดเบื้องต้น
- ความเข้าใจพื้นฐานเกี่ยวกับ Google Cloud Console
- ทักษะพื้นฐานในอินเทอร์เฟซบรรทัดคำสั่งและ Google Cloud Shell
สิ่งที่คุณจะได้เรียนรู้
- วิธีทำให้อินสแตนซ์ Cloud SQL พร้อมใช้งาน
- วิธีทำให้ Private Service Connect ใช้งานได้
- วิธีเชื่อมต่อจาก VM กับอินสแตนซ์ Cloud SQL ผ่าน Private Service Connect
สิ่งที่ต้องมี
- บัญชี Google Cloud และโปรเจ็กต์ Google Cloud
- เว็บเบราว์เซอร์ เช่น Chrome
2. การตั้งค่าและข้อกําหนด
การตั้งค่าสภาพแวดล้อมด้วยตนเอง
- ลงชื่อเข้าใช้ Google Cloud Console แล้วสร้างโปรเจ็กต์ใหม่หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ำ หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี
- ชื่อโปรเจ็กต์คือชื่อที่แสดงสำหรับผู้เข้าร่วมโปรเจ็กต์นี้ ซึ่งเป็นสตริงอักขระที่ Google APIs ไม่ได้ใช้ โดยคุณจะอัปเดตได้ทุกเมื่อ
- รหัสโปรเจ็กต์ต้องไม่ซ้ำกันในโปรเจ็กต์ Google Cloud ทั้งหมดและจะเปลี่ยนแปลงไม่ได้ (เปลี่ยนแปลงไม่ได้หลังจากตั้งค่าแล้ว) คอนโซล Cloud จะสร้างสตริงที่ไม่ซ้ำกันโดยอัตโนมัติ ซึ่งปกติแล้วคุณไม่จำเป็นต้องสนใจว่าสตริงนั้นจะเป็นอะไร ในโค้ดแล็บส่วนใหญ่ คุณจะต้องอ้างอิงรหัสโปรเจ็กต์ (โดยปกติจะระบุเป็น
PROJECT_ID
) หากไม่ชอบรหัสที่สร้างขึ้น คุณก็สร้างรหัสอื่นแบบสุ่มได้ หรือจะลองใช้อุปกรณ์ของคุณเองเพื่อดูว่าฟีเจอร์นี้พร้อมใช้งานหรือไม่ก็ได้ คุณจะเปลี่ยนแปลงชื่อหลังจากขั้นตอนนี้ไม่ได้ และชื่อจะยังคงอยู่ตลอดระยะเวลาของโปรเจ็กต์ - โปรดทราบว่ามีค่าที่ 3 ซึ่งเป็นหมายเลขโปรเจ็กต์ที่ API บางรายการใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับค่าทั้ง 3 รายการนี้ได้ในเอกสารประกอบ
ข้อควรระวัง: รหัสโปรเจ็กต์จะซ้ำกันไม่ได้ทั่วโลก และบุคคลอื่นจะใช้รหัสที่คุณเลือกไม่ได้ คุณเป็นผู้ใช้รหัสดังกล่าวเพียงคนเดียว แม้ว่าโปรเจ็กต์จะถูกลบไปแล้ว แต่รหัสดังกล่าวก็ไม่สามารถนํามาใช้ซ้ำได้
- ถัดไป คุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากร/API ของ Cloud การทำตามโค้ดแล็บนี้จะไม่เสียค่าใช้จ่ายมากนัก หากต้องการปิดทรัพยากรเพื่อหลีกเลี่ยงการเรียกเก็บเงินหลังจากบทแนะนำนี้ คุณลบทรัพยากรที่สร้างไว้หรือลบโปรเจ็กต์ได้ ผู้ใช้ Google Cloud รายใหม่มีสิทธิ์เข้าร่วมโปรแกรมช่วงทดลองใช้ฟรีมูลค่า$300 USD
เริ่ม Cloud Shell
แม้ว่าคุณจะดำเนินการกับ Google Cloud จากระยะไกลจากแล็ปท็อปได้ แต่ในโค้ดแล็บนี้ คุณจะใช้ Google Cloud Shell ซึ่งเป็นสภาพแวดล้อมบรรทัดคำสั่งที่ทำงานในระบบคลาวด์
จากคอนโซล Google Cloud ให้คลิกไอคอน Cloud Shell ในแถบเครื่องมือด้านขวาบน
การจัดสรรและเชื่อมต่อกับสภาพแวดล้อมจะใช้เวลาเพียงไม่กี่นาที เมื่อดำเนินการเสร็จแล้ว คุณควรเห็นข้อมูลดังต่อไปนี้
เครื่องเสมือนนี้โหลดเครื่องมือการพัฒนาทั้งหมดที่คุณต้องการ ซึ่งจะมีไดเรกทอรีหลักขนาด 5 GB ถาวรและทำงานบน Google Cloud ซึ่งจะช่วยเพิ่มประสิทธิภาพเครือข่ายและการรับรองได้อย่างมีประสิทธิภาพ คุณทํางานทั้งหมดในโค้ดแล็บนี้ได้ภายในเบราว์เซอร์ คุณไม่จำเป็นต้องติดตั้งอะไรเลย
3. ก่อนเริ่มต้น
เปิดใช้ API
โปรดทราบว่าทรัพยากรบางอย่างที่คุณเปิดใช้จะมีค่าใช้จ่ายหากคุณไม่ได้ใช้ระดับโปรโมชัน ในกรณีปกติ หากทรัพยากรทั้งหมดถูกทำลายเมื่อทำ Lab เสร็จแล้ว ค่าใช้จ่ายของทรัพยากรทั้งหมดจะไม่เกิน $5 เราขอแนะนำให้ตรวจสอบการเรียกเก็บเงินและดูว่าคุณยอมรับการออกกำลังหรือไม่
ใน Cloud Shell ให้ตรวจสอบว่าได้ตั้งค่ารหัสโปรเจ็กต์แล้ว โดยทำดังนี้
โดยปกติแล้ว รหัสโปรเจ็กต์จะแสดงในวงเล็บในพรอมต์คำสั่งในเชลล์ระบบคลาวด์ดังที่แสดงในภาพ
gcloud config set project [YOUR-PROJECT-ID]
หน้าต่างจะปรากฏขึ้นเพื่อขอสิทธิ์เข้าถึง Cloud Shell โปรดคลิกให้สิทธิ์
จากนั้นตั้งค่าตัวแปรสภาพแวดล้อม PROJECT_ID เป็นรหัสโปรเจ็กต์ Google Cloud ดังนี้
PROJECT_ID=$(gcloud config get-value project)
ตั้งค่าตัวแปรสภาพแวดล้อม REGION และ ZONE เป็นภูมิภาคและโซนที่ต้องการ ดังนี้
REGION=europe-west4
ZONE=europe-west4-a
เปิดใช้บริการที่จำเป็นทั้งหมด
gcloud services enable compute.googleapis.com \
sqladmin.googleapis.com \
dns.googleapis.com \
iam.googleapis.com
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud services enable compute.googleapis.com \ sqladmin.googleapis.com \ dns.googleapis.com \ iam.googleapis.com Operation "operations/acat.p2-577410439131-dfb33f74-3447-485c-bae2-bc130126c965" finished successfully.
4. ติดตั้งใช้งานอินสแตนซ์ Cloud SQL สำหรับ Postgres
ใน Cloud Shell คุณสามารถใช้บรรทัดคำสั่งเพื่อสร้างอินสแตนซ์ Cloud SQL Postgres ใหม่โดยเปิดใช้ Private Service Connect ดังนี้
gcloud sql instances create cloudsql-postgres \
--project=$PROJECT_ID \
--region=$REGION \
--enable-private-service-connect \
--allowed-psc-projects=$PROJECT_ID \
--availability-type=ZONAL \
--no-assign-ip \
--cpu=2 \
--memory=7680MB \
--edition=ENTERPRISE \
--database-version=POSTGRES_16
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud sql instances create cloudsql-postgres \ --project=$PROJECT_ID \ --region=europe-west4 \ --enable-private-service-connect \ --allowed-psc-projects=$PROJECT_ID \ --availability-type=ZONAL \ --no-assign-ip \ --cpu=2 \ --memory=7680MB \ --edition=ENTERPRISE \ --database-version=POSTGRES_16 Creating Cloud SQL instance for POSTGRES_16...done. Created [https://sqladmin.googleapis.com/sql/v1beta4/projects/psc-cloud-sql-test02/instances/cloudsql-postgres1]. NAME: cloudsql-postgres DATABASE_VERSION: POSTGRES_16 LOCATION: europe-west4-b TIER: db-custom-2-7680 PRIMARY_ADDRESS: - PRIVATE_ADDRESS: - STATUS: RUNNABLE
เปลี่ยนรหัสผ่านสำหรับผู้ใช้ฐานข้อมูล postgres หลังจากติดตั้ง Cloud SQL สำหรับ Postgres เรียบร้อยแล้ว
gcloud sql users set-password postgres \
--instance=cloudsql-postgres \
--password='changeme'
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud sql users set-password postgres \ --instance=cloudsql-postgres \ --password='changeme' Updating Cloud SQL user...done.
5. ตั้งค่า Private Service Connect
สําหรับงานที่เกี่ยวข้องกับเครือข่ายต่อไปนี้ เราจะถือว่ามี VPC ที่ชื่อ default อยู่แล้ว
จองที่อยู่ IP ภายใน
ค้นหาช่วง CIDR ของเครือข่ายย่อย VPC ในภูมิภาค GCP ที่อ้างอิงในตัวแปรสภาพแวดล้อม REGION แล้วเลือกที่อยู่ IP ว่างในช่วง CIDR นี้สำหรับปลายทาง Privat Service Connect
gcloud compute networks subnets describe default \
--region=$REGION --project=$PROJECT_ID \
--format="value(ipCidrRange)"
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute networks subnets describe default \ --region=$REGION --project=$PROJECT_ID \ --format="value(ipCidrRange)" 10.164.0.0/20
จองที่อยู่ IP ภายในสำหรับปลายทาง Private Service Connect ในช่วงที่อยู่ CIDR ของซับเน็ต VPC ที่ดึงข้อมูลข้างต้น
gcloud compute addresses create cloudsql-psc \
--project=$PROJECT_ID \
--region=$REGION \
--subnet=default \
--addresses=10.164.0.10
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute addresses create cloudsql-psc \ --project=$PROJECT_ID \ --region=$REGION \ --subnet=default \ --addresses=10.164.0.10 Created [https://www.googleapis.com/compute/v1/projects/psc-cloud-sql-test02/regions/europe-west4/addresses/cloudsql-psc].
ยืนยันว่ามีการสำรองที่อยู่ IP ภายในและสถานะ "สงวนไว้" ปรากฏขึ้นสำหรับที่อยู่ IP
gcloud compute addresses list --project=$PROJECT_ID \
--filter="name=cloudsql-psc"
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute addresses list --project=$PROJECT_ID \ --filter="name=cloudsql-psc" NAME: cloudsql-psc ADDRESS/RANGE: 10.164.0.10 TYPE: INTERNAL PURPOSE: GCE_ENDPOINT NETWORK: REGION: europe-west4 SUBNET: default STATUS: RESERVED
รับ URI ของไฟล์แนบบริการ
หลังจากสร้างอินสแตนซ์ Cloud SQL ที่เปิดใช้ Private Service Connect แล้ว ให้รับ URI ของไฟล์แนบบริการและใช้เพื่อสร้างปลายทาง Private Service Connect ด้วยที่อยู่ IP ภายในที่สงวนไว้ด้านบน
gcloud sql instances describe cloudsql-postgres \
--project=$PROJECT_ID --format="value(pscServiceAttachmentLink)"
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud sql instances describe cloudsql-postgres --project=$PROJECT_ID \ --format="value(pscServiceAttachmentLink)" projects/l639336e2c716e3d8p-tp/regions/europe-west4/serviceAttachments/a-33446dfaf850-psc-service-attachment-e6471fc6708a6cfe
สร้าง Private Service Connect
สร้างปลายทาง Private Service Connect และชี้ปลายทางไปยัง URI ไฟล์แนบบริการ Cloud SQL โดยทำดังนี้
gcloud compute forwarding-rules create cloudsql-psc-ep \
--address=cloudsql-psc \
--project=$PROJECT_ID \
--region=$REGION \
--network=default \
--target-service-attachment=projects/l639336e2c716e3d8p-tp/regions/europe-west4/serviceAttachments/a-33446dfaf850-psc-service-attachment-e6471fc6708a6cfe \
--allow-psc-global-access
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute forwarding-rules create cloudsql-psc-ep \ --address=cloudsql-psc \ --project=$PROJECT_ID \ --region=$REGION \ --network=default \ --target-service-attachment=projects/l639336e2c716e3d8p-tp/regions/europe-west4/serviceAttachments/a-33446dfaf850-psc-service-attachment-e6471fc6708a6cfe \ --allow-psc-global-access Created [https://www.googleapis.com/compute/v1/projects/psc-cloud-sql-test02/regions/europe-west4/forwardingRules/cloudsql-psc-ep].
ตรวจสอบว่าอุปกรณ์ปลายทางเชื่อมต่อกับไฟล์แนบของบริการได้ โดยทำดังนี้
gcloud compute forwarding-rules describe cloudsql-psc-ep \
--project=$PROJECT_ID \
--region=$REGION \
--format="value(pscConnectionStatus)"
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute forwarding-rules describe cloudsql-psc-ep \ --project=$PROJECT_ID \ --region=$REGION \ --format="value(pscConnectionStatus)" ACCEPTED
กำหนดค่าโซน DNS ที่มีการจัดการ
หากต้องการเพิ่มชื่อ DNS ที่แนะนำสำหรับอินสแตนซ์ Cloud SQL วิธีที่ดีที่สุดคือสร้างโซน DNS ส่วนตัวในเครือข่าย VPC ที่เกี่ยวข้อง โดยทำดังนี้
gcloud dns managed-zones create cloudsql-dns \
--project=$PROJECT_ID \
--description="DNS zone for the Cloud SQL instances" \
--dns-name=$REGION.sql.goog. \
--networks=default \
--visibility=private
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud dns managed-zones create cloudsql-dns \ --project=$PROJECT_ID \ --description="DNS zone for the Cloud SQL instances" \ --dns-name=$REGION.sql.goog. \ --networks=default \ --visibility=private Created [https://dns.googleapis.com/dns/v1/projects/psc-cloud-sql-test02/managedZones/cloudsql-dns].
เพิ่มระเบียน DNS สำหรับ Private Service Connect
รับระเบียน DNS ที่แนะนำสําหรับอินสแตนซ์ Cloud SQL โดยทำดังนี้
gcloud sql instances describe cloudsql-postgres \
--project=$PROJECT_ID \
--format="value(dnsName)"
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud sql instances describe cloudsql-postgres --project=$PROJECT_ID --format="value(dnsName)" 33446dfaf850.1rrhex0himzzc.europe-west4.sql.goog.
เพิ่มระเบียน DNS ที่แนะนําลงในโซน DNS ที่มีการจัดการ
gcloud dns record-sets create 3446dfaf850.1rrhex0himzzc.europe-west4.sql.goog. \
--project=$PROJECT_ID \
--type=A \
--rrdatas=10.164.0.10 \
--zone=cloudsql-dns
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud dns record-sets create 3446dfaf850.1rrhex0himzzc.europe-west4.sql.goog. \ --project=$PROJECT_ID \ --type=A \ --rrdatas=10.164.0.10 \ --zone=cloudsql-dns NAME: 3446dfaf850.1rrhex0himzzc.europe-west4.sql.goog. TYPE: A TTL: 0 DATA: 10.164.0.10
6. เตรียมเครื่องเสมือน Google Compute Engine
ทำให้เครื่องเสมือน Google Compute Engine ใช้งานได้
ระบบจะใช้เครื่องเสมือน (VM) เพื่อเชื่อมต่อกับอินสแตนซ์ Cloud SQL
gcloud compute instances create cloudsql-client \
--zone=$ZONE \
--create-disk=auto-delete=yes,boot=yes,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-12 AND family!=debian-12-arm64" --format="value(name)") \
--scopes=https://www.googleapis.com/auth/cloud-platform \
--network-interface=no-address
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute instances create cloudsql-client \ --zone=$ZONE \ --create-disk=auto-delete=yes,boot=yes,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-12 AND family!=debian-12-arm64" --format="value(name)") \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --network-interface=no-address Created [https://www.googleapis.com/compute/v1/projects/psc-cloud-sql-test02/zones/europe-west4-a/instances/cloudsql-client]. NAME: cloudsql-client ZONE: europe-west4-a MACHINE_TYPE: n1-standard-1 PREEMPTIBLE: INTERNAL_IP: 10.164.0.2 EXTERNAL_IP: STATUS: RUNNING
ติดตั้งไคลเอ็นต์ Postgres
สร้าง Cloud NAT สําหรับการรับส่งข้อมูลขาออกไปยังอินเทอร์เน็ตที่ VM สามารถเข้าถึงที่เก็บ Linux ได้ ดังนี้
gcloud compute routers create cloud-nat-router \
--network=default \
--region=$REGION
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute routers create cloud-nat-router \ --network=default \ --region=$REGION Creating router [cloud-nat-router]...done. NAME: cloud-nat-router REGION: europe-west4 NETWORK: default
gcloud compute routers nats create cloud-nat \
--router=cloud-nat-router \
--region=$REGION \
--nat-all-subnet-ip-ranges \
--auto-allocate-nat-external-ips
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute routers nats create cloud-nat \ --router=cloud-nat-router \ --region=$REGION \ --nat-all-subnet-ip-ranges \ --auto-allocate-nat-external-ips Creating NAT [cloud-nat] in router [cloud-nat-router]...done.
ติดตั้งซอฟต์แวร์ไคลเอ็นต์ PostgreSQL ใน VM ที่ติดตั้งใช้งาน
เชื่อมต่อกับ VM
gcloud compute ssh --zone $ZONE "cloudsql-client" \
--tunnel-through-iap --project $PROJECT_ID
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute ssh --zone "europe-west4-a" "cloudsql-client" --tunnel-through-iap --project $PROJECT_ID WARNING: To increase the performance of the tunnel, consider installing NumPy. For instructions, please see https://cloud.google.com/iap/docs/using-tcp-forwarding#increasing_the_tcp_upload_bandwidth Warning: Permanently added 'compute.1355719684363734964' (ED25519) to the list of known hosts. Linux cloudsql-client 6.1.0-26-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.112-1 (2024-09-30) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Creating directory '/home/student_org_altostrat_com'.
ติดตั้งคำสั่งเรียกใช้ซอฟต์แวร์ภายใน VM โดยทำดังนี้
sudo apt-get update
sudo apt-get install --yes postgresql-client
ผลลัพธ์ที่คาดหวัง
student@cloudsql-client:~$ sudo apt-get update sudo apt-get install --yes postgresql-client Get:1 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B] Get:5 file:/etc/apt/mirrors/debian-security.list Mirrorlist [39 B] Get:7 https://packages.cloud.google.com/apt google-compute-engine-bookworm-stable InRelease [1321 B] Get:2 https://deb.debian.org/debian bookworm InRelease [151 kB] Get:3 https://deb.debian.org/debian bookworm-updates InRelease [55.4 kB] Get:4 https://deb.debian.org/debian bookworm-backports InRelease [59.0 kB] ...redacted... update-alternatives: using /usr/share/postgresql/15/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode Setting up postgresql-client (15+248) ... Processing triggers for man-db (2.11.2-2) ... Processing triggers for libc-bin (2.36-9+deb12u8) ...
7. เชื่อมต่อกับอินสแตนซ์ Cloud SQL สำหรับ Postgres ผ่าน Private Service Connect
เชื่อมต่อกับอินสแตนซ์
psql "sslmode=disable dbname=postgres user=postgres host=3446dfaf850.1rrhex0himzzc.europe-west4.sql.goog."
ผลลัพธ์ที่คาดหวัง
student@cloudsql-client:~$ psql "sslmode=disable dbname=postgres user=postgres host=3446dfaf850.1rrhex0himzzc.europe-west4.sql.goog." Password for user postgres: psql (15.8 (Debian 15.8-0+deb12u1), server 16.4) WARNING: psql major version 15, server major version 16. Some psql features might not work. Type "help" for help. postgres=>
สร้างและทดสอบฐานข้อมูล
สร้างฐานข้อมูล
CREATE DATABASE company;
ผลลัพธ์ที่คาดหวัง
postgres=> CREATE DATABASE company; CREATE DATABASE postgres=>
แสดงรายการฐานข้อมูลทั้งหมด
\l
ผลลัพธ์ที่คาดหวัง
postgres=> \l List of databases Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges ---------------+-------------------+----------+------------+------------+------------+-----------------+----------------------------------------- cloudsqladmin | cloudsqladmin | UTF8 | en_US.UTF8 | en_US.UTF8 | | libc | company | postgres | UTF8 | en_US.UTF8 | en_US.UTF8 | | libc | postgres | cloudsqlsuperuser | UTF8 | en_US.UTF8 | en_US.UTF8 | | libc | template0 | cloudsqladmin | UTF8 | en_US.UTF8 | en_US.UTF8 | | libc | =c/cloudsqladmin + | | | | | | | cloudsqladmin=CTc/cloudsqladmin template1 | cloudsqlsuperuser | UTF8 | en_US.UTF8 | en_US.UTF8 | | libc | =c/cloudsqlsuperuser + | | | | | | | cloudsqlsuperuser=CTc/cloudsqlsuperuser (5 rows) postgres=>
เชื่อมต่อกับฐานข้อมูลพนักงาน
\c company
ผลลัพธ์ที่คาดหวัง
postgres=> \c company psql (15.8 (Debian 15.8-0+deb12u1), server 16.4) WARNING: psql major version 15, server major version 16. Some psql features might not work. You are now connected to database "company" as user "postgres". company=>
สร้างตารางในฐานข้อมูลของบริษัท
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
first VARCHAR(255) NOT NULL,
last VARCHAR(255) NOT NULL,
salary DECIMAL (10, 2)
);
ผลลัพธ์ที่คาดหวัง
company=> CREATE TABLE employees ( id SERIAL PRIMARY KEY, first VARCHAR(255) NOT NULL, last VARCHAR(255) NOT NULL, salary DECIMAL (10, 2) ); CREATE TABLE
แทรกข้อมูลลงในตาราง employees ของฐานข้อมูลบริษัท
INSERT INTO employees (first, last, salary) VALUES
('Max', 'Mustermann', 5000.00),
('Anna', 'Schmidt', 7000.00),
('Peter', 'Mayer', 6000.00);
ผลลัพธ์ที่คาดหวัง
company=> INSERT INTO employees (first, last, salary) VALUES ('Max', 'Mustermann', 5000.00), ('Anna', 'Schmidt', 7000.00), ('Peter', 'Mayer', 6000.00); INSERT 0 3 company=>
ค้นหาตารางพนักงาน
SELECT * FROM employees;
ผลลัพธ์ที่คาดหวัง
company=> SELECT * FROM employees; id | first | last | salary ----+-------+------------+--------- 1 | Max | Mustermann | 5000.00 2 | Anna | Schmidt | 7000.00 3 | Peter | Mayer | 6000.00 (3 rows) company=>
ออกจากฐานข้อมูล Postgres และ VM แล้วกลับไปที่ Cloud Shell โดยทำดังนี้
\q
exit
ผลลัพธ์ที่คาดหวัง
postgres=> \q student@cloudsql-client:~$ exit logout Connection to compute.1355719684363734964 closed. student@cloudshell:~ (psc-cloud-sql-test02)$
8. ล้างสภาพแวดล้อม
ทำลายทรัพยากร Google Cloud ทั้งหมดในโปรเจ็กต์เมื่อคุณใช้งานห้องทดลองเสร็จแล้ว
ลบอินสแตนซ์ Cloud SQL Postgres
ใน Cloud Shell ให้ลบอินสแตนซ์ Cloud SQL สำหรับ PostgreSQL โดยทำดังนี้
gcloud sql instances delete cloudsql-postgres --quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud sql instances delete cloudsql-postgres --quiet Deleting Cloud SQL instance...done. Deleted [https://sqladmin.googleapis.com/sql/v1beta4/projects/psc-cloud-sql-test02/instances/cloudsql-postgres].
ลบเครื่องเสมือน Google Compute Engine
ลบ VM ใน Cloud Shell โดยทำดังนี้
gcloud compute instances delete cloudsql-client \
--zone=$ZONE \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute instances delete cloudsql-client \ --quiet$ZONE \ --quiet Deleted [https://www.googleapis.com/compute/v1/projects/psc-cloud-sql-test02/zones/europe-west4-a/instances/cloudsql-client].
ลบคอมโพเนนต์เครือข่าย
ลบคอมโพเนนต์ที่เกี่ยวข้องกับเครือข่าย ได้แก่ Cloud NAT, Cloud Router, ปลายทาง Private Service Connect, ที่อยู่ IP ภายในที่สงวนไว้, ระเบียน DNS และโซน DNS ที่มีการจัดการ
ลบ Cloud NAT
gcloud compute routers nats delete cloud-nat \
--router=cloud-nat-router \
--region=$REGION \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute routers nats delete cloud-nat \ --router=cloud-nat-router \ --region=$REGION \ --quiet Updated [https://www.googleapis.com/compute/v1/projects/psc-cloud-sql-test02/regions/europe-west4/routers/cloud-nat-router].
ลบ Cloud Router
gcloud compute routers delete cloud-nat-router \
--region=$REGION \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute routers delete cloud-nat-router \ --region=$REGION \ --quiet Deleted [https://www.googleapis.com/compute/v1/projects/psc-cloud-sql-test02/regions/europe-west4/routers/cloud-nat-router].
ลบปลายทาง Private Service Connect
gcloud compute forwarding-rules delete cloudsql-psc-ep \
--project=$PROJECT_ID \
--region=$REGION \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute forwarding-rules delete cloudsql-psc-ep \ --project=$PROJECT_ID \ --region=$REGION \ --quiet Deleted [https://www.googleapis.com/compute/v1/projects/psc-cloud-sql-test02/regions/europe-west4/forwardingRules/cloudsql-psc-ep].
ปล่อยที่อยู่ IP ภายในที่สงวนไว้
gcloud compute addresses delete cloudsql-psc \
--project=$PROJECT_ID \
--region=$REGION \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute addresses delete cloudsql-psc \ --project=$PROJECT_ID \ --region=$REGION \ --quiet Deleted [https://www.googleapis.com/compute/v1/projects/psc-cloud-sql-test02/regions/europe-west4/addresses/cloudsql-psc].
ลบระเบียน DNS
gcloud dns record-sets delete 3446dfaf850.1rrhex0himzzc.europe-west4.sql.goog. \
--project=$PROJECT_ID \
--type=A \
--zone=cloudsql-dns
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud dns record-sets delete 3446dfaf850.1rrhex0himzzc.europe-west4.sql.goog. \ --project=$PROJECT_ID \ --type=A \ --zone=cloudsql-dns Deleted [https://dns.googleapis.com/dns/v1/projects/psc-cloud-sql-test02/managedZones/cloudsql-dns/rrsets/3446dfaf850.1rrhex0himzzc.europe-west4.sql.goog./A].
ลบโซน DNS ที่มีการจัดการ
gcloud dns managed-zones delete cloudsql-dns \
--project=$PROJECT_ID \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud dns managed-zones delete cloudsql-dns \ --project=$PROJECT_ID \ --quiet Deleted [https://dns.googleapis.com/dns/v1/projects/psc-cloud-sql-test02/managedZones/cloudsql-dns].
9. ขอแสดงความยินดี
ขอแสดงความยินดีที่ทํา Codelab จนเสร็จสมบูรณ์
สิ่งที่เราได้พูดถึง
- วิธีทำให้อินสแตนซ์ Cloud SQL พร้อมใช้งาน
- วิธีทำให้ Private Service Connect ใช้งานได้
- วิธีเชื่อมต่อจาก VM กับอินสแตนซ์ Cloud SQL ผ่าน Private Service Connect