Oracle Data Pump Full Transportable方式导入导出

Posted dingdingfish

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle Data Pump Full Transportable方式导入导出相关的知识,希望对你有一定的参考价值。

参考

本实验参考了Oracle官方白皮书:Full Transportable Export and Import,简明扼要,写得很好。

实验环境准备

利用OCI上的Oracle数据库image建立3套单实例数据库环境,SYS口令为Ora_DB4U,普通用户口令为Welcome1

数据库环境/主机名版本公网IP私网IPDB_NAMECDBPDB
源数据库1/db122-noncdb12.2138.3.221.18810.0.0.15NONCDB12NN/A
源数据库2/db122-cdb12.2158.101.152.6010.0.0.107CDB12YORCLPDB1
目标数据库/db19-cdb19c158.101.67.21210.0.0.203DB19YORCLPDB1

源数据库是指需要迁移的数据库,目标数据库指需要迁往的数据库。

源数据库上均安装了Oracle示例Schema。安装方法请参见如何使用github安装Oracle 数据库12c Sample Schema (示例Schema)

源数据库中对象的情况如下:

system@ORCLPDB1> select owner, count(*) from dba_objects where owner in 
(select username from all_users where ORACLE_MAINTAINED = 'N') group by owner order by owner;
OWNER                  COUNT(*)
-------------------- ----------
BI                            8
HR                           34
IX                           58
OE                          142
PM                           22
SH                          310

6 rows selected.

3个数据库服务器均开放1521端口:

firewall-cmd --add-port=1521/tcp  --permanent
firewall-cmd --reload
# firewall-cmd --list-all
public
  target: default
  icmp-block-inversion: no
  interfaces:
  sources:
  services: dhcpv6-client ssh
  ports: 1521/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

3个数据库服务器均通过/etc/hosts设置了主机名解析:

10.0.0.15       db122-noncdb
10.0.0.107      db122-cdb
10.0.0.203      db19-cdb

3个数据库服务器均在oracle用户根目录下建立了dumpdir目录:

$ id -un
oracle
$ cd ~
$ mkdir dumpdir

实验1:non-CDB到PDB的Full Transportable迁移

源数据库:db122-noncdb上的NONCDB12数据库中的所有用户Schema
目标数据库:db19-cdb上的orclpdb2

是否满足Full Transportable迁移要求

我们需要迁移的只有一个表空间USERS,TRANSPORT_SET_CHECK也支持参数为逗号分割的多个表空间。

sys@NONCDB12> select TABLESPACE_NAME from dba_tablespaces;

TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS

sys@NONCDB12> EXECUTE DBMS_TTS.TRANSPORT_SET_CHECK('users', TRUE);

PL/SQL procedure successfully completed.

sys@NONCDB12> SELECT * FROM TRANSPORT_SET_VIOLATIONS;

no rows selected

TRANSPORT_SET_VIOLATIONS无输出,说明满足要求。

源数据库服务器上的Full Transportable导出

定义目录:

create directory my_data_pump_dir as '/home/oracle/dumpdir';

准备data pump参数文件export.par:

[oracle@db122-cdb dumpdir]$ cat export.par
DIRECTORY=my_data_pump_dir
DUMPFILE=dumpfile%U.dmp
LOGFILE=logfile.log
EXCLUDE=STATISTICS
LOGTIME=ALL
METRICS=YES
PARALLEL=4
FULL=Y
TRANSPORTABLE=ALWAYS

将需要迁移的表空间置为只读:

sys@NONCDB12> alter tablespace users read only;

执行导出:

