1. 程式人生 > 資料庫 >ORACLE_OCP多租戶之12C手工建立CDB PDB版本

ORACLE_OCP多租戶之12C手工建立CDB PDB版本

ORACLE_OCP多租戶之12C手工建立CDB PDB版本

一、手工建立CDB

第一步:首先修改oracle環境變數為cdb1:
  • 可以使用以下命令進行修改:
  • 切換到oracle使用者中: 後續步驟都在oracle使用者中操作。

    su - oracle


[oracle@oracle12-cdb ~]$ vim .bash_profile
[oracle@oracle12-cdb ~]$ cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH
export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_SID=cdb1
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/12.2.0/db_1
export INVENTORY_LOCATION=/u01/oraInventory
export TNS_ADMIN=$ORACLE_HOME/network/admin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
export NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"
export PATH=$ORACLE_HOME/bin:/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:$PATH:$HOME/bin
umask 022

alias sqlplus='rlwrap sqlplus'
alias rman='rlwrap rman'
[oracle@oracle12-cdb ~]$

  • 生成環境變數

    [oracle@oracle12-cdb ~]$ source .bash_profile

第二步:建立密碼檔案

[oracle@oracle12-cdb dbs]$ orapwd file=$ORACLE_HOME/dbs/orapwcdb1 password=oracle force=y format=12

第三步:建立pfile。使用下面的命令和引數去建立pfile
  • 請確定你已經建立好了一個監聽。
  • cd /home/oracle --注意這是LINUX作業系統命令不能在sqlplus下執行。
  • mkdir -p /u01/app/oracle/admin/cdb1/adump --建立cdb1的審計目錄
  • mkdir -p /u01/app/oracle/oradata/cdb1 --建立cdb1的資料檔案存放目錄
  • mkdir -p /u01/app/oracle/oradata/cdb1/pdbseed --建立pdbseed目錄
[oracle@oracle12-cdb ~]$ mkdir -p /u01/app/oracle/admin/cdb1/adump
[oracle@oracle12-cdb ~]$ mkdir -p /u01/app/oracle/oradata/cdb1
[oracle@oracle12-cdb ~]$ mkdir -p /u01/app/oracle/oradata/cdb1/pdbseed
[oracle@oracle12-cdb ~]$ vim /home/oracle/init_cdb1.ora
[oracle@oracle12-cdb ~]$ cat /home/oracle/init_cdb1.ora
*.audit_file_dest='/u01/app/oracle/admin/cdb1/adump'
*.audit_trail='db'
*.control_files='/u01/app/oracle/oradata/cdb1/control01.ctl','/u01/app/oracle/oradata/cdb1/control02.ctl'
*.db_block_size=8192
*.db_name='cdb1'
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=cdb1XDB)'
*.enable_pluggable_database=true
*.open_cursors=300
*.pga_aggregate_target=200m
*.processes=300
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=800m
*.undo_tablespace='UNDOTBS1'
第四步:使用pfile建立spfile
  • 使用sqlplus / as sysdba進入到oracle資料庫,使用我們創建出來的pfile啟動資料庫到nomount狀態,並建立spfile然後重新把資料庫啟動到startup nomount
    [oracle@oracle12-cdb ~]$ sqlplus / as sysdba
    
    SQL*Plus: Release 12.2.0.1.0 Production on Sat Jan 9 14:26:57 2021
    
    Copyright (c) 1982, 2016, Oracle.  All rights reserved.
    
    Connected to an idle instance.
    
    SQL> startup pfile='/home/oracle/init_cdb1.ora' nomount
    ORACLE instance started.
    
    Total System Global Area  838860800 bytes
    Fixed Size                  8798312 bytes
    Variable Size             343936920 bytes
    Database Buffers          478150656 bytes
    Redo Buffers                7974912 bytes
    SQL> create spfile from pfile='/home/oracle/init_cdb1.ora';
    
    File created.
    
    SQL> shutdown abort;
    ORACLE instance shut down.
    SQL> startup nomount;
    ORACLE instance started.
    
    Total System Global Area  838860800 bytes
    Fixed Size                  8798312 bytes
    Variable Size             343936920 bytes
    Database Buffers          478150656 bytes
    Redo Buffers                7974912 bytes
    SQL>
    
    
第五步:建立一個CDB的建庫指令碼
  • cd /home/oracle --不是重複了,是我要一直提醒大家,所有的相關檔案都必須放到這裡。
