使用DBMS_PDB将non-CDB迁移到PDB中

1  说明

由于从10g,11g直接升级到12c时候,是无法直接升级到CDB的而是non-CDB,所以要想使用CDB,必须进行转换。Non-CDB本身是无法直接转换成CDB-PDB模式,只能通过迁移的方式将non-cdb迁移到一个已存在的CDB数据库上的PDB中。

2  使用DBMS_PDB包

如果CDB不存在,那么就首先创建CDB

2.1   创建CDB

首先使用dbca或CREATE DATABASE(不建议)创建一个CDB

使用DBMS_PDB将non-CDB迁移到PDB中_第1张图片使用DBMS_PDB将non-CDB迁移到PDB中_第2张图片使用DBMS_PDB将non-CDB迁移到PDB中_第3张图片使用DBMS_PDB将non-CDB迁移到PDB中_第4张图片 

安装CDB完毕!

2.1.1  查看安装是否正确

 

[[email protected] ~]$ sqlplus [email protected] as sysdba

SQL*Plus: Release 12.1.0.2.0 Production on Thu Jul 27 22:02:09 2017

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Connected to:

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

With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options


SQL> show pdbs;

 
 CON_ID CON_NAME OPEN MODE  RESTRICTED

---------- ------------------------------ ---------- ----------

 2 PDB$SEED	  READ ONLY  NO

 3 PDB1   READ WRITE NO

 

下图:使用DBMS_PDB.DESCRIBE存储过程插入non-CDB

官方文档:http://docs.oracle.com/database/121/ADMIN/cdb_plug.htm#ADMIN13598

使用DBMS_PDB将non-CDB迁移到PDB中_第5张图片

 

使用DBMS_PDB包是为了将从non-cdb生成XML元数据文件,文件里有详细的步骤:如果拔PDB数据库。将非CDB插入到一个CDB的PDB中。

 

2.2   以read-only打开non-CDB

 

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

 --以read only重新打开数据库

SQL> startup open read only

ORACLE instance started.


Total System Global Area  624951296 bytes

Fixed Size                  2927480 bytes

Variable Size             276825224 bytes

Database Buffers          339738624 bytes

Redo Buffers                5459968 bytes

Database mounted.

Database opened.

 

2.3   运行DBMS_PDB.DESCRIBE生成XML文件

 

SQL> BEGIN

  DBMS_PDB.DESCRIBE(

    pdb_descr_file => '/software/cndba.xml');

END;

/  2    3    4    5  


PL/SQL procedure successfully completed.

 

 

[[email protected] software]$ ll cndba.xml

-rw-r--r-- 1 oracle oinstall 5668 Jul 27 20:46 cndba.xml

 

2.4   检查兼容性

使用DBMS_PDB.CHECK_PLUG_COMPATIBILITY函数检查non-CDB和CDB的兼容性

 

SQL> SET SERVEROUTPUT ON

DECLARE

  compatible CONSTANT VARCHAR2(3) :=

    CASE DBMS_PDB.CHECK_PLUG_COMPATIBILITY(

           pdb_descr_file => '/software/cndba.xml',

           pdb_name       => 'cndba_pdb')

    WHEN TRUE THEN 'YES'

    ELSE 'NO'

END;

BEGIN

  DBMS_OUTPUT.PUT_LINE(compatible);

END;

/SQL>   2    3    4    5    6    7    8    9   10   11   12  

YES

PL/SQL procedure successfully completed.

 

可以看到输出YES,表示相兼容。

2.5   关闭non-

CDB数据库

 

SQL> shutdown immediate

Database closed.

Database dismounted.

ORACLE instance shut down.

 

2.6   插入non-CDB

 

SQL> CREATE PLUGGABLE DATABASE cndba_pdb USING '/software/cndba.xml' --上面生成的XML文件

  COPY

  FILE_NAME_CONVERT = ('/u01/app/oracle/oradata/cndba', '/u01/app/oracle/oradata/cndba_pdb/')

  USER_TABLESPACES=('USERS');  2    3    4  



