Showing posts with label 12c. Show all posts
Showing posts with label 12c. Show all posts

24 September 2015

step by step to migrate Non Container Database (Non CDB) to Container Database (CDB, as PDB)

Moving a Non Container Database (Non CDB) to Container Database (CDB, as PDB)

In this post, I discuss moving a regular Non CDB Database to CDB architecture. DBMS_PDB.DESCRIBE metadata procedure is used for the same purpose. Following analogy applies to the post,

ORCL - Non CDB Database (Instance)

Step -1 Identify CDB or non CDB database

[oracle@r12c orcl]$ sqlplus "/as sysdba"

SQL*Plus: Release 12.1.0.2.0 Production on Thu Sep 10 18:49:34 2015

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> SELECT name, open_mode FROM   v$pdbs ORDER BY name;

no rows selected

SQL> select con_id, dbid, name from v$pdbs;

no rows selected

SQL> select cdb from v$database;

CDB
---
NO

Step -2 Creat CDB database

Below steps to create CDB Database (Instance)
$ ./runinstaller

 

 



Step -3

Open the Non CDB (ORCL) in READ-ONLY Mode to keep it transactionally consistent.

SQL> STARTUP OPEN READ ONLY;
ORACLE instance started.

Total System Global Area 1275068416 bytes
Fixed Size                  2923968 bytes
Variable Size             872415808 bytes
Database Buffers          385875968 bytes
Redo Buffers               13852672 bytes
Database mounted.
Database opened.

Step -4
Connect to the Non-CDB Database(ORCL) and execute DBMS_PDB.DESCRIBE procedure to generate an xml file that will help to build the Pluggable Database on the CDB Database(CDB)

SQL> BEGIN
  DBMS_PDB.DESCRIBE(
    pdb_descr_file => '/tmp/cdb.xml');
END;
/  2    3    4    5

PL/SQL procedure successfully completed.

Step -5
Shutdown the Non CDB (ORCL) Database, and login to CDB Database (CDB) as SYS user

SQL> SHUTDOWN IMMEDIATE;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> exit
Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

Step -6
On CDB container database  Create the PDB Database by mapping the XML file that Is generated in step -4

[oracle@r12c datafile]$ export ORACLE_SID=cdb1
[oracle@r12c datafile]$ sqlplus "/as sysdba"

SQL*Plus: Release 12.1.0.2.0 Production on Thu Sep 10 20:14:23 2015

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> select cdb from v$database;

CDB
---
YES

SQL> select name from v$database;

NAME
---------
CDB1

SQL> SELECT pdb_name, status FROM   dba_pdbs ORDER BY pdb_name;

PDB_NAME        STATUS
-----------       ----------
PDB             NORMAL
PDB$SEED        NORMAL

SQL> CREATE PLUGGABLE DATABASE orcl USING '/tmp/cdb.xml'
  COPY
  FILE_NAME_CONVERT = ('/db/app/product/oradata/orcl/','/db/app/product/oradata/orcl/pdb1/');  2    3

Pluggable database created.

SQL> SELECT NAME,OPEN_MODE FROM V$PDBS;

NAME                           OPEN_MODE
------------------------------ ----------
PDB$SEED                       READ ONLY
PDB                            MOUNTED
ORCL                           MOUNTED

Step -7
Connect to the new PDB that has been created on CDB Database (CONDB) as SYS, and execute the "noncdb_to_pdb.sql" script.

SQL> ALTER SESSION SET CONTAINER=ORCL;

Session altered.

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> 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>
SQL> WHENEVER SQLERROR CONTINUE;
SQL> commit;

Commit complete.

SQL> exit
Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
[oracle@r12c datafile]$

Step -8
Open the PDB in READ WRITE Mode, confirm the status, and make it available for Client Operations,  it is also advisable to take a backup.

[oracle@r12c datafile]$ sqlplus "/as sysdba"

