{OCI データベース} Base Database Service(Data Guard)

 

https://oracle-japan.github.io/ocitutorials/basedb/dbcs106-dataguard/#2-data-guard%E3%82%92%E6%A7%8B%E6%88%90%E3%81%99%E3%82%8B%E7%82%BA%E3%81%AE%E5%89%8D%E6%8F%90%E6%9D%A1%E4%BB%B6

https://docs.oracle.com/en-us/iaas/tools/oci-cli/3.46.0/oci_cli_docs/cmdref/db/data-guard-association.html

https://www.oracle.com/jp/database/base-database-service/pricing/
https://speakerdeck.com/oracle4engineer/basedb-tech-detail
https://docs.public.oneportal.content.oci.oraclecloud.com/ja-jp/iaas/dbcs/doc/virtual-machine-db-systems.html

 

ENTERPRISE_EDITION
19.21
VM.Standard.A1.Flex
LVM
1cpu


-- 1. terraformによるVPC構築


cat <<-'EOF' > variables.tf

locals {
  tenancy_ocid = "ocid1.tenancy.oc1..111111111111111111111111111111111111111111111111111111111111"

}

variable "compartment_name" {
  description = "compartment_name"
  type = string
  default = "cmp01"
}

EOF

 

cat <<-'EOF' > main.tf

terraform {
  required_version = ">= 1.0.0, < 2.0.0"
  required_providers {
    oci = {
       source  = "hashicorp/oci"
       version = "= 5.23.0"
    }
  }
}

provider "oci" {
  tenancy_ocid = local.tenancy_ocid
  user_ocid = "ocid1.user.oc1..111111111111111111111111111111111111111111111111111111111111" 
  private_key_path = "~/.oci/oci_api_key.pem"
  fingerprint = "45:ed:22:e6:cc:fd:63:97:12:9d:62:7a:90:12:65:7a"
  region = "us-ashburn-1"
}


resource "oci_identity_compartment" "cmp01" {
    # Required
    compartment_id = local.tenancy_ocid
    description = var.compartment_name
    name = var.compartment_name
    
    enable_delete = true
}

resource "oci_core_vcn" "vcn01" {
    #Required
    compartment_id = oci_identity_compartment.cmp01.id

    #Optional
    cidr_block = "10.0.0.0/16"
    display_name = "vcn01"
    dns_label = "vcn01"

}


resource "oci_core_internet_gateway" "igw01" {
    #Required
    compartment_id = oci_identity_compartment.cmp01.id
    vcn_id = oci_core_vcn.vcn01.id

    #Optional
    enabled = true
    display_name = "igw01"
}

resource "oci_core_route_table" "rt01" {
    #Required
    compartment_id = oci_identity_compartment.cmp01.id
    vcn_id = oci_core_vcn.vcn01.id

    #Optional
    display_name = "rt01"
    route_rules {
        #Required
        network_entity_id = oci_core_internet_gateway.igw01.id
        #Optional
        destination = "0.0.0.0/0"
    }
    
}


resource "oci_core_security_list" "sl01" {
    #Required
    compartment_id = oci_identity_compartment.cmp01.id
    vcn_id = oci_core_vcn.vcn01.id

    #Optional
    display_name = "sl01"
    
    egress_security_rules {
        destination = "0.0.0.0/0"
        protocol = "all"
        stateless = false
    }
    
    ingress_security_rules {
        protocol = "6"
        source = "0.0.0.0/0"
        stateless = false
        tcp_options {
            max = 22
            min = 22
        }
    }
    ingress_security_rules {
        protocol = "6"
        source = "0.0.0.0/0"
        stateless = false
        tcp_options {
            max = 1521
            min = 1521
        }
    }
}

 

resource "oci_core_subnet" "subnet01" {
    #Required
    cidr_block = "10.0.1.0/24"
    compartment_id = oci_identity_compartment.cmp01.id
    vcn_id = oci_core_vcn.vcn01.id

    #Optional

    display_name = "subnet01"
    dns_label = "subnet01"
    route_table_id = oci_core_route_table.rt01.id
    security_list_ids = [oci_core_security_list.sl01.id]
}

resource "oci_core_subnet" "subnet02" {
    #Required
    cidr_block = "10.0.2.0/24"
    compartment_id = oci_identity_compartment.cmp01.id
    vcn_id = oci_core_vcn.vcn01.id

    #Optional

    display_name = "subnet02"
    dns_label = "subnet02"
    route_table_id = oci_core_route_table.rt01.id
    security_list_ids = [oci_core_security_list.sl01.id]
}


