2016年12月23日 星期五

Oracle Database 12c Release 1 (12.1) Multitenant: Oracle Pluggable Database

Using DBMS_PDB:

最近研究下Oracle 12c中的新特性,下面主要是記錄 convert non-cdb to cdb

1. orcl is non-cdb database
2. orclcdb is cdb database

可以先建立好 CDB 空殼即可

SQL> select name,CDB from v$database;
NAME      CDB
--------- ---
orclcdb      YES

SQL> select name,CDB from v$database;
NAME      CDB
--------- ---
orcl      NO

[oracle@oracle12c ~]$ echo $ORACLE_SID
orcl
[oracle@oracle12c ~]$ sqlplus  / as sysdba
SQL*Plus: Release 12.1.0.2.0 Production on Fri Dec 23 11:39:18 2016
Copyright (c) 1982, 2014, Oracle.  All rights reserved.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

SQL> startup open read only;
ORACLE instance started.

Total System Global Area 2315255808 bytes
Fixed Size                  2926952 bytes
Variable Size            1409287832 bytes
Database Buffers          889192448 bytes
Redo Buffers               13848576 bytes
Database mounted.
Database opened.



Datafile Location
SQL> select name from v$datafile;

NAME
--------------------------------------------------------------------------------
/u02/oradata/orcl/system01.dbf
/u02/oradata/orcl/gp_data01.dbf
/u02/oradata/orcl/sysaux01.dbf
/u02/oradata/orcl/undotbs01.dbf
/u02/oradata/orcl/gerp01.dbf
/u02/oradata/orcl/users01.dbf
/u02/oradata/orcl/perfstat.dbf
/u02/oradata/orcl/data_03.dbf
/u02/oradata/orcl/duty_03.dbf
/u02/oradata/orcl/duty_01.dbf
/u02/oradata/orcl/gerp02.dbf
/u02/oradata/orcl/data_02.dbf
/u02/oradata/orcl/users02.dbf
/u02/oradata/orcl/users03.dbf
/u02/oradata/orcl/users04.dbf
/u02/oradata/orcl/users05.dbf
/u02/oradata/orcl/users06.dbf
/u02/oradata/orcl/users07.dbf
/u02/oradata/orcl/users08.dbf
/u02/oradata/orcl/users09.dbf
/u02/oradata/orcl/data_01.dbf
/u03/oraindx/orcl/indx01.dbf
/u03/oraindx/orcl/indx02.dbf
/u02/oradata/orcl/duty_02.dbf

24 rows selected.



使用DBMS_PDB.DESCRIBE  non-CDB    創建 XML 描述文件。
SQL> BEGIN
  DBMS_PDB.DESCRIBE(
    pdb_descr_file => '/cdb/orcl.xml');      --> 此路徑自己設定
END;
/
PL/SQL procedure successfully completed.



Execute the Plug In Check and check PDB_PLUG_IN_VIOLATIONS when the result of the plug in check is
"NO":
SQL> SET SERVEROUTPUT ON
DECLARE
compatible CONSTANT VARCHAR2(3) := CASE
DBMS_PDB.CHECK_PLUG_COMPATIBILITY(
pdb_descr_file => '/cdb/orcl.xml',
pdb_name => 'pdborcl2')
WHEN TRUE THEN 'YES' ELSE 'NO'
END;
BEGIN
DBMS_OUTPUT.PUT_LINE(compatible);
END;
/

PL/SQL procedure successfully completed.

select message, type, status from pdb_plug_in_violations where type like '%ERR%';


Shutdown the non-CDB database.
export ORACLE_SID=orcl
sqlplus / as sysdba
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.



連接到 CDB 並使用 non-CDB 描述數據庫的XML文件創建新的PDB。 請記得配置FILE_NAME_CONVERT參數將現有文件轉換為新位置。
[oracle@oracle12c ~]$ export ORACLE_SID=orclcdb
[oracle@oracle12c ~]$ echo $ORACLE_SID
orclcdb

sqlplus / as sysdba

SQL> CREATE PLUGGABLE DATABASE pdborcl USING '/cdb/orcl.xml'
COPY
FILE_NAME_CONVERT = ('/u02/oradata/orcl/','/cdb/oradata/orclcdb/pdborcl/','/u03/oraindx/orcl/','/cdb/oradata/orclcdb/pdborcl/');

Pluggable database created.

切換到 PDB container 並且執行 "$ORACLE_HOME/rdbms/admin/noncdb_to_pdb.sql" script 
ALTER SESSION SET CONTAINER=pdborcl;

@$ORACLE_HOME/rdbms/admin/noncdb_to_pdb.sql

開啟 PDB 並且確認
ALTER SESSION SET CONTAINER=pdborcl;
ALTER PLUGGABLE DATABASE OPEN;

SQL> SELECT name, open_mode FROM v$pdbs;

NAME                           OPEN_MODE
------------------------------ ----------
PDBORCL                        READ WRITE

1 row selected.

SQL>

確認目前狀態沒問題,最好備份一次。
SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         3 PDBORCL                        READ WRITE NO

SQL> select open_mode from v$database;

OPEN_MODE
--------------------
READ WRITE

1 row selected.

SQL> select name, CDB from v$database;

NAME      CDB
--------- ---
ORCLCDB   YES

1 row selected.