Oracle 12c系列(七)| Non-CDB转换为PDB

当我们需要将Non-CDB数据库类型更改为PDB数据库类型时,除了Cloning的方式外我们还可以使用DBMS_PDB包来生成Non-CDB数据库的XML元数据文件。

作者 杨禹航·沃趣科技高级数据库技术专家
出品 沃趣科技

当我们需要将Non-CDB数据库类型更改为PDB数据库类型时,可以使用Cloning的方式将其复制到现有的CDB中,但是该方法需要将Non-CDB中的数据文件复制到新的目录中,除了Cloning的方式外我们还可以使用DBMS_PDB包来生成Non-CDB数据库的XML元数据文件,该XML元数据文件中描述了Non-CDB中的数据文件信息,可以使用XML文件将Non-CDB数据库附加为CDB中的PDB,通过该方式将Non-CDB数据库转换成CDB中的PDB,它的优点在于省去了复制Non-CDB数据文件的过程,但要求Non-CDB必须为12.1.0之上的版本,如果Non-CDB为12c之前的版本,需要将其升级到12c,另外需要我们提前创建一个CDB容器数据库,或者现有环境中已存在CDB容器数据库(将Non-CDB插入已存在的CDB中)。

使用DBMS_PDB将Non-CDB转换为PDB的流程如下:

  1. 确保Non-CDB处于只读状态。

  2. 使用DBMS_PDB.DESCRIBE创建描述Non-CDB的XML元数据文件。

  3. 使用DBMS_PDB.CHECK_PLUG_COMPATIBILITY检查Non-CDB与目标CDB是否兼容。

  4. 关闭源Non-CDB。

  5. 使用描述Non-CDB的XML文件将Non-CDB插入CDB中。

  6. 执行$ORACLE_HOME/rdbms/admin/noncdb_to_pdb.sql。

  7. 以读写模式打开新的PDB。

如下示例将Non-CDB:orcl转换成CDB:ora12c中的pdb:orclpdb,详细过程如下:

原数据库:

sys. orcl>select * from v$version;
BANNER                                                                               CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production              0
PL/SQL Release 12.2.0.1.0 - Production                                                    0
CORE    12.2.0.1.0      Production                                                                0
TNS for Linux: Version 12.2.0.1.0 - Production                                            0
NLSRTL Version 12.2.0.1.0 - Production                                                    0
Elapsed: 00:00:00.00
sys. orcl>select name, dbid, cdb from v$database;
NAME                                 DBID CDB
------------------------------ ---------- ---
ORCL                           1503851221 NO
Elapsed: 00:00:00.01
sys. orcl>

目标数据库:

sys. ora12c>select * from v$version;
BANNER                                                                               CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production              0
PL/SQL Release 12.2.0.1.0 - Production                                                    0
CORE    12.2.0.1.0      Production                                                                0
TNS for Linux: Version 12.2.0.1.0 - Production                                            0
NLSRTL Version 12.2.0.1.0 - Production                                                    0
Elapsed: 00:00:00.01
sys. ora12c>select name, dbid, cdb from v$database;
NAME                                 DBID CDB
------------------------------ ---------- ---
ORA12C                          345654762 YES
Elapsed: 00:00:00.01
sys. ora12c>show pdbs
CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
     2 PDB$SEED                       READ ONLY  NO
     4 QDATAPDB                       MOUNTED
sys. ora12c>

1.将Non-CDB:orcl置于只读模式。

03:09:33 sys. orcl>shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
03:09:57 sys. orcl>startup mount
ORACLE instance started.
Total System Global Area  947912704 bytes
Fixed Size                  8627488 bytes
Variable Size             348130016 bytes
Database Buffers          587202560 bytes
Redo Buffers                3952640 bytes
Database mounted.
03:10:08 sys. orcl>alter database open read only;
Database altered.
Elapsed: 00:00:00.90
03:12:58 sys. orcl>

2.连接到Non-CDB并使用存储过程dbms_pdb.describe来创建描述Non-CDB的XML文件。

sys. orcl>exec dbms_pdb.describe(pdb_descr_file=>'/home/oracle/tmp/orcl.xml');
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.47
sys. orcl>