resource "oci_core_nat_gateway" "ngw01" {
    #Required
    compartment_id = oci_identity_compartment.cmp01.id
    vcn_id = oci_core_vcn.vcn01.id

    #Optional
    block_traffic = false
    display_name = "ngw01"
}


data "oci_core_services" "svc01" {
  filter {
    name   = "name"
    values = ["All .* Services In Oracle Services Network"]
    regex  = true
  }
}


resource "oci_core_service_gateway" "sgw01" {
    #Required
    compartment_id = oci_identity_compartment.cmp01.id
    services {
        #Required
        service_id = data.oci_core_services.svc01.services.0.id
    }
    vcn_id = oci_core_vcn.vcn01.id

    #Optional
    display_name = "sgw01"
}


EOF

 

cat <<-'EOF' > outputs.tf

output "cmp01_id" {
  value = oci_identity_compartment.cmp01.id
  description = "cmp01.id"
}

output "vcn01_id" {
  value = oci_core_vcn.vcn01.id
  description = "vcn01.id"
}

output "igw01_id" {
  value = oci_core_internet_gateway.igw01.id
  description = "igw01.id"
}
output "rt01_id" {
  value = oci_core_route_table.rt01.id
  description = "rt01.id"
}

output "sl01_id" {
  value = oci_core_security_list.sl01.id
  description = "sl01.id"
}

output "subnet01_id" {
  value = oci_core_subnet.subnet01.id
  description = "subnet01.id"
}
output "subnet02_id" {
  value = oci_core_subnet.subnet02.id
  description = "subnet02.id"
}

output "ngw01_id" {
  value = oci_core_nat_gateway.ngw01.id
  description = "ngw01.id"
}

output "svc01_id" {
  value = data.oci_core_services.svc01.services.0.id
  description = "svc01.id"
}

output "sgw01_id" {
  value = oci_core_service_gateway.sgw01.id
  description = "sgw01.id"
}

EOF

 


terraform init
terraform fmt
terraform -version

# export TF_VAR_compartment_name=cmp01


terraform plan

 

terraform apply -auto-approve


# terraform destroy -auto-approve

 

 

 

-- 2. Oracleベース・データベース作成

oci db version list \
--compartment-id ocid1.compartment.oc1..111111111111111111111111111111111111111111111111111111111111 

oci db system-shape list \
--compartment-id ocid1.compartment.oc1..111111111111111111111111111111111111111111111111111111111111  \
--query 'sort_by(data, &"name").{"name":"name","shape":"shape","available-core-count":"available-core-count"}' \
--output table

 

oci db system list \
--compartment-id ocid1.compartment.oc1..111111111111111111111111111111111111111111111111111111111111  \
--query 'data.{"display-name":"display-name","id":"id","lifecycle-state":"lifecycle-state"}' \
--output table


oci db system launch \
--compartment-id ocid1.compartment.oc1..111111111111111111111111111111111111111111111111111111111111  \
--admin-password 'passwordpassword' \
--availability-domain OEIw:US-ASHBURN-AD-3 \
--cpu-core-count 1 \
--database-edition ENTERPRISE_EDITION \
--db-name db11 \
--db-version 19.21.0.0 \
--hostname orcl11 \
--shape VM.Standard.A1.Flex \
--ssh-authorized-keys-file "$HOME/.ssh/id_rsa.pub" \
--subnet-id ocid1.subnet.oc1.iad.111111111111111111111111111111111111111111111111111111111111 \
--auto-backup-enabled false \
--character-set AL32UTF8 \
--cluster-name cluster11 \
--db-unique-name dbu11 \
--db-workload OLTP \
--disk-redundancy NORMAL \
--display-name dbs11 \
--domain subnet01.vcn01.oraclevcn.com \
--initial-data-storage-size-in-gb 256 \
--is-diagnostics-events-enabled false \
--is-health-monitoring-enabled false \
--is-incident-logs-enabled false \
--license-model LICENSE_INCLUDED \
--ncharacter-set AL16UTF16 \
--node-count 1 \
--pdb-name dbu11pdb11 \
--private-ip 10.0.1.11 \
--storage-management LVM \
--storage-performance BALANCED \
--time-zone "Asia/Tokyo" 

 


Ampere A1シェイプは、Logical Volume Managerでのみサポートされています。
Ampere A1シェイプは、単一ノードのDBシステムでのみサポートされます。

 

oci db system list \
--compartment-id ocid1.compartment.oc1..111111111111111111111111111111111111111111111111111111111111 \
--query 'data.{"display-name":"display-name","id":"id","lifecycle-state":"lifecycle-state"}' \
--output table

 

oci db node list \
--compartment-id ocid1.compartment.oc1..111111111111111111111111111111111111111111111111111111111111 \
--db-system-id ocid1.dbsystem.oc1.iad.111111111111111111111111111111111111111111111111111111111111 