[oracle@db122-noncdb dumpdir]$ expdp system/Ora_DB4U@noncdb12 parfile=export.par
...
25-NOV-21 03:04:45.214: Dump file set for SYSTEM.SYS_EXPORT_FULL_01 is:
25-NOV-21 03:04:45.216:   /home/oracle/dumpdir/dumpfile01.dmp
25-NOV-21 03:04:45.217:   /home/oracle/dumpdir/dumpfile02.dmp
25-NOV-21 03:04:45.217: ******************************************************************************
25-NOV-21 03:04:45.219: Datafiles required for transportable tablespace USERS:
25-NOV-21 03:04:45.220:   /u01/app/oracle/oradata/noncdb12/users01.dbf
25-NOV-21 03:04:45.240: Job "SYSTEM"."SYS_EXPORT_FULL_01" completed with 1 error(s) at Thu Nov 25 03:04:45 2021 elapsed 0 00:02:14

在输出的最后部分,指出了需要传输的Datafiles:/u01/app/oracle/oradata/noncdb12/users01.dbf

导出文件为:

[oracle@db122-noncdb dumpdir]$ ls -l
total 6340
-rw-r-----. 1 oracle dba      5554176 Nov 25 03:04 dumpfile01.dmp
-rw-r-----. 1 oracle dba       897024 Nov 25 03:04 dumpfile02.dmp
-rw-r--r--. 1 oracle oinstall     153 Nov 25 03:00 export.par
-rw-r-----. 1 oracle dba        25849 Nov 25 03:04 logfile.log

将导出文件和数据文件拷贝到目标数据库服务器,数据文件我们后续还会放到正确的位置:

[oracle@db122-noncdb dumpdir]$ scp dumpfile*.dmp db19-cdb:~/dumpdir
[oracle@db122-noncdb dumpdir]$ scp /u01/app/oracle/oradata/noncdb12/users01.dbf db19-cdb:~/dumpdir

目标数据库服务器上的Full Transportable导入

使用脚本CreateTargetCDB.sql建立空的PDB:orclpdb2(因为已经有orclpdb1了):

-- CreateTargetCDB.sql
alter pluggable database orclpdb2 close;
drop pluggable database orclpdb2 including datafiles;

create pluggable database orclpdb2 admin user pdbadmin identified by Welcome1 
default tablespace users datafile '/u01/app/oracle/oradata/DB19/orclpdb2/user01.dbf' size 1m autoextend on next 1m
FILE_NAME_CONVERT = ('pdbseed', 'orclpdb2');

alter pluggable database orclpdb2 open;
alter pluggable database orclpdb2 save state;

alter session set container=orclpdb2;
create directory my_data_pump_dir as '/home/oracle/dumpdir';

将数据文件放到正确的位置:

[oracle@db122-noncdb dumpdir] $ cp users01.dbf /u01/app/oracle/oradata/DB19/orclpdb2/samples01.dbf

为什么要改名?因为目标数据库已经有USERS表空间了,那我们只有改为SAMPLES了。

import参数文件如下,USERS表空间映射成为了SAMPLES:

[oracle@db19-cdb dumpdir]$ cat import.par
DIRECTORY=my_data_pump_dir
DUMPFILE=dumpfile%U.dmp
LOGFILE=logfile.log
EXCLUDE=STATISTICS
LOGTIME=ALL
METRICS=YES
REMAP_TABLESPACE=USERS:SAMPLES
TRANSPORT_DATAFILES='/u01/app/oracle/oradata/DB19/orclpdb2/samples01.dbf'

导入:

[oracle@db19-cdb dumpdir]$ impdp system/Ora_DB4U@orclpdb2 parfile=import.par
...
25-NOV-21 03:34:50.047: ORA-31684: Object type TABLESPACE:"UNDOTBS1" already exists

25-NOV-21 03:34:50.047: ORA-31684: Object type TABLESPACE:"TEMP" already exists
...
25-NOV-21 03:34:50.762: ORA-31685: Object type USER:"SYS" failed due to insufficient privileges. Failing sql is:
 ALTER USER "SYS" IDENTIFIED BY VALUES 'S:0BC45F1BB7B5F39E1813AC49299EE458BC194F6C5A03CE74CC7FDF906EC5;T:2043EAC278CA6C9A199CECEC834994C543B5A5E9498F255278BA1968AED618CBC84F71925E734C4490FE6870F1209682834CB1295A0EA19AB9C41073528779A63AED2708E730F307329789FC8FCCAC7F' TEMPORARY TABLESPACE "TEMP"