vim /home/oracle/create_cdb1.sql
CREATE DATABASE cdb1
  USER SYS IDENTIFIED BY oracle
  USER SYSTEM IDENTIFIED BY oracle
  LOGFILE GROUP 1 ('/u01/app/oracle/oradata/cdb1/redo01a.log','/u01/app/oracle/oradata/cdb1/redo01b.log')
             SIZE 100M BLOCKSIZE 512,
          GROUP 2 ('/u01/app/oracle/oradata/cdb1/redo02a.log','/u01/app/oracle/oradata/cdb1/redo02b.log')
             SIZE 100M BLOCKSIZE 512,
          GROUP 3 ('/u01/app/oracle/oradata/cdb1/redo03a.log','/u01/app/oracle/oradata/cdb1/redo03b.log')
             SIZE 100M BLOCKSIZE 512
  MAXLOGHISTORY 1
  MAXLOGFILES 16
  MAXLOGMEMBERS 3
  MAXDATAFILES 1024
  CHARACTER SET AL32UTF8
  NATIONAL CHARACTER SET AL16UTF16
  EXTENT MANAGEMENT LOCAL
  DATAFILE '/u01/app/oracle/oradata/cdb1/system01.dbf'
    SIZE 700M REUSE AUTOEXTEND ON NEXT 10240K MAXSIZE UNLIMITED
  SYSAUX DATAFILE '/u01/app/oracle/oradata/cdb1/sysaux01.dbf'
    SIZE 550M REUSE AUTOEXTEND ON NEXT 10240K MAXSIZE UNLIMITED
  DEFAULT TABLESPACE deftbs
     DATAFILE '/u01/app/oracle/oradata/cdb1/deftbs01.dbf'
     SIZE 500M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
  DEFAULT TEMPORARY TABLESPACE tempts1
     TEMPFILE '/u01/app/oracle/oradata/cdb1/temp01.dbf'
     SIZE 20M REUSE AUTOEXTEND ON NEXT 640K MAXSIZE UNLIMITED
  UNDO TABLESPACE undotbs1
     DATAFILE '/u01/app/oracle/oradata/cdb1/undotbs01.dbf'
     SIZE 200M REUSE AUTOEXTEND ON NEXT 5120K MAXSIZE UNLIMITED
  USER_DATA TABLESPACE usertbs
    DATAFILE '/u01/app/oracle/oradata/cdb1/pdbseed/usertbs01.dbf'
    SIZE 50M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
  ENABLE PLUGGABLE DATABASE
    SEED
    FILE_NAME_CONVERT = ('/u01/app/oracle/oradata/cdb1/', '/u01/app/oracle/oradata/cdb1/pdbseed/')
    SYSTEM DATAFILES SIZE 125M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
    SYSAUX DATAFILES SIZE 100M
    LOCAL UNDO ON;
  • 在已經啟動到nomount狀態下的資料庫sqlplus工具中執行該指令碼。

    SQL> @/home/oracle/create_cdb1.sql
    Database created.
    SQL>

  • 成功了!下一步我們執行一個指令碼。不過我們在執行下一個指令碼之前,我們需要執行點其他的東西,不然會出現錯誤。

    export PERL5LIB=$ORACLE_HOME/rdbms/admin:$PERL5LIB
    export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/perl/bin:$PATH
    cd $ORACLE_HOME/perl/lib/5.22.0/x86_64-linux-thread-multi/Hash
    ln -s Util.pm util.pm

  • 執行完成後,我們現在去執行下面的指令碼

    @?/rdbms/admin/catcdb.sql

  • 如果遇到讓你輸入引數的時候 ,輸入下面的引數:

    Enter value for 1: /home/oracle
    Enter value for 2: /home/oracle/cdb.log
    Enter new password for SYS: oracle
    Enter new password for SYSTEM: oracle
    Enter temporary tablespace name: temp

  • 當最後輸出這些時候代表指令碼執行成功:

    catcon: ALL catcon-related output will be written to [/home/oracle/catclust_catcon_39700.lst]
    catcon: See [/home/oracle/catclust*.log] files for output generated by scripts
    catcon: See [/home/oracle/catclust_.lst] files for spool files, if any
    catcon.pl: completed successfully
    catcon: ALL catcon-related output will be written to [/home/oracle/catfinal_catcon_40093.lst]
    catcon: See [/home/oracle/catfinal
    .log] files for output generated by scripts
    catcon: See [/home/oracle/catfinal_.lst] files for spool files, if any
    catcon.pl: completed successfully
    catcon: ALL catcon-related output will be written to [/home/oracle/catbundleapply_catcon_40180.lst]
    catcon: See [/home/oracle/catbundleapply
    .log] files for output generated by scripts
    catcon: See [/home/oracle/catbundleapply_.lst] files for spool files, if any
    catcon.pl: completed successfully
    catcon: ALL catcon-related output will be written to [/home/oracle/utlrp_catcon_40266.lst]
    catcon: See [/home/oracle/utlrp
    .log] files for output generated by scripts
    catcon: See [/home/oracle/utlrp_*.lst] files for spool files, if any
    catcon.pl: completed successfully

