1) Cloud Spanner with Console
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-327.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-328.png)
Cloud Spanner is a managed, mission-critical, globally consistent and scalable relational database service.
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-329.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-330.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-331.png)
2) Cloud Spanner with Terraform
Giờ mình cung cấp code terraform
file main.tf
resource "google_spanner_instance" "spanner_tf" { name = "spannertf" config = "regional-asia-south1" display_name = "Spanner from TF" num_nodes = 1 labels = { "env" = "learningtf" } } resource "google_spanner_database" "db1"{ name = "db1" instance = google_spanner_instance.spanner_tf.name }
giờ bạn terraform init rổi đến apply
Và đương nhiên là chúng ta sẽ gặp lỗi:
Error creating Instance: googleapi: Error 403: Caller is missing IAM permission spanner.instances.create on resource projects/xxxxx
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-332.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-333.png)
giờ thực hiện apply thôi:
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-334.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-335.png)
Chỗ này nếu bạn chạy terraform destroy sẽ bị fail vì mình chưa set deletion_protection=false
Và sẽ bị lỗi bên dưới.
các từ tìm hiểu thêm vào nhé!
Error: cannot destroy instance without setting deletion_protection=false
and running `terraform apply`
Còn hông lên UI của GCP xoá thôi.
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-336.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-337.png)
3) Cloud SQL – Console
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-338.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-339.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-340.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-341.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-342.png)
Giờ chúng ta chọn RAM, CPU, storage, …
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-343.png)
mỗi style sẽ có định sẵn 1 lượng ram, cpu,… khác nhau
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-344.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-345.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-346.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-347.png)
Thôi chúng ta nhấn nut create thôi
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-348.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-349.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-350.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-351.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-352.png)
gcloud sql connect mysql-nimtechnology-console --user=root --quiet
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-353.png)
ERROR: (gcloud.sql.connect) PERMISSION_DENIED: Cloud SQL Admin API has not been used in project 90059718136 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/sqladmin.googleapis.com/overview?project=90059718136 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
Nếu bạn bị lỗi như ảnh thì click vào đường link trong thông báo.
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-354.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-355.png)
Hiện thị như dưới là ngon.
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-356.png)
GIờ gõ lại command nhé.
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-357.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-358.png)
4) Cloud SQL with Terraform
resource "google_sql_database_instance" "mysql-from-tf" { name = "mysql-from-tf" deletion_protection = false region = "us-central1" settings { tier = "db-f1-micro" } } resource "google_sql_user" "myuser" { name = "ankit" password = "ankit@123" instance = google_sql_database_instance.mysql-from-tf.name }
Tìm hiểu về tier
:
(Required) The machine type to use. See tiers for more details and supported versions. Postgres supports only shared-core machine types, and custom machine types such as db-custom-2-13312
. See the Custom Machine Type Documentation to learn about specifying custom machine types.
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-359.png)
https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/tiers/list
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-360.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-361.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-362.png)
https://codebeautify.org/jsonviewer
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-363.png)
Ta chỉ cần chọn tier như thế nào là phù hợp
ví dụ mình chọn tier: db-f1-micro
chúng ta cũng đã hiểu tier là gì rồi thì terraform apply thôi
CHúng ta bị lỗi:
Error: Error, failed to create instance mysql-from-tf: googleapi: Error 403: The client is not authorized to make this request., notAuthorized
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-364.png)
Giờ apply tiếp.
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-366.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-365.png)
Còn rất nhiều config chắc các bạn phải tự đọc