[Qwiklabs] GSP355 - Manage PostgreSQL Databases on Cloud SQL: Challenge Lab
[Qwiklabs] GSP355 - Manage PostgreSQL Databases on Cloud SQL: Challenge Lab
The boss stage of “Manage PostgreSQL Databases on Cloud SQL” quest
Task 1. Migrate a stand-alone PostgreSQL database to a Cloud SQL for PostgreSQL instance
考 GSP918 的內容,但有些沒涵蓋到
Prepare the stand-alone PostgreSQL database for migration
- Enable the Google Cloud APIs required for Database Migration Services
Database Migration
ENABLE
- Upgrade pglogical database extension on vm
Compute Engine > VM instance
postgresql-vm -> SSH
cmd time!
sudo apt install postgresql-13-pglogical |
sudo su - postgres -c "gsutil cp gs://cloud-training/gsp918/pg_hba_append.conf ." |
sudo su - postgres |
\c postgres; |
- Create user
!!記得把底下所有 {user}
改成 quest 指定的 Migration user name!!
CREATE USER {user} PASSWORD 'DMS_1s_cool!'; |
- Grant the user some privileges
!!記得把底下所有 {user}
改成 quest 指定的 Migration user name!!
\c postgres; |
\c orders; |
- Add primary key
我這邊只觀察到 inventiory_items
沒有 primary key,所以只打一行
ALTER TABLE inventory_items ADD PRIMARY KEY (id); |
Migrate the stand-alone PostgreSQL database to a Cloud SQL for PostgreSQL instance
接下來的部分,不知道為什麼 Loading 都很久,但是切到其他 tab 再回來卻顯示完成了,懷疑他們程式有 bug。
總之如果轉圈圈轉太久,試著切到其他 tab 再回來
- Create new DMS
Database Migration > Connection profile (如果一直被導向啟用 API,試著重新登入)
CREATE PROFILE
- Create new continuous DMS job
S1
S2
S3
Follow the quest instruction!!! (特別注意 Destination Instance ID 與 Database Version)
S4 (Need to wait S3 to be completed)
S5
omitted
- Test & Start the job
START
稍等一下等到 Status 變成 Running CDC in progress 就可以回去按 check progress 了
Task 2. Promote a Cloud SQL to be a stand-alone instance for reading and writing data
進 Database Migration 給他按 Promote ,稍等一下就完成了,EZ
Task 3. Implement Cloud SQL for PostgreSQL IAM database authentication
GSP920 Task 1, 3
- Allow public ip of postgres-vm to sql instance
SQL > (instance) > Connections
- Create CloudIAM user
SQL > (instance) > Users
Add user account
選擇 Cloud IAM,Principle 填自己的 account name (Username)
- Grant
SELECT
permission to the Cloud IAM user
SQL > (instance) > Overview,滑下去找 OPEN CLOUD SELL 以連線
密碼是 supersecret!
\c orders |
密碼一樣是 supersecret!
底下{Database Table} 替換成 Database Table to be secured with IAM Authentication、 {IAM UserName}
換成自己的 account name (Username)!!最後面引號("
)跟分號(;
)都要記得!!
GRANT SELECT ON TABLE {Database Table} TO "{IAM Username}"; |
- Test Run as IAM user
(You can omit this part, go check the progress directly 😄)
開啟新的 Cloud Shell
底下 、{IAM UserName}
換成自己的 account name!!
gcloud sql connect {} --user={IAM Username} |
Task 4. Configure and test point-in-time recovery
- Enable backups
In cloud shell,
底下 {SQL Instance Name}
替換成 SQL instance name
export CLOUD_SQL_INSTANCE={SQL Instance Name} |
gcloud sql instances describe $CLOUD_SQL_INSTANCE |
- Enable point-in-time recovery
底下的 --retained-transaction-log-days=X
,X
要替換為指定的 Point-in-time recovery retention days
gcloud sql instances patch $CLOUD_SQL_INSTANCE \ |
等他跑跑跑
- Make a note of the timestamp for the point-in-time you wish to restore to.
紀錄一下現在時間
date --rfc-3339=seconds |
- Make some changes to the database (Add a row of data to the
orders.distribution_centers
table)
用跟之前同樣的方式連進資料庫 (SQL > Overview: OPEN CLOUD SHELL, psw=supersecret!
)
\c orders; |
- Use point-in-time recovery to create a clone that replicates the instance state at your chosen timestamp.
底下 {TIMESTAMP_IN_STEP_3}
替換為剛剛 Step 3 印出來的時間,同樣記得引號('
)
export NEW_INSTANCE_NAME=postgres-orders-pitr |
等好久好久,跑完就可以去評分了