SQL*Plus: Release 12.1.0.2.0 Production on Thu Sep 10 20:46:02 2015

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> SELECT NAME,OPEN_MODE FROM V$PDBS;

NAME                           OPEN_MODE
------------------------------ ----------
PDB$SEED                       READ ONLY
PDB                            MOUNTED
ORCL                           MOUNTED

SQL> ALTER PLUGGABLE DATABASE ORCL open;

Pluggable database altered.

SQL> SELECT NAME,OPEN_MODE FROM V$PDBS;

NAME                           OPEN_MODE
------------------------------ ----------
PDB$SEED                       READ ONLY
PDB                            MOUNTED
ORCL                           READ WRITE

11 September 2015

step by step Clone a Pluggable Database (PDB) Manually

PDB Clones in Oracle Database 12c Release 1 (12.1.0.2)

[oracle@r12c ~]$ sqlplus "/as sysdba"

SQL*Plus: Release 12.1.0.2.0 Production on Fri Sep 11 11:33:26 2015

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 con_name;

CON_NAME
------------------------------
CDB$ROOT
SQL> select con_id, name, open_mode, total_size from v$pdbs;

    CON_ID NAME                           OPEN_MODE  TOTAL_SIZE
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY   838860800
         4 ORCL                           READ WRITE 2945187840

SQL> alter session set container=ORCL;

Session altered.

Creating the user for testing purpose.

SQL> create user muthu identified by muthu;

User created.

SQL> SELECT NAME,OPEN_MODE FROM V$PDBS;

NAME                           OPEN_MODE
------------------------------ ----------
ORCL                           READ WRITE

SQL> alter pluggable database ORCL close;

Pluggable database altered.

SQL> alter pluggable database ORCL open read only;

Pluggable database altered.

Cloning full database from ORCL to ORCLNEW.

SQL> CREATE PLUGGABLE DATABASE orclnew from orcl FILE_NAME_CONVERT = ('/db/app/product/oradata/orcl/pdb1','/db/app/product/oradata/orclnew/');

Pluggable database created.

SQL> alter pluggable database orclnew open read write;

Pluggable database altered.

SQL> alter pluggable database orcl close;

Pluggable database altered.

SQL> alter pluggable database orcl open read write;

Pluggable database altered.

SQL> SELECT NAME,OPEN_MODE FROM V$PDBS;

NAME                           OPEN_MODE
------------------------------ ----------
PDB$SEED                       READ ONLY
ORCLNEW                        READ WRITE
ORCL                           READ WRITE

Checking the contents of the muthu user in the new PDB show the user is present,

SQL> alter session set container=ORCLNEW;

Session altered.
SQL> col USERNAME format a20
SQL> select USERNAME,CREATED from DBA_users where USERNAME='MUTHU';

USERNAME             CREATED
-------------------- ---------
MUTHU                11-SEP-15

SQL> show con_name;

CON_NAME
------------------------------
ORCLNEW
SQL> alter session set container=ORCL;

Session altered.

SQL> select USERNAME,CREATED from DBA_users where USERNAME='MUTHU';

USERNAME             CREATED
-------------------- ---------
MUTHU                11-SEP-15

SQL> show con_name;

CON_NAME
------------------------------
ORCL
SQL>

step by step to connect Container database and Pluggable database

How to connect Container database and Pluggable database

Container database (CDB1)
Pluggable database (ORCL)

[oracle@r12c ~]$ lsnrctl services

LSNRCTL for Linux: Version 12.1.0.2.0 - Production on 11-SEP-2015 11:20:03

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))
Services Summary...
Service "cdb1.muthu.com" has 1 instance(s).
  Instance "cdb1", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:0 refused:0 state:ready
         LOCAL SERVER
Service "cdb1XDB.muthu.com" has 1 instance(s).
  Instance "cdb1", status READY, has 1 handler(s) for this service...
    Handler(s):
      "D000" established:0 refused:0 current:0 max:1022 state:ready
         DISPATCHER <machine: r12c.muthu.com, pid: 13621>
         (ADDRESS=(PROTOCOL=tcp)(HOST=r12c.muthu.com)(PORT=60683))
