[OCI]OCI CLIを用いてSQLを使用せずにDBシステム内のPDBの作成/起動/停止/削除をやってみた

# はじめに

OCIコンソールからDBシステム内のPDBを管理できるようになったのに合わせて、OCI CLIを使用してPDBを管理できるようになりました。

```
oci db pluggable-database
```

OCI CLIを用いたPDBの基本的な操作を試してみました。

# 1.PDBの一覧取得
以下のコマンドでPDBの一覧を表示できます。

```
oci db pluggable-database list
```

以下のようにコンパートメントを指定すると、コンパートメント内のPDBの一覧が取得できます。
```
oci db pluggable-database list -c <コンパートメントのOCID>
```
コンパートメントのOCIDを指定して、PDBの一覧を取得してみます。

```
[opc@work ~]$ oci db pluggable-database list -c ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx
{
  "data": [
    {
      "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
      "connection-strings": {
        "all-connection-strings": {
          "pdbDefault": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB1.subnetmds.vcnmds.oraclevcn.com",
          "pdbIpDefault": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB1.subnetmds.vcnmds.oraclevcn.com)))"
        },
        "pdb-default": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB1.subnetmds.vcnmds.oraclevcn.com",
        "pdb-ip-default": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB1.subnetmds.vcnmds.oraclevcn.com)))"
      },
      "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
      "defined-tags": {},
      "freeform-tags": {},
      "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqay753w3gqqgymzzizjhkgo4tphilxncm3sktlij2w2d4a",
      "is-restricted": null,
      "lifecycle-details": null,
      "lifecycle-state": "AVAILABLE",
      "open-mode": "READ_WRITE",
      "pdb-name": "PDB1",
      "time-created": "2021-10-18T02:06:34.125000+00:00"
    }
  ]
}
[opc@work ~]$ 
```
以下のようにDBを指定すると、DB内のPDBの一覧が取得できます。

```
oci db pluggable-database list --database-id <データベースのOCID>
```
DBのOCIDを指定して、PDBの一覧を取得してみます。

```
[opc@test ~]$ oci db pluggable-database list --database-id ocid1.database.oc1.ap-tokyo-1.abxhiljrnxmsqvqd5ewkvvh3ehkbwf5jc7b5aycfi7nzkxa3sqde35dww4aa
{
  "data": [
    {
      "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
      "connection-strings": {
        "all-connection-strings": {
          "pdbDefault": "testdb.subnetmds.vcnmds.oraclevcn.com:1521/PDB1.subnetmds.vcnmds.oraclevcn.com",
          "pdbIpDefault": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.31)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB1.subnetmds.vcnmds.oraclevcn.com)))"
        },
        "pdb-default": "testdb.subnetmds.vcnmds.oraclevcn.com:1521/PDB1.subnetmds.vcnmds.oraclevcn.com",
        "pdb-ip-default": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.31)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB1.subnetmds.vcnmds.oraclevcn.com)))"
      },
      "container-database-id": "ocid1.database.oc1.ap-tokyo-1.abxhiljrnxmsqvqd5ewkvvh3ehkbwf5jc7b5aycfi7nzkxa3sqde35dww4aa",
      "defined-tags": {},
      "freeform-tags": {},
      "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqapgz6udpbbidlinttilt6qa7hzjbijjs4acq4ewxl2mra",
      "is-restricted": null,
      "lifecycle-details": null,
      "lifecycle-state": "AVAILABLE",
      "open-mode": "READ_WRITE",
      "pdb-name": "PDB1",
      "time-created": "2021-07-21T11:57:42.893000+00:00"
    }
  ]
}
[opc@test ~]$
```

# 2.PDB情報の取得
以下のコマンドで、指定したPDBの情報を取得することができます。

```
oci db pluggable-database get --pluggable-database-id <PDBのOCID>
```
PDBのOCIDを指定して、PDBの情報を取得してみます。

```
[opc@work ~]$ oci db pluggable-database get --pluggable-database-id ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqay753w3gqqgymzzizjhkgo4tphilxncm3sktlij2w2d4a
{
  "data": {
    "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
    "connection-strings": {
      "all-connection-strings": {
        "pdbDefault": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB1.subnetmds.vcnmds.oraclevcn.com",
        "pdbIpDefault": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB1.subnetmds.vcnmds.oraclevcn.com)))"
      },
      "pdb-default": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB1.subnetmds.vcnmds.oraclevcn.com",
      "pdb-ip-default": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB1.subnetmds.vcnmds.oraclevcn.com)))"
    },
    "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
    "defined-tags": {},
    "freeform-tags": {},
    "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqay753w3gqqgymzzizjhkgo4tphilxncm3sktlij2w2d4a",
    "is-restricted": null,
    "lifecycle-details": null,
    "lifecycle-state": "AVAILABLE",
    "open-mode": "READ_WRITE",
    "pdb-name": "PDB1",
    "time-created": "2021-10-18T02:06:34.125000+00:00"
  }
}
[opc@work ~]$ 
```

