Oracle 12c系列(二)|PDB的创建

PDB数据库的创建可以从现存的数据库中复制数据文件,包括种子容器、可插拔数据库、non-CDB数据库,创建时可使用CREATE PLUGGABLE、RMAN、以及EM。

PDB数据库的创建可以从现存的数据库中复制数据文件,包括种子容器、可插拔数据库、non-CDB数据库,创建时可使用CREATE PLUGGABLE、RMAN、以及EM。

在12.1版本中在创建PDB时,SOURCE PDB必须处于read only状态,在12.2版本中,因为undo local mode新特性的推出,在创建PDB时,SOURCE PDB在read write状态,依然可以创建。

另外在12.2版本中Oracle推出了refresh PDB特性,具有对SOURCE PDB进行增量同步的功能。

• 使用CREATE PLUGGABLE命令可以使用以下资源创建PDB

1.CDB seed (PDB$SEED)
2.克隆已经存在的PDB
Local PDB
Remote PDB
3.non-CDB数据库
4.拔下的PDB

• 使用DBCA可以使用以下资源创建PDB

1.CDB seed (PDB$SEED)
2.RMAN备份
3.拔下的PDB

示例:(这里只演示使用create pluggable database命令方式创建PDB)

使用CDB seed创建PDB

1.在SQL*Plus中输入CREATE PLUGGABLE DATABASE语句
(这里创建未ypdb1的pdb,管理用户为ypdb1)

CREATE PLUGGABLE DATABASE ypdb1 ADMIN USER ypdb1 IDENTIFIED BY oracle
  STORAGE (MAXSIZE 2G)
  DEFAULT TABLESPACE ypdb1
    DATAFILE '/u01/app/oracle/oradata/ora12c/ypdb1/ypdb01.dbf' SIZE 100M AUTOEXTEND ON
  PATH_PREFIX = '/u01/app/oracle/oradata/ora12c/ypdb1/'
  FILE_NAME_CONVERT = ('/u01/app/oracle/oradata/ora12c/pdbseed', '/u01/app/oracle/oradata/ora12c/ypdb1');