...
25-NOV-21 03:34:56.950: ORA-39083: Object type DEFAULT_ROLE:"SPATIAL_CSW_ADMIN_USR" failed to create with error:
ORA-01918: user 'SPATIAL_CSW_ADMIN_USR' does not exist
...
25-NOV-21 03:36:50.305: Job "SYSTEM"."SYS_IMPORT_TRANSPORTABLE_01" completed with 12 error(s) at Thu Nov 25 03:36:50 2021 elapsed 0 00:02:11

有12个错误,但都是无关紧要的,如UNDOTBS1和TEMP表空间已存在,修改SYS口令失败,未安装Spatial等。

验证,以下语句说明数据和权限都导入了:

[oracle@db19-cdb dumpdir]$ sqlplus oe/Welcome1@orclpdb2
oe@ORCLPDB2> select count(*) from hr.employees;

  COUNT(*)
----------
       107

再比较对象数量:

SQL> select owner, count(*) from dba_objects where owner in 
(select username from all_users where ORACLE_MAINTAINED = 'N') group by owner;

OWNER                  COUNT(*)
-------------------- ----------
BI                            8
HR                           34
IX                           58
OE                          124
PM                           22
SH                          310

6 rows selected.

其它的都对,OE的对象应该为142,少了18个。原因是OE Schema需要Spatial等组件,而这些并没有安装到PDB中。

也许你需要将SAMPLES设为默认表空间:

sys@DB19> ALTER DATABASE DEFAULT TABLESPACE SAMPLES;

Database altered.

将源数据库置为可读写

sys@NONCDB12> ALTER TABLESPACE users READ WRITE;

Tablespace altered.

导入后更新字典统计信息

sys@DB19> alter session set container=orclpdb2;

Session altered.

sys@DB19> exec dbms_stats.gather_dictionary_stats;

PL/SQL procedure successfully completed.

清理目标数据库环境

第一次删除失败:

sys@DB19> drop tablespace samples including contents and datafiles cascade constraints;
drop tablespace samples including contents and datafiles cascade constraints
*
ERROR at line 1:
ORA-29857: domain indexes and/or secondary objects exist in the tablespace

参考文档

sys@DB19> select t.owner, t.table_name from dba_tables t inner join dba_indexes i on t.owner = i.table_owner and t.table_name = i.table_name where i.index_type= 'DOMAIN' and t.tablespace_name = 'SAMPLES';

OWNER
----------
TABLE_NAME
--------------------------------------------------------------------------------
SH
SUPPLEMENTARY_DEMOGRAPHICS


sys@DB19> drop table SH.SUPPLEMENTARY_DEMOGRAPHICS cascade constraints purge;

Table dropped.

sys@DB19> select owner, object_name from dba_objects where object_type = 'MATERIALIZED VIEW';

OWNER
----------
OBJECT_NAME
--------------------------------------------------------------------------------
SH
FWEEK_PSCAT_SALES_MV

SH
CAL_MONTH_SALES_MV


sys@DB19> drop materialized view SH.CAL_MONTH_SALES_MV;

Materialized view dropped.

sys@DB19> drop materialized view SH.FWEEK_PSCAT_SALES_MV;

Materialized view dropped.

sys@DB19> drop tablespace samples including contents and datafiles cascade constraints;

Tablespace dropped.

以上是关于Oracle Data Pump Full Transportable方式导入导出的主要内容,如果未能解决你的问题,请参考以下文章

『ORACLE』 数据泵Data Pump中expdp导出(11g)

Oracle Database Data Pump(expdp/impdp)

如何将常规文件(例如 cwallet.sso )上传到 oracle db 中的 data_pump_dir?

Oracle Data Pump Schema 方式导入导出

Oracle Data Pump Schema 方式导入导出

ORA-39405: Oracle Data Pump 时区升级