# 3.PDBの作成
以下のコマンドで、PDBを作成することができます。

```
oci db pluggable-database create --container-database-id <CDB(DB)のOCID> --pdb-name <作成するPDBの名前> --pdb-admin-password <作成するPDBの管理者パスワード> --tde-wallet-password <CDBのTDEウォレットのパスワード>
```
以下のパラメータを指定して、PDBを作成してみます。
DB(CDB）のOCID：ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa
作成するPDBの名前：PDB2
PDBの管理者パスワード：Demo#1Demo#1
CDBのTDEウォレットのパスワード：Demo#1Demo#1

```
[opc@work ~]$ oci db pluggable-database create --container-database-id ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa --pdb-name PDB2 --pdb-admin-password Demo#1Demo#1 --tde-wallet-password Demo#1Demo#1
{
  "data": {
    "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
    "connection-strings": null,
    "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
    "defined-tags": {},
    "freeform-tags": {},
    "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa",
    "is-restricted": null,
    "lifecycle-details": null,
    "lifecycle-state": "PROVISIONING",
    "open-mode": "READ_WRITE",
    "pdb-name": "PDB2",
    "time-created": "2021-11-30T06:45:18.808000+00:00"
  },
  "etag": "e980e599",
  "opc-work-request-id": "ocid1.coreservicesworkrequest.oc1.ap-tokyo-1.abxhiljrsrel2dmc5cyrbqsiurmmeq6xtbjscfgl2ssoop4rvzc2ezbq262q"
}
[opc@work ~]$ 
```
コンソールでDBの詳細画面を確認すると、PDB2がプロビジョニング中になっていることが確認できます。
![スクリーンショット 2021-11-30 15.46.10.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/784824/106ab445-47f7-0a3e-2ed6-ccdbb766f060.png)

OCI CLIで、CDBを指定してPDBの一覧を表示してみます。

```
[opc@work ~]$ oci db pluggable-database list --database-id ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa
{
  "data": [
    {
      "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
      "connection-strings": {
        "all-connection-strings": {
          "pdbDefault": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB1.subnetmds.vcnmds.oraclevcn.com",
          "pdbIpDefault": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB1.subnetmds.vcnmds.oraclevcn.com)))"
        },
        "pdb-default": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB1.subnetmds.vcnmds.oraclevcn.com",
        "pdb-ip-default": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB1.subnetmds.vcnmds.oraclevcn.com)))"
      },
      "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
      "defined-tags": {},
      "freeform-tags": {},
      "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqay753w3gqqgymzzizjhkgo4tphilxncm3sktlij2w2d4a",
      "is-restricted": null,
      "lifecycle-details": null,
      "lifecycle-state": "AVAILABLE",
      "open-mode": "READ_WRITE",
      "pdb-name": "PDB1",
      "time-created": "2021-10-18T02:06:34.125000+00:00"
    },
    {
      "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
      "connection-strings": null,
      "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
      "defined-tags": {},
      "freeform-tags": {},
      "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa",
      "is-restricted": null,
      "lifecycle-details": null,
      "lifecycle-state": "PROVISIONING",
      "open-mode": "READ_WRITE",
      "pdb-name": "PDB2",
      "time-created": "2021-11-30T06:45:18.808000+00:00"
    }
  ]
}
[opc@work ~]$ 
```
PDB2の"lifecycle-state"が"PROVISIONING"であることが確認できます。

しばらく待つと、PDB2が使用可能になります。
![スクリーンショット 2021-11-30 15.51.20.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/784824/237e743c-c0f9-7728-d5ee-f207ad01aecd.png)
再度OCI CLIで、CDBを指定してPDBの一覧を表示してみます。

```
[opc@work ~]$ oci db pluggable-database list --database-id ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa
{
  "data": [
    {
      "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
      "connection-strings": {
        "all-connection-strings": {
          "pdbDefault": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB1.subnetmds.vcnmds.oraclevcn.com",
          "pdbIpDefault": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB1.subnetmds.vcnmds.oraclevcn.com)))"
        },
        "pdb-default": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB1.subnetmds.vcnmds.oraclevcn.com",
        "pdb-ip-default": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB1.subnetmds.vcnmds.oraclevcn.com)))"
      },
      "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
      "defined-tags": {},
      "freeform-tags": {},
      "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqay753w3gqqgymzzizjhkgo4tphilxncm3sktlij2w2d4a",
      "is-restricted": null,
      "lifecycle-details": null,
      "lifecycle-state": "AVAILABLE",
      "open-mode": "READ_WRITE",
      "pdb-name": "PDB1",
      "time-created": "2021-10-18T02:06:34.125000+00:00"
    },
    {
      "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
      "connection-strings": {
        "all-connection-strings": {
          "pdbDefault": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
          "pdbIpDefault": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
        },
        "pdb-default": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
        "pdb-ip-default": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
      },
      "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
      "defined-tags": {},
      "freeform-tags": {},
      "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa",
      "is-restricted": null,
      "lifecycle-details": null,
      "lifecycle-state": "AVAILABLE",
      "open-mode": "READ_WRITE",
      "pdb-name": "PDB2",
      "time-created": "2021-11-30T06:45:18.808000+00:00"
    }
  ]
}
[opc@work ~]$ 
```
PDB2の"lifecycle-state"が"AVAILABLE"になっていることが確認できました。

# 4.PDBの停止
以下のコマンドで、PDBを指定してPDBを停止することができます。

```
oci db pluggable-database stop --pluggable-database-id <停止するPDBのOCID>
```

PDB2を操作するために、コンソールでPDB2のOCIDを確認しておきます。
![スクリーンショット 2021-11-30 15.52.24.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/784824/ef7906ed-a8ae-362e-044f-2821c492d931.png)
現在のPDB2の状態を確認します。

```
[opc@work ~]$ oci db pluggable-database get --pluggable-database-id ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa
{
  "data": {
    "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
    "connection-strings": {
      "all-connection-strings": {
        "pdbDefault": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
        "pdbIpDefault": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
      },
      "pdb-default": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
      "pdb-ip-default": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
    },
    "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
    "defined-tags": {},
    "freeform-tags": {},
    "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa",
    "is-restricted": null,
    "lifecycle-details": null,
    "lifecycle-state": "AVAILABLE",
    "open-mode": "READ_WRITE",
    "pdb-name": "PDB2",
    "time-created": "2021-11-30T06:45:18.808000+00:00"
  }
}
[opc@work ~]$ 
```
"lifecycle-state"が"AVAILABLE"、"open-mode"が"READ_WRITE"であることが確認できます。
OCI CLIでPDB2を停止します。

```
[opc@work ~]$ oci db pluggable-database stop --pluggable-database-id ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa
{
  "data": {
    "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
    "connection-strings": {
      "all-connection-strings": {
        "pdbDefault": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
        "pdbIpDefault": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
      },
      "pdb-default": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
      "pdb-ip-default": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
    },
    "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
    "defined-tags": {},
    "freeform-tags": {},
    "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa",
    "is-restricted": null,
    "lifecycle-details": null,
    "lifecycle-state": "UPDATING",
    "open-mode": "READ_WRITE",
    "pdb-name": "PDB2",
    "time-created": "2021-11-30T06:45:18.808000+00:00"
  },
  "etag": "ed200cf4",
  "opc-work-request-id": "ocid1.coreservicesworkrequest.oc1.ap-tokyo-1.abxhiljr5rdir5jb2jwwbib5y36z2do7yetmrgfoergh7qbjnuhbsqyrszaa"
}
[opc@work ~]$ 
```
PDB2の"lifecycle-state"が"UPDATING"になりました。
しばらく待ってからPDB2の状態を確認すると、"open-mode"が"MOUNTED"に変更されていることが確認できます。

```
[opc@work ~]$ oci db pluggable-database get --pluggable-database-id ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa
{
  "data": {
    "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
    "connection-strings": {
      "all-connection-strings": {
        "pdbDefault": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
        "pdbIpDefault": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
      },
      "pdb-default": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
      "pdb-ip-default": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
    },
    "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
    "defined-tags": {},
    "freeform-tags": {},
    "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa",
    "is-restricted": null,
    "lifecycle-details": null,
    "lifecycle-state": "AVAILABLE",
    "open-mode": "MOUNTED",
    "pdb-name": "PDB2",
    "time-created": "2021-11-30T06:45:18.808000+00:00"
  }
}
[opc@work ~]$
```

# 5.PDBの開始
以下のコマンドで、PDBを指定して停止しているPDBを開始することができます。

```
oci db pluggable-database start --pluggable-database-id <開始するPDBのOCID>
```

先ほど停止したPDB2を開始します。

```
[opc@work ~]$ oci db pluggable-database start --pluggable-database-id ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa
{
  "data": {
    "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
    "connection-strings": {
      "all-connection-strings": {
        "pdbDefault": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
        "pdbIpDefault": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
      },
      "pdb-default": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
      "pdb-ip-default": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
    },
    "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
    "defined-tags": {},
    "freeform-tags": {},
    "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa",
    "is-restricted": null,
    "lifecycle-details": null,
    "lifecycle-state": "UPDATING",
    "open-mode": "MOUNTED",
    "pdb-name": "PDB2",
    "time-created": "2021-11-30T06:45:18.808000+00:00"
  },
  "etag": "65cb8f83",
  "opc-work-request-id": "ocid1.coreservicesworkrequest.oc1.ap-tokyo-1.abxhiljrvfbps55zq3cdbp5dbovusclg3eeztrkxos37p2mlwuc32oanvssq"
}
[opc@work ~]$ 
```

しばらく待ってからPDB2の状態を確認すると、"open-mode"が"READ_WRITE"に変更されていることが確認できました。

```
[opc@work ~]$ oci db pluggable-database get --pluggable-database-id ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa
{
  "data": {
    "compartment-id": "ocid1.compartment.oc1..aaaaaaaaa4xfcxvm53g6kqpnjfsgdgiwtzwg425xxxxxxxxxxxxxxxxxxxxx",
    "connection-strings": {
      "all-connection-strings": {
        "pdbDefault": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
        "pdbIpDefault": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
      },
      "pdb-default": "demodb1.subnetmds.vcnmds.oraclevcn.com:1521/PDB2.subnetmds.vcnmds.oraclevcn.com",
      "pdb-ip-default": "(DESCRIPTION=(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PDB2.subnetmds.vcnmds.oraclevcn.com)))"
    },
    "container-database-id": "ocid1.database.oc1.ap-tokyo-1.anxhiljrssl65iqa3qk6lue7obcacw5l3c4aoyojs4gqsxljcqqv4y7tqtsa",
    "defined-tags": {},
    "freeform-tags": {},
    "id": "ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa",
    "is-restricted": null,
    "lifecycle-details": null,
    "lifecycle-state": "AVAILABLE",
    "open-mode": "READ_WRITE",
    "pdb-name": "PDB2",
    "time-created": "2021-11-30T06:45:18.808000+00:00"
  }
}
[opc@work ~]$ 
```
OCI CLIから実行したPDBの作成、起動、停止はコンソールのPDB詳細画面の作業リクエスト欄でも確認できます。
![スクリーンショット 2021-11-30 16.13.24.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/784824/c1442e97-ab7d-dbf3-b98e-4048217abd7d.png)


# 6.PDBの削除

以下のコマンドでPDBを削除できます。

```
oci db pluggable-database delete --pluggable-database-id <削除するPDBのOCID>
```
PDB2を削除します。
削除の際には確認のプロンプトが表示されるので、Yを入力します。

```
[opc@work ~]$ oci db pluggable-database delete --pluggable-database-id ocid1.pluggabledatabase.oc1.ap-tokyo-1.anxhiljrssl65iqazmjyedcscofavnyifpt6rjmjiiu7jhy23dsc45s4lpfa
Are you sure you want to delete this resource? [y/N]: y
{
  "opc-work-request-id": "ocid1.coreservicesworkrequest.oc1.ap-tokyo-1.abxhiljrjjyiliruhskeow55xpa2x7552lkpjq2yfjvps66kyysfptsc3f7a"
}
[opc@work ~]$
```
コンソールのPDB2の詳細画面を確認すると「終了中」になっています。
![スクリーンショット 2021-11-30 16.14.58.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/784824/06378000-da05-8a2e-38b8-f7219400c662.png)
しばらく待つと、PDB2が「終了済」になり、PDB2が削除されました。
![スクリーンショット 2021-11-30 16.17.47.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/784824/df63647d-58e3-fdc7-00c6-3274d214c04b.png)

# まとめ
以上のように、OCI CLIを用いてDBシステム内のPDBの作成、起動、停止、削除等が行えることがわかりました。
これらの操作の他にPDBのローカル・クローンやリモート・クローンも実行可能です。

# 参考情報
[OCI CLI Command Refference db >> pluggable-database](https://docs.oracle.com/en-us/iaas/tools/oci-cli/3.2.0/oci_cli_docs/cmdref/db/pluggable-database.html)