Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Moving Data Between PDBs

Back


Lab: Moving Data Between PDBs

Requirements

diagram_lab


Create Source PDB

sqlplus / as sysdba

show user
--USER is "SYS"
show con_name
--CON_NAME
--------------------------------
--CDB$ROOT

-- verify creation
show pdbs
-- 8 PDBX1                          READ WRITE NO

alter session set container=pdbx1;
show con_name
--CON_NAME
--------------------------------
--PDBX1

-- gran privilege
grant create session, dba to pdbx1_admin;
vi $ORACLE_HOME/network/admin/tnsnames.ora

# add:
PDBX1 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = test.com)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = pdbx1.com)
    )
  )
sqlplus pdbx1_admin@pdbx1

show user
show con_name
create user x1 identified by x1;
grant create session, create view, create table, unlimited tablespace to x1;

connect x1/x1@pdbx1
show user
show con_name
create table emp (id number primary key, name varchar2(100));
Insert into emp values (1, ' khaled');
Insert into emp values (2, ' ahmed');
Commit;

Create table dept ( deptno number primary key , name varchar2(100 ) );
Insert into dept values (1, ' sales dept ');
Commit;

-- verify
SELECT count(*) FROM emp;
-- 2
SELECT count(*) FROM dept;
-- 1

Create Target PDB

sqlplus / as sysdba
show user
show con_name

-- verify pdb creation
show pdbs

alter session set container=pdbx2;
show con_name
grant create session, dba to pdbx2_admin;
vi $ORACLE_HOME/network/admin/tnsnames.ora

# add:
PDBX2 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = test.com)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = pdbx2.com)
    )
  )
sqlplus pdbx2_admin/pdbx2_admin@pdbx2

create user x2 identified by x2;
grant create session, create view, create table, unlimited tablespace to x2;

connect x2/x2@pdbx2
show user
show con_name

Create Directory in Source PDB

mkdir /home/oracle/move_pdb
connect pdbx1_admin@pdbx1;
CREATE DIRECTORY d_pdbx1
AS '/home/oracle/move_pdb';

GRANT read, write ON DIRECTORY d_pdbx1 TO x1;
exit

Export Data from Source PDB to a Dump File

# in terminal
# expdb: a utility of data pump to export database
#   with X1 connection
#   schema: x1
#   directory: use dir to export
#   dumpfile: specify the file name for export
expdp x1/x1@pdbx1 SCHEMAS=x1 DIRECTORY=d_pdbx1 DUMPFILE=expx1.dmp

lab_move_data_pdb


# in terminal
# import the dmp file then output the sql file containing DDL helps in understanding the data in the dmp file
impdp x1/x1@pdbx1 SCHEMAS=x1 DIRECTORY= d_pdbx1 DUMPFILE=expx1.dmp SQLFILE=x1_sql

lab_move_data_pdb lab_move_data_pdb

note:

  • All DDL statements are read from the dump file ( not the DML )
  • DDL does not include creation of user/schema, x1.
    • This is why must create the schema in target pdb.
    • The reason for this is that this dump file is created by x1, the schema which created by sys. So x1’s file cannot include the sql to create its owner.

Import the dump file into Target PDB

# import from dump file
# remap to tell oracle to convert schema
impdp pdbx2_admin/pdbx2_admin@pdbx2 REMAP_SCHEMA=x1:x2 DIRECTORY= d_pdbx1 DUMPFILE=expx1.dmp
# ORA-39002: invalid operation
# ORA-39070: Unable to open the log file.
# ORA-39087: directory name D_PDBX1 is invalid
  • No such file or directory due to there is no directory created in the target PDB.
sqlplus pdbx2_admin/pdbx2_admin@pdbx2;

CREATE DIRECTORY d_pdbx1
AS '/home/oracle/move_pdb';
# in terminal
# import data from a dump file
impdp pdbx2_admin/pdbx2_admin@pdbx2 REMAP_SCHEMA=x1:x2 DIRECTORY= d_pdbx1 DUMPFILE=expx1.dmp

lab_move_data_pdb

sqlplus x2/x2@pdbx2

-- verify
SELECT count(*) FROM emp;
-- 2
SELECT count(*) FROM dept;
-- 1

Option: Export schema using user with DBA roles

# using pdbx1_admin with DBA role
expdp pdbx1_admin@pdbx1 SCHEMAS=x1 DIRECTORY=d_pdbx1 DUMPFILE=test.dmp

lab_move_data_pdb

# generate the sql file
impdp pdbx1_admin@pdbx1 SCHEMAS=x1 DIRECTORY=d_pdbx1 DUMPFILE=test.dmp SQLFILE=test_sql

lab_move_data_pdb

lab_move_data_pdb

  • the sql includes the statement to create user and grant privileges.

impdp pdbx2_admin/pdbx2_admin@pdbx2 REMAP_SCHEMA=x1:x3 DIRECTORY=d_pdbx1 DUMPFILE=test.dmp

lab_move_data_pdb

  • target pdb automatically create user x3
sqlplus pdbx2_admin@pdbx2
show con_name
show user

select count(*) from x3.dept;
-- 1
select count(*) from x3.emp;
-- 2

Comparison:


TOP