![](http://blog.woqutech.com/wp-content/uploads/2018/02/2-1-300x85.png)

2.语句执行完毕之后查看创建完成的PDB:

sys. ora12c>show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 YPDB1                          MOUNTED
sys. ora12c>

克隆已经存在的PDB

这种方式可以通过使用当前CDB中的其他PDB,以及复制远端CDB中的PDB,还可以复制non-CDB数据库创建成PDB。

Clone a Local PDB

在Clone本地PDB时需要注意一下几点:

1.使用的用户必须拥有’CREATE PLUGGABLE DATABASE’的权限(测试里使用sys用户)
2.源PDB不可以是关闭状态
3.如果CDB为shared undo,PDB必须为READ-ONLY状态
4.如果CDB不是归档模式,那么PDB必须为READ-ONLY状态
PS:如果是Oracle Database 版本为12.1,那么PDB只能为READ-ONLY状态,因在12.1中undo模式,还只能选择shared undo。

sys. ora12c>show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 YPDB1                          READ WRITE NO
sys. ora12c>

这里使用YPDB1做为源PDB,创建的PDB为YPDB2。

(1) 检查是否为shared undo模式

COL PROPERTY_NAME FOR A30
COL PROPERTY_VALUE FOR A30
SELECT property_name, property_value
FROM database_properties
WHERE property_name='LOCAL_UNDO_ENABLED';

PROPERTY_NAME                  PROPERTY_VALUE
------------------------------ ------------------------------
LOCAL_UNDO_ENABLED             TRUE

(2) 检查是否未archivelog模式

sys. ora12c>ARCHIVE LOG LIST
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /u01/app/oracle/recovery/ora12c/arch
Oldest online log sequence     21
Next log sequence to archive   23
Current log sequence           23
sys. ora12c>

(3) 创建验证数据

yangyuhang. ypdb1>CONN /AS SYSDBA
Connected.
sys. ora12c>conn yangyuhang/yangyuhang@ypdb1
Connected.
yangyuhang. ypdb1>SELECT COUNT(*) FROM t;

  COUNT(*)
----------
     22198

Elapsed: 00:00:00.01
yangyuhang. ypdb1>

(4) 执行CREATE PLUGGABLE DATABASE语句(这里使用sys用户连接根容器)

CREATE PLUGGABLE DATABASE ypdb2 FROM ypdb1
  PATH_PREFIX = '/u01/app/oracle/oradata/ora12c/ypdb2/'
  FILE_NAME_CONVERT = ('/u01/app/oracle/oradata/ora12c/ypdb1/', '/u01/app/oracle/oradata/ora12c/ypdb2/')
  SERVICE_NAME_CONVERT = ('ypdb1t','ypdb2t');

(5) 查看创建完成的YPDB2

sys. ora12c>show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 YPDB1                          READ WRITE NO
         4 YPDB2                          MOUNTED
sys. ora12c>SELECT pdb_id, pdb_name, con_uid, status, creation_scn, con_id FROM cdb_pdbs;

    PDB_ID PDB_NAME                CON_UID STATUS     CREATION_SCN     CON_ID
---------- -------------------- ---------- ---------- ------------ ----------
         2 PDB$SEED             1453953285 NORMAL              213          2
         3 YPDB1                2376019304 NORMAL           697945          3
         4 YPDB2                3908707960 NEW              707501          4

Elapsed: 00:00:00.02
sys. ora12c>

(6) 检查数据文件

sys. ora12c>ALTER PLUGGABLE DATABASE YPDB2 OPEN;

Pluggable database altered.

Elapsed: 00:00:14.02
sys. ora12c>SELECT con_id, tablespace_name, file_name FROM cdb_data_files WHERE con_id=4;

CON_ID TABLESP FILE_NAME
------ ------- ---------------------------------------------------------
     4 SYSTEM  /u01/app/oracle/oradata/ora12c/ypdb2/system01.dbf
     4 SYSAUX  /u01/app/oracle/oradata/ora12c/ypdb2/sysaux01.dbf
     4 DEFTBS  /u01/app/oracle/oradata/ora12c/ypdb2/deftbs01.dbf
     4 USERTBS /u01/app/oracle/oradata/ora12c/ypdb2/usertbs01.dbf
     4 UNDO_1  /u01/app/oracle/oradata/ora12c/ypdb2/system01_i1_undo.dbf
     4 YPDB1   /u01/app/oracle/oradata/ora12c/ypdb2/ypdb01.dbf

6 rows selected.

Elapsed: 00:00:00.01
sys. ora12c>

(7) 检查service_name

sys. ora12c>SELECT service_id, name, network_name, enabled, pdb, con_id FROM cdb_services;

SERVICE_ID NAME                NETWORK_NAME         ENA PDB           CON_ID
---------- ------------------- -------------------- --- --------- ----------
         1 SYS$BACKGROUND                           NO  CDB$ROOT           1
         2 SYS$USERS                                NO  CDB$ROOT           1
         3 ora12c.linux.com    ora12c.linux.com     NO  CDB$ROOT           1
         6 ypdb1.linux.com     ypdb1.linux.com      NO  YPDB1              3
         1 ypdb1t              ypdb1t               NO  YPDB1              3
         1 ypdb2t              ypdb2t               NO  YPDB2              4
         2 ypdb2.linux.com     ypdb2.linux.com      NO  YPDB2              4

7 rows selected.

Elapsed: 00:00:00.00
sys. ora12c>

源PDB中的service_name已经被更改指定的service_name.

(8) 检查验证数据

sys. ora12c>conn yangyuhang/yangyuhang@ypdb2
Connected.
yangyuhang. ypdb2>SELECT COUNT(*) FROM t;

  COUNT(*)
----------
     22198

Elapsed: 00:00:00.11
yangyuhang. ypdb2>

Creating a PDB by Cloning a Remote PDB

Clone远端PDB时需要注意以下几点:

1.如果PDB被Clone到的CDB的字符集不是AL32UTF8,那么源与目标字符集必须兼容。
2.源端与目标端的字节顺序必须相同.
3.连接的用户在CDB中必须拥有'CREATE PLUGGABLE DATABASE'的权限
4.源PDB不可以是关闭状态
5.如果远端CDB为shared undo,源PDB必须为READ-ONLY状态
6.如果远端CDB不是归档模式,源PDB必须为READ-ONLY状态

这里测试将win平台ORA12CW中的PDB(ORA12CWPDB)复制到linux的,目标PDB为PDB2。

(1) 查看源端与目标的字符集

col parameter for a30
col value for a30
select * from nls_database_parameters where parameter='NLS_CHARACTERSET'
or parameter='NLS_LANGUAGE' or parameter='NLS_NCHAR_CHARACTERSET';

(2) 查看源端与目标端字节顺序

col platform_name for a40
SELECT d.inst_id, t.platform_id, t.platform_name, t.endian_format, d.name FROM v$transportable_platform t, gv$database d
WHERE t.platform_name = d.platform_name;

(3) 查看源端归档模式

archive log list

(4) 查看源端undo模式

SELECT property_name, property_value
FROM database_properties
WHERE property_name='LOCAL_UNDO_ENABLED';

(5) 目标端创建dblink

CREATE DATABASE LINK ora12cw CONNECT TO system IDENTIFIED BY oracle USING 'ORA12CW';

(6) 运行CREATE PLUGGABLE DATABASE语句进行复制PDB

CREATE PLUGGABLE DATABASE pdb2 FROM ORA12CWPDB@ora12cw
  PATH_PREFIX = '/u01/app/oracle/oradata/ora12cl/pdb2/'
  FILE_NAME_CONVERT = ('D:\U01\APP\ORACLE\ORADATA\ORA12CW\ORA12CWPDB\', '/u01/app/oracle/oradata/ora12cl/pdb2/');

(7) 创建完成后目标端查看PDB2

sys. ora12cl>show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         4 PDB2                           MOUNTED
         5 PDB1                           MOUNTED
sys. ora12cl>SELECT pdb_id, pdb_name, con_uid, status, creation_scn, con_id FROM cdb_pdbs;

    PDB_ID PDB_NAME                CON_UID STATUS     CREATION_SCN     CON_ID
---------- -------------------- ---------- ---------- ------------ ----------
         2 PDB$SEED             3409233005 NORMAL          1408788          2
         4 PDB2                 2218727525 NEW             1824437          4
         5 PDB1                 1058019921 NORMAL          1535445          5

Elapsed: 00:00:00.00
sys. ora12cl>

(8) 目标查看数据文件

sys. ora12cl>alter pluggable database pdb2 open;

Pluggable database altered.

Elapsed: 00:00:14.02
sys. ora12cl>SELECT con_id, tablespace_name, file_name FROM cdb_data_files WHERE con_id=4;

CON_ID TABLESPACE_NAME FILE_NAME
------ --------------- --------------------------------------------------
     4 SYSTEM          /u01/app/oracle/oradata/ora12cl/pdb2/SYSTEM01.DBF
     4 SYSAUX          /u01/app/oracle/oradata/ora12cl/pdb2/SYSAUX01.DBF
     4 UNDOTBS1        /u01/app/oracle/oradata/ora12cl/pdb2/UNDOTBS01.DBF
     4 USERS           /u01/app/oracle/oradata/ora12cl/pdb2/USERS01.DBF

Elapsed: 00:00:00.00
sys. ora12cl>

克隆远端的PDB成功。

Creating a PDB by Cloning a Non-CDB

Clone远端的non-CDB时需要注意以下几点:

1.连接的用户必须拥有'CREATE PLUGGABLE DATABASE'的权限
2.non-CDB不可以是关闭状态
4.如果远端non-CDB不是归档模式,那么non-CDB必须为READ-ONLY状态
5.源端与目标端的字节序必须相同.
6.如果创建的PDB来自于non-CDB,那么non-CDB的版本必须在12.1.0.2之后。
7.如果创建的PDB来自于non-CDB,当PDB创建完成后使用SYSDBA权限用户登录PDB运行noncdb_to_pdb.sql脚本,这里将源端non-CDB数据库orcl12复制到ora12cl中,命名为PDB3。

(1)查看non-CDB数据库版本

SELECT * FROM v$version;

Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

(2)查看远端non-CDB数据库模式

SELECT dbid, name, open_mode, cdb FROM v$database;

      DBID NAME      OPEN_MODE            CDB
---------- --------- -------------------- ---
3544959965 ORCL12    READ WRITE           NO

(3)查看non-CDB数据库是否为归档模式

ARCHIVE LOG LIST

(4)查看源端与目标端字节顺序

col platform_name for a40
SELECT d.inst_id, t.platform_id, t.platform_name, t.endian_format, d.name FROM v$transportable_platform t, gv$database d
WHERE t.platform_name = d.platform_name;

(5)目标端创建dblink

CREATE DATABASE LINK orcl12 CONNECT TO system IDENTIFIED BY oracle USING 'ORCL12';

(6)执行CREATE PLUGGABLE DATABASE语句
CREATE PLUGGABLE DATABASE pdb3 FROM orcl12@orcl12
  PATH_PREFIX = '/u01/app/oracle/oradata/ora12cl/pdb3/'
  FILE_NAME_CONVERT = ('/u01/app/oracle/oradata/orcl12/', '/u01/app/oracle/oradata/ora12cl/pdb3/');

(7)PDB创建成功后查看PDB

sys. ora12cl>show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB3                           MOUNTED
         4 PDB2                           READ WRITE NO
         5 PDB1                           MOUNTED
sys. ora12cl>SELECT pdb_id, pdb_name, con_uid, status, creation_scn, con_id FROM cdb_pdbs;

    PDB_ID PDB_NAME           CON_UID STATUS     CREATION_SCN     CON_ID
---------- --------------- ---------- ---------- ------------ ----------
         3 PDB3            3564415452 NEW             3014651          3
         2 PDB$SEED        3409233005 NORMAL          1408788          2
         4 PDB2            2218727525 NORMAL          1824437          4
         5 PDB1            1058019921 NORMAL          1535445          5

Elapsed: 00:00:00.00

(8)运行noncdb_to_pdb.sql脚本(必须使用拥有SYSDBA权限的用户在PDB中执行)

$ORACLE_HOME/rdbms/admin/noncdb_to_pdb.sql

(9)查看数据文件

sys. ora12cl>alter pluggable database pdb3 open;

Pluggable database altered.

Elapsed: 00:00:52.88
sys. ora12cl>SELECT con_id, tablespace_name, file_name FROM cdb_data_files WHERE con_id=3;

    CON_ID TABLESPACE_NAME FILE_NAME
---------- --------------- --------------------------------------------------
         3 SYSTEM          /u01/app/oracle/oradata/ora12cl/pdb3/system01.dbf
         3 SYSAUX          /u01/app/oracle/oradata/ora12cl/pdb3/sysaux01.dbf
         3 UNDOTBS1        /u01/app/oracle/oradata/ora12cl/pdb3/undotbs01.dbf
         3 USERS           /u01/app/oracle/oradata/ora12cl/pdb3/users01.dbf

Elapsed: 00:00:00.00
sys. ora12cl>

Cloning a PDB Without Cloning Its Data

在克隆已有的PDB或者non CDB时,我们也可以将克隆语句中加入NO DATA子句,在使用NO DATA子句时,仅仅会克隆源PDB中的模型定义,并不会克隆PDB中的数据。(Oracle数据库内自动创建的schemas下的对象会被成功克隆,而用户创建的schemas下的对象仅仅会克隆对象的定义结构)。

  1. 测试克隆本地PDB时使用no data子句
    这里将WOQUPDB克隆为PDBTEST.
    (1)源PDB:
sys. woqu>show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 WOQUPDB                        READ WRITE NO
sys. woqu>COL PROPERTY_NAME FOR A30
sys. woqu>COL PROPERTY_VALUE FOR A30
sys. woqu>

(2)检查UNDO模式及归档模式

sys. woqu>SELECT property_name, property_value
  2  FROM database_properties
  3  WHERE property_name='LOCAL_UNDO_ENABLED';


PROPERTY_NAME                  PROPERTY_VALUE
------------------------------ ------------------------------
LOCAL_UNDO_ENABLED             TRUE

Elapsed: 00:00:00.04
sys. woqu>sys. woqu>archive log list
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     7
Next log sequence to archive   9
Current log sequence           9
sys. woqu>

(3)创建测试数据表t,注意这里的t表位于sys用户下:

05:51:10 sys. woqupdb>create table t as select * from dba_objects where rownum<=10;

Table created.

Elapsed: 00:00:00.10
05:52:41 sys. woqupdb>select count(*) from t;

  COUNT(*)
----------
        10

Elapsed: 00:00:00.01
05:52:50 sys. woqupdb>

表t中共有10条数据。

(4)创建测试数据表t_tab,注意这里的t表位于yangyuhang用户下:

05:51:46 yangyuhang. woqupdb>create table t_tab as select * from dba_objects where rownum<=999;

Table created.

Elapsed: 00:00:01.84
05:52:15 yangyuhang. woqupdb>select count(*) from t_tab;

  COUNT(*)
----------
       999

Elapsed: 00:00:00.01
05:52:19 yangyuhang. woqupdb>

(5)使用create pluggable database + NO DATA子句创建pdb:pdbtest:

sys. woqu>CREATE PLUGGABLE DATABASE pdbtest FROM woqupdb NO DATA
  2  PATH_PREFIX = '/u01/app/oracle/oradata/woqu/pdbtest'
  3  FILE_NAME_CONVERT = ('/u01/app/oracle/oradata/woqu/woqupdb', '/u01/app/oracle/oradata/woqu/pdbtest');

Pluggable database created.

Elapsed: 00:00:09.35
sys. woqu>

(6)检查克隆完成的pdbtest:

sys. woqu>show pdbs

    CON_ID CON_NAME    OPEN MODE  RESTRICTED
---------- ----------- ---------- ----------
         2 PDB$SEED    READ ONLY  NO
         3 WOQUPDB     READ WRITE NO
         5 PDBTEST     MOUNTED
sys. woqu>SELECT pdb_id, pdb_name, con_uid, status, creation_scn, con_id FROM cdb_pdbs;

    PDB_ID PDB_NAME       CON_UID STATUS     CREATION_SCN     CON_ID
---------- ----------- ---------- ---------- ------------ ----------
         3 WOQUPDB     1167267009 NORMAL          1443334          3
         2 PDB$SEED     399989944 NORMAL          1408751          2
         5 PDBTEST     3356573055 NEW             1956581          5

Elapsed: 00:00:00.05
sys. woqu>alter pluggable database pdbtest open;

Pluggable database altered.

Elapsed: 00:00:07.09
sys. woqu>show pdbs

    CON_ID CON_NAME    OPEN MODE  RESTRICTED
---------- ----------- ---------- ----------
         2 PDB$SEED    READ ONLY  NO
         3 WOQUPDB     READ WRITE NO
         5 PDBTEST     READ WRITE NO
sys. woqu>SELECT pdb_id, pdb_name, con_uid, status, creation_scn, con_id FROM cdb_pdbs;

    PDB_ID PDB_NAME     CON_UID STATUS CREATION_SCN     CON_ID
---------- --------- ---------- ------ ------------ ----------
         3 WOQUPDB   1167267009 NORMAL      1443334          3
         2 PDB$SEED   399989944 NORMAL      1408751          2
         5 PDBTEST   3356573055 NORMAL      1956581          5

Elapsed: 00:00:00.02
sys. woqu>

(7)将从测试数据

sys. woqu>alter session set container=pdbtest;

Session altered.

Elapsed: 00:00:00.07
sys. woqu>show user con_id
USER is "SYS"

CON_ID
------------------------------
5
sys. woqu>select count(*) from t;

  COUNT(*)
----------
        10

Elapsed: 00:00:00.00
sys. woqu>select count(*) from yangyuhang.t_tab;

  COUNT(*)
----------
         0

Elapsed: 00:00:00.02
sys. woqu>

通过测试可以看到sys下的对象中的数据依然克隆成功,而自建用户下的对象中数据并没有跟着一个克隆。

2.测试克隆远端PDB时使用no data子句
这里将远端PDB:PDBTEST克隆到本地,命名为ORA12CPDB2:

(1)创建DBLINK

sys. ora12c>CREATE DATABASE LINK woqu CONNECT TO system IDENTIFIED BY oracle USING 'WOQU';

Database link created.

Elapsed: 00:00:00.15
sys. ora12c>

(2)克隆PDBTEST

sys. ora12c>CREATE PLUGGABLE DATABASE ORA12CPDB2 FROM PDBTEST@WOQU NO DATA
  2    PATH_PREFIX = '/u01/app/oracle/oradata/ora12c/ora12cpdb2'
  3    FILE_NAME_CONVERT = ('/u01/app/oracle/oradata/woqu/pdbtest', '/u01/app/oracle/oradata/ora12c/ora12cpdb2');

Pluggable database created.

Elapsed: 00:00:10.44
sys. ora12c>

(3)检查克隆完成后的ORA12CPDB2

sys. ora12c>show pdbs

    CON_ID CON_NAME       OPEN MODE  RESTRICTED
---------- -------------- ---------- ----------
         2 PDB$SEED       READ ONLY  NO
         3 ORA12CPDB      READ WRITE NO
         5 ORA12CPDB2     MOUNTED
sys. ora12c>SELECT pdb_id, pdb_name, con_uid, status, creation_scn, con_id FROM cdb_pdbs;

    PDB_ID PDB_NAME         CON_UID STATUS     CREATION_SCN     CON_ID
---------- ------------- ---------- ---------- ------------ ----------
         3 ORA12CPDB     1788418398 NORMAL          1442734          3
         2 PDB$SEED      2375660446 NORMAL          1408749          2
         5 ORA12CPDB2    2003741801 NEW             1959473          5

Elapsed: 00:00:00.01
sys. ora12c>

(4)验证数据

sys. ora12c>alter pluggable database ora12cpdb2 open;

Pluggable database altered.

Elapsed: 00:00:05.24
sys. ora12c>alter session set container=ora12cpdb2;

Session altered.

Elapsed: 00:00:00.04
sys. ora12c>select count(*) from t;

  COUNT(*)
----------
        10

Elapsed: 00:00:00.01
sys. ora12c>select count(*) from yangyuhang.t_tab;

  COUNT(*)
----------
         0

Elapsed: 00:00:00.00
sys. ora12c>

发表评论

电子邮件地址不会被公开。 必填项已用*标注