Pluggable database created.

 

USER_TABLESPACES子句使您可以将非cdb中多个租户使用的数据分离为不同的PDBs。

可以使用多个CREATE PLUGGABLE数据库语句与此子句创建其他PDBs,其中包括在非cdb中存在的其他表空间中的数据。

2.7   执行脚本

部分结果

 

SQL> @?/rdbms/admin/noncdb_to_pdb.sql

SQL> @$ORACLE_HOME/rdbms/admin/noncdb_to_pdb.sql

SQL> SET SERVEROUTPUT ON

SQL> SET FEEDBACK 1

SQL> SET NUMWIDTH 10

SQL> SET LINESIZE 80

SQL> SET TRIMSPOOL ON

SQL> SET TAB OFF

SQL> SET PAGESIZE 100

SQL>

SQL> WHENEVER SQLERROR EXIT;

SQL>

SQL> DOC

DOC>#######################################################################

DOC>#######################################################################

DOC>   The following statement will cause an "ORA-01403: no data found"

DOC>   error if we're not in a PDB.

DOC>   This script is intended to be run right after plugin of a PDB,

DOC>   while inside the PDB.

DOC>#######################################################################

DOC>#######################################################################

DOC>#

SQL>

SQL> VARIABLE cdbname VARCHAR2(128)

SQL> VARIABLE pdbname VARCHAR2(128)

SQL> BEGIN

  2    SELECT sys_context('USERENV', 'CDB_NAME')

  3      INTO :cdbname

  4      FROM dual

  5      WHERE sys_context('USERENV', 'CDB_NAME') is not null;

  6    SELECT sys_context('USERENV', 'CON_NAME')

  7      INTO :pdbname

  8      FROM dual

  9      WHERE sys_context('USERENV', 'CON_NAME') <> 'CDB$ROOT';

 10  END;

 11  /

 

PL/SQL procedure successfully completed.

 

SQL>

SQL> COLUMN pdbname NEW_VALUE pdbname

SQL> COLUMN pdbid NEW_VALUE pdbid

SQL>

SQL> select :pdbname pdbname from dual;

 

PDBNAME

............



SQL> alter session set container = "&pdbname";

 

Session altered.

 

SQL>

SQL> -- leave the PDB in the same state it was when we started

SQL> BEGIN

  2    execute immediate '&open_sql &restricted_state';

  3  EXCEPTION

  4    WHEN OTHERS THEN

  5    BEGIN

  6      IF (sqlcode <> -900) THEN

  7        RAISE;

  8      END IF;

  9    END;

 10  END;

 11  /

 

PL/SQL procedure successfully completed.

 

SQL>

SQL> WHENEVER SQLERROR CONTINUE;

SQL>

 

2.8   验证

目标库中PDB(cndba_pdb):

 

SQL> show pdbs;

 

    CON_ID CON_NAME	  OPEN MODE  RESTRICTED

---------- ------------------------------ ---------- ----------

 2 PDB$SEED	  READ ONLY  NO

 3 PDB1   READ WRITE NO

 4 CNDBA_PDB	  MOUNTED

SQL> alter pluggable database cndba_pdb open;

SQL> SHOW PDBS;

 

    CON_ID CON_NAME	  OPEN MODE  RESTRICTED

---------- ------------------------------ ---------- ----------

 2 PDB$SEED	  READ ONLY  NO

 3 PDB1   READ WRITE NO

 4 CNDBA_PDB READ WRITE YES

SQL> alter session set container=cndba_pdb;

Session altered.

SQL> select count(*) from cndba.test;

 

  COUNT(*)

----------

      1545

 

源库(non-CDB):

 

SQL> select count(*) from cndba.test;


  COUNT(*)

----------

      1545

 

成功将non-CDB迁移到PDB中,可以通过DBCA将non-CDB删掉了。

你可能感兴趣的:(Oracle,升级,迁移)