resource "google_sql_database_instance" "original-primary" {
name = "mysql-original-primary-instance"
region = "us-east1"
# Specify a database version that supports Cloud SQL Enterprise Plus edition.
database_version = "MYSQL_8_0"
instance_type = "CLOUD_SQL_INSTANCE"
settings {
# Specify a tier that supports Cloud SQL Enterprise Plus edition.
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
backup_configuration {
# You must enable automated backups and binary logging.
enabled = true
binary_log_enabled = true
}
}
# Set `deletion_protection` to true to ensure that one can't accidentally
# delete this instance by use of Terraform whereas
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
deletion_protection = false
}
resource "google_sql_database_instance" "dr-replica" {
name = "mysql-dr-replica-instance"
# DR replica must be in a different region than the region of the primary instance.
region = "us-west2"
# DR replica must be the same database version as the primary instance.
database_version = "MYSQL_8_0"
instance_type = "READ_REPLICA_INSTANCE"
# Specify the primary instance as the master instance.
master_instance_name = google_sql_database_instance.original-primary.name
settings {
# DR replica must be in the same tier as your primary instance and support Enterprise Plus edition.
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
}
# Set `deletion_protection` to true to ensure that one can't accidentally
# delete this instance by use of Terraform whereas
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
deletion_protection = false
}
REST v1
使用任何要求資料之前,請先替換以下項目:
PRIMARY_INSTANCE_NAME:主要執行個體的名稱。
PROJECT_ID:主執行單元和 DR 副本的 Google Cloud 專案 ID 或專案編號。
DATABASE_VERSION: 版本字串,必須與主要執行個體的資料庫主要版本和次要版本相符,例如 MYSQL_8_0_31。主要資料庫和 DR 副本的資料庫版本必須相同。
data "google_project" "default" {
}
resource "google_sql_database_instance" "original-primary" {
name = "mysql-original-primary-instance"
region = "us-east1"
# Specify a database version that supports Cloud SQL Enterprise Plus edition.
database_version = "MYSQL_8_0"
instance_type = "CLOUD_SQL_INSTANCE"
replication_cluster {
# Designate the DR replica.
# The format for setting the DR replica is `project-id:dr-replica-name`.
failover_dr_replica_name = "${data.google_project.default.project_id}:mysql-dr-replica-instance"
}
settings {
# Specify a tier that supports Cloud SQL Enterprise Plus edition.
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
backup_configuration {
# You must enable automated backups and binary logging.
enabled = true
binary_log_enabled = true
}
}
# Set `deletion_protection` to true to ensure that one can't accidentally
# delete this instance by use of Terraform whereas
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
deletion_protection = false
# Optional. Add more settings.
}
resource "google_sql_database_instance" "dr-replica" {
name = "mysql-dr-replica-instance"
# DR replica must be in a different region than the region of the primary instance.
region = "us-west2"
# DR replica must be the same database version as the primary instance.
database_version = "MYSQL_8_0"
instance_type = "READ_REPLICA_INSTANCE"
# Specify the primary instance as the master instance.
master_instance_name = google_sql_database_instance.original-primary.name
settings {
# DR replica must be in the same tier as your primary instance and support Enterprise Plus edition.
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
}
# Set `deletion_protection` to true to ensure that one can't accidentally
# delete this instance by use of Terraform whereas
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
deletion_protection = false
# Optional. Add more settings.
}
如要開始切換作業,請使用 Terraform 資源。如要將 DR 備援機制設為新的主執行個體,請執行第一個範例。
#
# This sample provides the first part of the switchover operation and turns the DR replica
# into the new primary instance.
data "google_project" "default" {
}
resource "google_sql_database_instance" "original-primary" {
name = "mysql-original-primary-instance"
region = "us-east1"
database_version = "MYSQL_8_0"
instance_type = "CLOUD_SQL_INSTANCE"
replication_cluster {
failover_dr_replica_name = "${data.google_project.default.project_id}:mysql-dr-replica-instance"
}
settings {
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
backup_configuration {
enabled = true
binary_log_enabled = true
}
}
# Set `deletion_protection` to true to ensure that one can't accidentally
# delete this instance by use of Terraform whereas
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
deletion_protection = false
# Optional. Add more settings.
}
resource "google_sql_database_instance" "dr-replica" {
name = "mysql-dr-replica-instance"
region = "us-west2"
database_version = "MYSQL_8_0"
# Change the instance type from "READ_REPLICA_INSTANCE" to "CLOUD_SQL_INSTANCE".
instance_type = "CLOUD_SQL_INSTANCE"
# Remove or comment out the master_instance_name from the DR replica.
# master_instance_name = google_sql_database_instance.original-primary.name
# Add the original primary instance to a list of replicas for the new primary.
replica_names = [google_sql_database_instance.original-primary.name]
# Designate the original primary instance as the DR replica of the new primary instance.
# The format for setting the DR replica is `project-id:dr-replica-name`.
replication_cluster {
failover_dr_replica_name = "${data.google_project.default.project_id}:${google_sql_database_instance.original-primary.name}"
}
settings {
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
backup_configuration {
# Add a backup configuration section to enable automated backups and binary logging for the new primary instance.
enabled = true
binary_log_enabled = true
}
}
# Set `deletion_protection` to true to ensure that one can't accidentally
# delete this instance by use of Terraform whereas
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
deletion_protection = false
# Optional. Add more settings.
}
如要將原始主要執行個體設為新主要執行個體的複本,請使用第二個範例。
#
# This sample provides the second part of the switchover operation and makes the original primary instance
# a replica of the new primary instance. After you run `terraform apply` for this sample, you'll see
# the following message:
#
# "No changes. Your infrastructure matches the configuration.
#
# Terraform has compared your real infrastructure against your configuration and found no differences,
# so no changes are needed.
#
# Apply complete! Resources: 0 added, 0 changed, 0 destroyed."
data "google_project" "default" {
}
resource "google_sql_database_instance" "original-primary" {
name = "mysql-original-primary-instance"
region = "us-east1"
database_version = "MYSQL_8_0"
# Change instance type for the original primary from "CLOUD_SQL_INSTANCE" to "READ_REPLICA_INSTANCE".
instance_type = "READ_REPLICA_INSTANCE"
# Set master_instance_name to the the new primary instance, the old DR replica.
master_instance_name = "mysql-dr-replica-instance"
# replica_names = [] # If you previously defined a replica_names field in your template, then delete the DR replica
# (new primary) from the list of replicas. Don't delete the entire replica_names field.
# Instead set the field to an empty string. For example, replica_names = [""].
replication_cluster {
# This instance no longer requires a designated DR replica since it's a replica.
# Remove the DR replica designation by setting the field to an empty string.
failover_dr_replica_name = ""
}
settings {
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
backup_configuration {
# Disable automated backups because this instance is now a replica.
enabled = false
binary_log_enabled = true
}
}
deletion_protection = false
}
resource "google_sql_database_instance" "dr-replica" {
name = "mysql-dr-replica-instance"
region = "us-west2"
database_version = "MYSQL_8_0"
instance_type = "CLOUD_SQL_INSTANCE"
replica_names = [google_sql_database_instance.original-primary.name]
replication_cluster {
failover_dr_replica_name = "${data.google_project.default.project_id}:${google_sql_database_instance.original-primary.name}"
}
settings {
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
backup_configuration {
enabled = true
binary_log_enabled = true
}
}
deletion_protection = false
}
POST https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/REPLICA_NAME/promoteReplica?failover=ENABLE_REPLICA_FAILOVER
POST https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/REPLICA_NAME/promoteReplica?failover=ENABLE_REPLICA_FAILOVER
"Instance was converted into a replica between the target PITR
time and the last available base backup. PITR logs are not available
for the period instance was a replica. Please clone from the instance
that was primary at time %s"
"You can only designate a disaster recovery (DR) replica for primary instances that are
storing their PITR logs in Cloud Storage. PITR logs of the instance %s are not stored in Cloud Storage"
您的主要執行個體尚未將交易記錄檔的儲存位置切換至 Cloud Storage。您可以切換交易記錄的儲存位置後再試一次,也可以嘗試為其他主要執行個體指定 DR 副本。