3.使用dbms_pdb.check_plug_compatibility检查Non-CDB与目标CDB是否兼容:

#pdb_name:指定CDB中新的PDB的名字。

set serverout on
declare
compatible constant varchar2(3) := case
   dbms_pdb.check_plug_compatibility(
    pdb_descr_file => '/home/oracle/tmp/orcl.xml',
    pdb_name       => 'orclpdb')
    when true then 'yes' else 'no' end;
begin
dbms_output.put_line(compatible);
end;
/
03:14:53 sys. ora12c>set serverout on
03:15:02 sys. ora12c>declare
03:15:03   2    compatible constant varchar2(3) := case
03:15:03   3         dbms_pdb.check_plug_compatibility(
03:15:03   4          pdb_descr_file => '/home/oracle/tmp/orcl.xml',
03:15:03   5          pdb_name       => 'orclpdb')
03:15:03   6          when true then 'yes' else 'no' end;
03:15:03   7  begin
03:15:03   8    dbms_output.put_line(compatible);
03:15:03   9  end;
03:15:03  10  /
yes
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.05
03:15:03 sys. ora12c>

4.关闭源库Non-CDB:orcl

03:15:40 sys. orcl>shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
03:15:59 sys. orcl>

5.将Non-CDB:orcl插入到CDB:ora12c中,且命名为orclpdb。

03:16:29 sys. ora12c>show pdbs
CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
     2 PDB$SEED                       READ ONLY  NO
     4 QDATAPDB                       MOUNTED
sys. ora12c>create pluggable database orclpdb using '/home/oracle/tmp/orcl.xml' nocopy tempfile reuse;
Pluggable database created.
Elapsed: 00:00:01.36
03:19:16 sys. ora12c>
03:19:33 sys. ora12c>show pdbs
CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
     2 PDB$SEED                       READ ONLY  NO
     4 QDATAPDB                       MOUNTED
     5 ORCLPDB                        MOUNTED
03:20:09 sys. ora12c>select pdb_id, pdb_name, dbid, status, con_id from cdb_pdbs;
PDB_ID PDB_NAME                   DBID STATUS         CON_ID
---------- -------------------- ---------- ---------- ----------
     2 PDB$SEED             1587318379 NORMAL              2
     4 QDATAPDB             2726866135 NORMAL              4
     5 ORCLPDB              1503851221 NEW                 5
Elapsed: 00:00:00.00
03:20:10 sys. ora12c>

6.执行
$ORACLE_HOME/rdbms/admin/noncdb_to_pdb.sql脚本,这个脚本需要在打开新的PDB:orclpdb之前执行。

03:21:28 sys. ora12c>alter session set container=orclpdb;
Session altered.
Elapsed: 00:00:00.00
03:21:36 sys. ora12c>@$ORACLE_HOME/rdbms/admin/noncdb_to_pdb.sql
…
…

7.以读写模式打开新PDB

03:50:20 sys. ora12c>show pdbs
CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
     2 PDB$SEED                       READ ONLY  NO
     4 QDATAPDB                       MOUNTED
     5 ORCLPDB                        MOUNTED
03:50:21 sys. ora12c>alter pluggable database orclpdb open;
Pluggable database altered.
Elapsed: 00:00:11.04
03:51:48 sys. ora12c>show pdbs
CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
     2 PDB$SEED                       READ ONLY  NO
     4 QDATAPDB                       MOUNTED
     5 ORCLPDB                        READ WRITE NO
03:55:17 sys. ora12c>alter session set container=orclpdb;
Session altered.
Elapsed: 00:00:00.03
03:55:24 sys. ora12c>create table t as select * from dba_objects;
Table created.
Elapsed: 00:00:01.89
03:55:34 sys. ora12c>

Non-CDB:ORCL成功转换为容器数据库ORA12C中的PDB,且转换过程中未经过数据copy的过程。


| 作者简介

杨禹航·沃趣科技高级数据库技术专家

熟悉Oracle数据库内部机制,丰富的数据库及RAC集群层故障诊断、性能调优、OWI、数据库备份恢复及迁移经验。


发表评论

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