第六步:例項的關閉和開啟實驗
```
SQL> show user;
USER is "SYS"
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area  838860800 bytes
Fixed Size                  8798312 bytes
Variable Size             343936920 bytes
Database Buffers          478150656 bytes
Redo Buffers                7974912 bytes
Database mounted.
Database opened.
SQL> select * from dual;

D
-
X
SQL> set lines 300
SQL> col NAME for a40
SQL> select con_id, dbid, NAME, OPEN_MODE from v$pdbs;

    CON_ID       DBID NAME                                     OPEN_MODE
---------- ---------- ---------------------------------------- ----------
         2 3014806594 PDB$SEED                                 READ ONLY

SQL> show pdbs;

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
SQL>

```
可以看到沒問題,至此手工建立CDB的所有步驟完成了。

二、手工建立PDB

第一步:利用PDB$SEED建立PDB
  • 在SQLPLUS中執行以下的SQL語句

    CREATE PLUGGABLE DATABASE pdb1 ADMIN USER pdb1admin IDENTIFIED BY oracle
    FILE_NAME_CONVERT=(
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/system01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/system01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/sysaux01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/sysaux01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/undotbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/undotbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/deftbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/deftbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/usertbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/usertbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/temp01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/temp01.dbf’
    )
    STORAGE UNLIMITED TEMPFILE REUSE;

  • 如果你想對新建成的pdb1進行使用空間限制的話,可以寫成以下格式。

    CREATE PLUGGABLE DATABASE pdb1 ADMIN USER pdbadmin IDENTIFIED BY oracle
    FILE_NAME_CONVERT=(
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/system01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/system01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/sysaux01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/sysaux01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/undotbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/undotbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/deftbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/deftbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/usertbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/usertbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/temp01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/temp01.dbf’
    )
    STORAGE (
    MAXSIZE 2G
    MAX_SHARED_TEMP_SIZE 200M
    )
    TEMPFILE REUSE;

  • 結果:


SQL> show pdbs;

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB1                           MOUNTED
SQL> alter pluggable database pdb1 open;

Pluggable database altered.

SQL> show pdbs;

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB1                           READ WRITE NO
SQL>

三、使用已有的PDB去建立新的PDB

  • 我們需要注意幾點:
    • 使用的使用者必須擁有’CREATE PLUGGABLE DATABASE’的許可權(測試裡使用sys使用者)
    • 源PDB不可以是關閉狀態或者是mount狀態的
    • 如果CDB為shared undo,PDB必須為READ-ONLY狀態。才能進行相關複製,不然丟失事務。
    • 如果CDB不是歸檔模式,那麼PDB必須為READ-ONLY狀態。才能進行相關複製
第一步:建立測試PDB:pdbt
SQL> show con_name;

CON_NAME
------------------------------
CDB$ROOT
SQL> set lines 300
SQL> col NAME for a40
SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;

    CON_ID       DBID NAME                                     OPEN_MODE
---------- ---------- ---------------------------------------- ----------
         2 3014806594 PDB$SEED                                 READ ONLY
         3 2518959561 PDB1                                     READ WRITE

SQL>

CREATE PLUGGABLE DATABASE pdbt ADMIN USER pdbtadmin IDENTIFIED BY oracle
  FILE_NAME_CONVERT=(
    '/u01/app/oracle/oradata/cdb1/pdbseed/system01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/system01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbseed/sysaux01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/sysaux01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbseed/undotbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/undotbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbseed/deftbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/deftbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbseed/usertbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/usertbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbseed/temp01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/temp01.dbf'
  )
  STORAGE UNLIMITED TEMPFILE REUSE;
  • 嘗試把pdbt以READ ONLY方式開啟:

    SQL> ALTER PLUGGABLE DATABASE PDBt OPEN READ ONLY;
    ALTER PLUGGABLE DATABASE PDBt OPEN READ ONLY
    *
    ERROR at line 1:
    ORA-65085: cannot open pluggable database in read-only mode

    • 這裡會報錯,原因是:新的PDB一定要先啟動到READ WRITE模式,因為新的PDB狀態是NEW,而在NEW的這個模式下時,是不能啟動到READ ONLY的,第一次啟動到READ WRITE後,將PDB關掉,下次就能啟動到READ ONLY狀態了。
  • PDB狀態檢查語句:

set lines 500
col pdb_name for a10
col db_name for a10
col db_unique_name for a10
SQL> col db_version_string for a20
//查詢PDB的詳細資訊:
SQL> select pdb_name,pdb_id,pdb_dbid,pdb_guid,OP_TIMESTAMP,operation,db_version,db_name,db_unique_name,db_dbid,db_version_string from dba_pdb_history order by OP_TIMESTAMP;

