Managing Oracle 12c CDB’s and PDB’s – Cloning PDB’s – Part 3

Cloning PDB’s

Cloning a database used to be a difficult, if not a hectic process. Before 12c the most efficient method to create the clone required using RMAN. However even this method has quite a number of steps inlvolved. In 12c too, the effort required to clone the CDB database is almost same as before but not for PDBs. Cloning PDB is as easy as executing three to four simple commands. As PDBs will have the actual user data in them so this will be a ground breaking feature in multiple deployments scenarios.


In this article we will look at how to clone a PDB within the same the CDB container. Within the same CDB, I mean that both the target and the source PDBs will be in the same CDB. But this process can be used if you want to clone it to a different CDB with a very minor modification.

Preparing for the Clone process

The first step is to prepare the environment for cloning. This includes opening the source PDB in the read-only mode and also creating the directories where the target Database files will be placed. We have CDB12c as the CDB container database and the PDB1 as the source PDB database for cloning. The new target database will be called PDB1_CLONE.


Log into the CDB and execute the following command.

SQL> select name,open_mode from v$pdbs;
NAME OPEN_MODE
------------------------------ ----------
PDB$SEED READ ONLY
PDB1 READ WRITE

SQL> alter pluggable database pdb1 close immediate;
Pluggable database altered.

SQL> alter pluggable database pdb1 open read only;
Pluggable database altered.

SQL> select name,open_mode from v$pdbs;
NAME OPEN_MODE
------------------------------ ----------
PDB$SEED READ ONLY
PDB1 READ ONLY


Next we will create the directory where data files will be stored.

$ mkdir -p /u02/app/oracle/oradata/cdb12c/pdb1_clone After that we set the directory as the default file creation location for the entire instance. This is required so that files are created where we want them to be.

SQL> alter system set db_create_file_dest='/u02/app/oracle/oradata/cdb12c/pdb1_clone';
System altered.

Cloning the PDB

Once we have the environment ready, we can now start the clone process. The clone process in itself is just one command which goes something like below.

SQL> create pluggable database pdb1_clone from pdb1;
Pluggable database created.


That’s it. The PDB has been cloned. Depending on the size of database the above command may take sometime. If your source PDB is in different CDB then your target then you need a database link from target to source. The above command will work with just an addition of DB link reference at the end.


You can now open the source and target databases and confirm that cloning has been completed successfully.

SQL> alter pluggable database pdb1_clone open;
Pluggable database altered.

SQL> alter pluggable database pdb1 close immediate;
Pluggable database altered.

SQL> alter pluggable database pdb1 open;
Pluggable database altered.

SQL> select name,open_mode from v$pdbs;
NAME OPEN_MODE
------------------------------ ----------
PDB$SEED READ ONLY
PDB1 READ WRITE
PDB1_CLONE READ WRITE


As you can see both the source and target databases are up and running. You can now log into your newly created database. The easiest way if you are connected to CDB as SYS is to change the Container like below.

SQL> alter session set container=pdb1_clone;
Session altered.

SQL> select file_name from dba_data_files;
FILE_NAME
----------------------------------------------------------------------------------------------------
/u02/app/oracle/oradata/cdb12c/pdb1_clone/CDB12c/
E070CD4A69AC0893E0450000FF00020F/datafile/o1_mf_system_8x2tx8l9_.dbf

/u02/app/oracle/oradata/cdb12c/pdb1_clone/CDB12c/
E070CD4A69AC0893E0450000FF00020F/datafile/o1_mf_sysaux_8x2tx8m5_.dbf


As SYS is common user and has presence in almost every PDB created so you can easily switch between different Containers. In Oracle Database 12c every database whether it is CDB or PDB, both are considered a Container.


You can switch back to the CDB container just like that. The result of same SELECT statement will be different and will confirm that you are now connected to a different database.

SQL> alter session set container=CDB$ROOT;
Session altered.