oci db db-home list \
--compartment-id ocid1.compartment.oc1..111111111111111111111111111111111111111111111111111111111111 \
--db-system-id ocid1.dbsystem.oc1.iad.111111111111111111111111111111111111111111111111111111111111 

oci db database list \
--compartment-id ocid1.compartment.oc1..111111111111111111111111111111111111111111111111111111111111 \
--db-system-id ocid1.dbsystem.oc1.iad.111111111111111111111111111111111111111111111111111111111111 


while true ; do
oci db system list \
--compartment-id ocid1.compartment.oc1..111111111111111111111111111111111111111111111111111111111111 \
--query 'data.{"display-name":"display-name","id":"id","lifecycle-state":"lifecycle-state"}' \
--output table ; date; sleep 60
done

 

oci db system terminate \
--db-system-id ocid1.dbsystem.oc1.iad.111111111111111111111111111111111111111111111111111111111111 \
--force 

 

-- 3. Data Guardの構成

oci db data-guard-association list \
--database-id ocid1.database.oc1.iad.111111111111111111111111111111111111111111111111111111111111 \
--all 


oci db data-guard-association create with-new-db-system --generate-full-command-json-input


oci db data-guard-association create with-new-db-system \
--availability-domain OEIw:US-ASHBURN-AD-2 \
--creation-type "" \
--database-admin-password 'passwordpassword' \
--database-id ocid1.database.oc1.iad.111111111111111111111111111111111111111111111111111111111111 \
--display-name dbs22 \
--hostname orcl22 \
--protection-mode MAXIMUM_PERFORMANCE \
--subnet-id ocid1.subnet.oc1.iad.111111111111111111111111111111111111111111111111111111111111 \
--transport-type ASYNC \
--cpu-core-count 1 \
--is-active-data-guard-enabled false \
--is-diagnostics-events-enabled false \
--is-health-monitoring-enabled false \
--is-incident-logs-enabled false \
--license-model LICENSE_INCLUDED \
--node-count 1 \
--private-ip 10.0.1.22 \
--shape VM.Standard.A1.Flex \
--storage-performance BALANCED \
--time-zone "Asia/Tokyo" 


※creation-typeは空文字でOK


oci db system list \
--compartment-id ocid1.compartment.oc1..111111111111111111111111111111111111111111111111111111111111 \
--query 'data.{"display-name":"display-name","id":"id","lifecycle-state":"lifecycle-state"}' \
--output table

while true ; do
oci db system list \
--compartment-id ocid1.compartment.oc1..111111111111111111111111111111111111111111111111111111111111 \
--query 'data.{"display-name":"display-name","id":"id","lifecycle-state":"lifecycle-state"}' \
--output table ; date; sleep 60
done


しばらく待つとスタンバイ側のDBシステムのPROVISIONINGが始まる

 

-- 4. DB接続

ノードのパブリックIPを確認

 

ssh -i $HOME/.ssh/id_rsa opc@192.0.2.1
ssh -i $HOME/.ssh/id_rsa opc@192.0.2.2

 

sudo su -

dbcli describe-system
dbcli list-nodes
dbcli list-dbhomes
dbcli list-databases

dbcli describe-dbhome --dbhomeid 42c0d558-06ee-47a0-af81-ac52510d6674
dbcli describe-database --dbid 26ccca54-b1b2-427b-9812-acdd2a01b74d

 


su - oracle
sqlplus / as sysdba

show pdbs

select name, db_unique_name, open_mode, database_role from v$database;

 


-- 5. Data Guardの切り替え

-- 5.1 スイッチオーバー
※プライマリDB側で実行

oci db data-guard-association switchover \
--data-guard-association-id ocid1.dgassociation.oc1.iad.111111111111111111111111111111111111111111111111111111111111 \
--database-admin-password 'passwordpassword' \
--database-id ocid1.database.oc1.iad.111111111111111111111111111111111111111111111111111111111111 


oci db data-guard-association switchover \
--data-guard-association-id ocid1.dgassociation.oc1.iad.111111111111111111111111111111111111111111111111111111111111 \
--database-admin-password 'passwordpassword' \
--database-id ocid1.database.oc1.iad.111111111111111111111111111111111111111111111111111111111111 


-- 5.2 フェイルオーバー

※スタンバイDB側で実行

oci db data-guard-association failover \
--data-guard-association-id ocid1.dgassociation.oc1.iad.111111111111111111111111111111111111111111111111111111111111 \
--database-admin-password 'passwordpassword' \
--database-id ocid1.database.oc1.iad.111111111111111111111111111111111111111111111111111111111111