PDB_NAME       PDB_ID   PDB_DBID PDB_GUID                         OP_TIMESTAMP        OPERATION        DB_VERSION DB_NAME    DB_UNIQUE_    DB_DBID DB_VERSION_STRING
---------- ---------- ---------- -------------------------------- ------------------- ---------------- ---------- ---------- ---------- ---------- --------------------
PDB1                3 2518959561 B87ED8793A3F9EEBE0538CBEA8C0C689 2021-01-09 15:52:05 CREATE            203424000 CDB1       cdb1       1035107902 12.2.0.1.0
PDBT                4 2174543479 B87F08D83EACA512E0538CBEA8C0D193 2021-01-09 16:05:37 CREATE            203424000 CDB1       cdb1       1035107902 12.2.0.1.0

//查詢PDB的狀態
SQL> select pdb_name, status from cdb_pdbs;

PDB_NAME   STATUS
---------- ----------
PDB$SEED   NORMAL
PDB1       NORMAL
PDBT       NEW

  • pdb的status有 NEW、NORMAL、UNPLUGGED、NEEDS UPGRADE、CONVERTING、UNUSABLE 幾種狀態,對這幾種狀態的解釋如下:(文件的最後有英文原版內容)
    • NEW -自建立以來,從未開啟過PDB。 必須以READ WRITE模式開啟該檔案,Oracle才能執行將PDB整合到CDB中並將其標記為NORMAL所需的處理。 如果嘗試以只讀方式開啟PDB,將引發錯誤。
    • NORMAL -準備使用(准許使用)的PDB。
    • UNPLUGGED -PDB已拔出。可以對其執行的唯一操作是DROP PLUGGABLE DATABASE。
    • NEEDS UPGRADE -PDB需要升級到已插入的CDB的版本。
    • CONVERTING -非CDB已插入CDB,並且正在進行轉換,以使其表現得像真實的PDB。
    • UNUSABLE -正在建立PDB或在建立PDB時遇到不可恢復的錯誤。 當狀態設定為“不可用”時,無法開啟PDB。 如果PDB在建立過程中由於遇到錯誤而保持此狀態,則只能將其刪除。 可以檢查告警日誌以確定是否存在錯誤.
第二步:PDB改到READ ONLY狀態
  • 首先:啟動PDB到READ WRITE狀態
SQL> ALTER PLUGGABLE DATABASE PDBt OPEN READ WRITE;

Pluggable database altered.

SQL>

  • 關閉:
SQL> ALTER PLUGGABLE DATABASE PDBt close;

Pluggable database altered.

SQL>

  • 然後啟動到READ ONLY狀態
SQL> ALTER PLUGGABLE DATABASE PDBt OPEN READ ONLY;

Pluggable database altered.

SQL>

第三步:執行PDB的克隆語句
CREATE PLUGGABLE DATABASE pdb2 FROM pdbt
  STORAGE UNLIMITED TEMPFILE REUSE
  FILE_NAME_CONVERT=(
    '/u01/app/oracle/oradata/cdb1/pdbt/system01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/system01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbt/sysaux01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/sysaux01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbt/undotbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/undotbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbt/deftbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/deftbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbt/usertbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/usertbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbt/temp01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/temp01.dbf'
  )
  PARALLEL 2;

四、把已拔出的PDB再插回到CDB中

  • 首先關掉PDB

    alter pluggable database pdb2 close;
    Pluggable database altered.
    SQL> alter pluggable database pdb2 unplug into ‘/home/oracle/pdb2.xml’;
    Pluggable database altered.
    SQL>

  • 刪除PDB有兩種方式 第一個是保留檔案第二個是不保留檔案

    drop pluggable database pdb2 including datafiles; #刪除資料庫和檔案
    drop pluggable database pdb2 keep datafiles; #刪除資料庫並儲存檔案
    #預設狀態下是保留檔案的。
    drop pluggable database pdb2;

  • 建立PDB

    create pluggable database pdb2 using ‘/home/oracle/pdb2.xml’ nocopy;
    alter pluggable database pdb2 open read wirte ;
    #或
    alter pluggable database pdb2 open;

  • 結果:

SQL> select pdb_name, status from cdb_pdbs;

PDB_NAME   STATUS
---------- ----------
PDB$SEED   NORMAL
PDB1       NORMAL
PDBT       NORMAL
PDB2       NEW

SQL> alter pluggable database pdb2 open;

Pluggable database altered.

SQL> select pdb_name, status from cdb_pdbs;

PDB_NAME   STATUS
---------- ----------
PDB$SEED   NORMAL
PDB1       NORMAL
PDBT       NORMAL
PDB2       NORMAL

SQL>