Service "orcl.muthu.com" has 1 instance(s).
  Instance "cdb1", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:0 refused:0 state:ready
         LOCAL SERVER
The command completed successfully
[oracle@r12c ~]$ tnsping orcl

TNS Ping Utility for Linux: Version 12.1.0.2.0 - Production on 11-SEP-2015 11:20:25

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

Used parameter files:
/db/app/product/12.1.0/dbhome_1/network/admin/sqlnet.ora


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl.muthu.com)))
OK (0 msec)
[oracle@r12c ~]$ sqlplus system/manager@orcl  --  Pluggable database

SQL*Plus: Release 12.1.0.2.0 Production on Fri Sep 11 11:20:39 2015

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> select con_id, name, open_mode, total_size from v$pdbs;

    CON_ID NAME                           OPEN_MODE  TOTAL_SIZE
---------- ------------------------------ ---------- ----------
         4 ORCL                           READ WRITE 2945187840

SQL> show con_name;

CON_NAME
------------------------------
ORCL
SQL> exit
Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
[oracle@r12c ~]$ sqlplus system/manager@cdb1 --  Container database

SQL*Plus: Release 12.1.0.2.0 Production on Fri Sep 11 11:22:17 2015

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

Last Successful login time: Fri Sep 11 2015 11:20:40 +05:30

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 con_name;

CON_NAME
------------------------------
CDB$ROOT
SQL>

Keep learning.......

step by step to identify PDB or CDB

How to identity am i connected PDB or CDB?

In Oracle Database 12c, we can still find this information from V$INSTANCE.  Breath a sigh of relief, for the moment!  What if I connect to a container database and then issue an ALTER SESSION command to move into a different container, i.e. as PDB?  Will I get the name of the PDB that I move into from the V$INSTANCE view or do I need to look somewhere else?  Lets take a look.

Connect to the container database as usual and use the V$INSTANCE view to see where I’m at.

[oracle@r12c ~]$ sqlplus "/as sysdba"

SQL*Plus: Release 12.1.0.2.0 Production on Fri Sep 11 10:52:33 2015

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> select instance_name, version, status, con_id from v$instance;

INSTANCE_NAME    VERSION           STATUS           CON_ID
---------------- ----------------- ------------ ----------
cdb1             12.1.0.2.0        OPEN                  0

Lets move into a PDB.  I have created the ORCL.

SQL>alter session set container=ORCL;

Session altered.

Did I actually move containers?  How can I tell?  Instead of selecting INSTANCE_NAME from V$INSTANCE; we have two new SHOW commands that will provide us the information we are looking for.  These command are quite simple (SHOW CON_ID & SHOW CON_NAME).

SQL> show con_id;

CON_ID
------------------------------
4
SQL> show con_name

CON_NAME
------------------------------
ORCL

This view provides similar information as V$INSTANCE does; yet is only specific to PDBs.  If we wanted to find out the container_id, name, open_mode and size of the PDB we are currently connected to, we can use this query:


SQL> select con_id, name, open_mode, total_size from v$pdbs;

    CON_ID NAME                           OPEN_MODE  TOTAL_SIZE
---------- ------------------------------ ---------- ----------
         4 ORCL                           READ WRITE 2945187840

SQL>

What is interesting to point out, is that since we used an ALTER SESSION statement to switch to PDB1, the V$PDBS view only lists the PDB we are currently working in.  If we want to get a complete list of PDBs within the container database, we need to go back to the root container database and run the same SQL statement.

SQL> alter session set container=CDB$ROOT;

Session altered.

SQL> show con_name;

CON_NAME
------------------------------
CDB$ROOT
SQL> select con_id, name, open_mode, total_size from v$pdbs;

    CON_ID NAME                           OPEN_MODE  TOTAL_SIZE
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY   838860800
         4 ORCL                           READ WRITE 2945187840

SQL>