SQL> select file_name from dba_data_files;
FILE_NAME
----------------------------------------------------------------------------------------------------
/u01/app/oracle/oradata/cdb12c/system01.dbf
/u01/app/oracle/oradata/cdb12c/sysaux01.dbf
/u01/app/oracle/oradata/cdb12c/undotbs01.dbf
/u01/app/oracle/oradata/cdb12c/users01.dbf
/u01/app/oracle/oradata/cdb12c/cdata.dbf

Migrating Non-CDB database as PDB via Cloning

You can migrate any non-cdb database created either in 11.2.0.3 or 12.1 as a PDB into your Container database. If your target database is running in 11g then you will first have to upgrade it to 12.1. Once upgraded you can then follow the process outlined here to migrate your non-cdb database as PDB into your main Container database.


Suppose we have a non-cdb database running in 12.1 named ORCL. The first step to migrate it as PDB would be to generate manifest file. Shutdown the database and then restart it in Read-Only mode.

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

SQL> startup mount
ORACLE instance started.

Total System Global Area 626327552 bytes
Fixed Size 2291472 bytes
Variable Size 276826352 bytes
Database Buffers 343932928 bytes
Redo Buffers 3276800 bytes
Database mounted.
SQL> alter database open read only;

Database altered.


Once the database has been started in Read-Only mode, run the following procedure to generate manifest file. This file will be used to create clone of database as PDB into our CDB.

SQL> exec dbms_pdb.describe (pdb_descr_file=>'/u01/app/oracle/noncdb_orcl.xml');
PL/SQL procedure successfully completed.


Shutdown the database so that there is no data inconsistency while cloning.

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.


Now log into your CDB.
sqlplus sys/oracle as sysdba

SQL*Plus: Release 12.1.0.1.0 Production on Fri Jul 26 17:12:03 2013
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT

SQL> select name,open_mode from v$pdbs;
NAME OPEN_MODE
------------------------------ ----------
PDB$SEED READ ONLY
PDB12C READ WRITE


As you can see that apart from Seed database currently we have only one PDB.


As our ORCL database was using Oracle managed files so we will have to set the db_create_file_dest parameter to make sure that files are copied where we want them to be.

SQL> alter system set db_create_file_dest='/u02/app/oracle/oradata/cdb12c/noncdb_orcl';
System altered.


The name of our database would be noncdb_orcl as you might have guessed. We are now ready to finally clone the non-cdb database as PDB into our CDB.

SQL> create pluggable database noncdb_orcl
2 using '/u01/app/oracle/noncdb_orcl.xml' copy;
Pluggable database created.


The database has been created and is almost ready to use. But we have one last step left. Although this last step is optional but is highly recommended. We will have to run an Oracle supplied SQL script to make sure that the migration was smooth. This script is also required if you are planning to upgrade your CDB in future.


To run the script we will have to open the newly created PDB.

SQL> alter pluggable database noncdb_orcl open;
Warning: PDB altered with errors.


Database opened with errors. You can ignore this message as of now. Next, run the script while logged into PDB as SYS.

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


The script will take sometime and has lengthy output. But at the end it will leave your database in stage where it was when script was run. In our case database was open.


To make sure that everything is now OK with new PDB you can give database a bounce.

SQL> alter pluggable database noncdb_orcl close;
Pluggable database altered.

SQL> alter pluggable database noncdb_orcl open;
Pluggable database altered.

SQL> select name,open_mode from v$pdbs;
NAME OPEN_MODE
------------------------------ ----------
PDB$SEED READ ONLY
PDB12C READ WRITE
NONCDB_ORCL READ WRITE

Cloning vs Snapshot

When using the Cloning method above, the database will require exactly the same space as that consumed by the source database. Fortunately though, cloning further supports “SNAPSHOT COPY” which is synonymous with the traditional SNAP mechanism, reducing the requirementdisk space. However this option is only supported with ACFS and direct NFS Client storage. You can read more details in the CREATE PLUGGABLE DATABASE document.


Go to this article forOracle 12c quick installation guide on Solaris 11.

.

Leave a Reply

Your email address will not be published.