前一些测试里面,通过expdp导出了各种数据库表、模式、表空间以及全库。
在这里,我们就通过之前导出来的逻辑备份文件,使用impdp工具把导出的文件
逆向导入数据库里面。
----使用数据泵impdp导入数据:
--策略1:查看Scott用户拥有的表:
SQL> select tname from tab;
TNAME
------------------------------
BONUS
DEPT
EMP
HISLOADER
MYLOADER
SALGRADE
YOURLOADER
7 rows selected.
#可以看到Scott用户下只用7张表。
--使用impdp将从suxing用户导出的mytest表导入Scott用户:
--尝试策略2:impdp suxing/oracle directory=dump_home dumpfile=mytest.dmp remap_schema=suxing:scott
--导入策略3:impdp system/oracle directory=dump_home dumpfile=mytest.dmp remap_schema=suxing:scott
[oracle@enmo dirhome]$ impdp suxing/oracle directory=dump_home dumpfile=mytest.dmp remap_schema=suxing:scott
Import: Release 11.2.0.4.0 - Production on Wed Nov 2 13:53:14 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORA-31631: privileges are required
ORA-39122: Unprivileged users may not perform REMAP_SCHEMA remappings.
[oracle@enmo dirhome]$
#直接使用suxing用户导入发现没有权限,那就需要更改导入方案中的用户。
[oracle@enmo dirhome]$ impdp system/oracle directory=dump_home dumpfile=mytest.dmp remap_schema=suxing:scott
Import: Release 11.2.0.4.0 - Production on Wed Nov 2 13:54:30 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_FULL_01": system/******** directory=dump_home dumpfile=mytest.dmp remap_schema=suxing:scott
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."MYTEST" 5.859 KB 1 rows
Job "SYSTEM"."SYS_IMPORT_FULL_01" successfully completed at Wed Nov 2 13:54:34 2016 elapsed 0 00:00:03
[oracle@enmo dirhome]$
#更换使用system用户后,成功将suxing用户的表导入到Scott用户。
--到Scott用户查看新导入的表,并查看表中的数据:
SQL> select tname from tab;
TNAME
------------------------------
BONUS
DEPT
EMP
HISLOADER
MYLOADER
MYTEST
SALGRADE
YOURLOADER
8 rows selected.
#可见,比上面的查询多出了一个表。
--查看表MYTEST的数据:
SQL>
SQL> select * from MYTEST;
ID NAME CREATED
---------- ------ ---------
123 susky 01-NOV-16
#导入成功。
--导入Scott模式schema到数据库:
--策略1:删除数据库中已有的Scott用户的schema:
SQL> show user
USER is "SYS"
SQL> drop user scott cascade;
User dropped.
SQL> select username from dba_users
2 where username like 'sco%';
no rows selected
#已经删除用户。
--没有创建用户下直接导入:
[oracle@enmo dirhome]$ impdp system/oracle directory=dump_home dumpfile=scott.dmp
Import: Release 11.2.0.4.0 - Production on Wed Nov 2 14:11:01 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_FULL_01": system/******** directory=dump_home dumpfile=scott.dmp
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
ORA-39083: Object type PROCACT_SCHEMA failed to create with error:
ORA-31625: Schema SCOTT is needed to import this object, but is unaccessible
ORA-01435: user does not exist
Failing sql is:
BEGIN
sys.dbms_logrep_imp.instantiate_schema(schema_name=>SYS_CONTEXT('USERENV','CURRENT_SCHEMA'), export_db_name=>'PROD', inst_scn=>'1050430');COMMIT; END;
Processing object type SCHEMA_EXPORT/TABLE/TABLE
ORA-39083: Object type TABLE:"SCOTT"."DEPT" failed to create with error:
ORA-01918: user 'SCOTT' does not exist
Failing sql is:
CREATE TABLE "SCOTT"."DEPT" ("DEPTNO" NUMBER(2,0), "DNAME" VARCHAR2(14 BYTE), "LOC" VARCHAR2(13 BYTE)) SEGMENT CREATION IMMEDIATE PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) TABLESPAC
ORA-39083: Object type TABLE:"SCOTT"."EMP" failed to create with error:
ORA-01918: user 'SCOTT' does not exist
Failing sql is:
CREATE TABLE "SCOTT"."EMP" ("EMPNO" NUMBER(4,0), "ENAME" VARCHAR2(10 BYTE), "JOB" VARCHAR2(9 BYTE), "MGR" NUMBER(4,0), "HIREDATE" DATE, "SAL" NUMBER(7,2), "COMM" NUMBER(7,2), "DEPTNO" NUMBER(2,0)) SEGMENT CREATION IMMEDIATE PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1
ORA-39083: Object type TABLE:"SCOTT"."BONUS" failed to create with error:
ORA-01918: user 'SCOTT' does not exist
Failing sql is:
CREATE TABLE "SCOTT"."BONUS" ("ENAME" VARCHAR2(10 BYTE), "JOB" VARCHAR2(9 BYTE), "SAL" NUMBER, "COMM" NUMBER) SEGMENT CREATION DEFERRED PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING TABLESPACE "USERS"
ORA-39083: Object type TABLE:"SCOTT"."SALGRADE" failed to create with error:
ORA-01918: user 'SCOTT' does not exist
Failing sql is:
CREATE TABLE "SCOTT"."SALGRADE" ("GRADE" NUMBER, "LOSAL" NUMBER, "HISAL" NUMBER) SEGMENT CREATION IMMEDIATE PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) TABLESPACE "USERS"
ORA-39083: Object type TABLE:"SCOTT"."MYLOADER" failed to create with error:
ORA-01918: user 'SCOTT' does not exist
Failing sql is:
CREATE TABLE "SCOTT"."MYLOADER" ("ID" NUMBER(3,0), "DNAME" VARCHAR2(12 BYTE), "LNAME" VARCHAR2(10 BYTE)) SEGMENT CREATION IMMEDIATE PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) TAB
ORA-39083: Object type TABLE:"SCOTT"."HISLOADER" failed to create with error:
ORA-01918: user 'SCOTT' does not exist
Failing sql is:
CREATE TABLE "SCOTT"."HISLOADER" ("ID" NUMBER(3,0), "DNAME" VARCHAR2(12 BYTE), "LNAME" VARCHAR2(10 BYTE)) SEGMENT CREATION IMMEDIATE PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) T
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
ORA-39112: Dependent object type INDEX:"SCOTT"."PK_DEPT" skipped, base object type TABLE:"SCOTT"."DEPT" creation failed
ORA-39112: Dependent object type INDEX:"SCOTT"."PK_EMP" skipped, base object type TABLE:"SCOTT"."EMP" creation failed
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
ORA-39112: Dependent object type CONSTRAINT:"SCOTT"."PK_DEPT" skipped, base object type TABLE:"SCOTT"."DEPT" creation failed
ORA-39112: Dependent object type CONSTRAINT:"SCOTT"."PK_EMP" skipped, base object type TABLE:"SCOTT"."EMP" creation failed
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
ORA-39112: Dependent object type INDEX_STATISTICS skipped, base object type INDEX:"SCOTT"."PK_DEPT" creation failed
ORA-39112: Dependent object type INDEX_STATISTICS skipped, base object type INDEX:"SCOTT"."PK_EMP" creation failed
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
ORA-39112: Dependent object type REF_CONSTRAINT:"SCOTT"."FK_DEPTNO" skipped, base object type TABLE:"SCOTT"."EMP" creation failed
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SCOTT"."DEPT" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SCOTT"."EMP" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SCOTT"."BONUS" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SCOTT"."SALGRADE" creation failed
Job "SYSTEM"."SYS_IMPORT_FULL_01" completed with 18 error(s) at Wed Nov 2 14:11:06 2016 elapsed 0 00:00:03
[oracle@enmo dirhome]$
导入所有表都失败。
--先创建用户并授权:
SQL> create user scott identified by tiger;
User created.
SQL> grant create session,resource to scott;
Grant succeeded.
SQL> select tname from tab;
no rows selected
#发现没有任何对象。
--导入方案2:impdp system/oracle directory=dump_home dumpfile=scott.dmp
[oracle@enmo dirhome]$ impdp system/oracle directory=dump_home dumpfile=scott.dmp
Import: Release 11.2.0.4.0 - Production on Wed Nov 2 14:22:12 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_FULL_01": system/******** directory=dump_home dumpfile=scott.dmp
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."DEPT" 5.929 KB 4 rows
. . imported "SCOTT"."EMP" 8.562 KB 14 rows
. . imported "SCOTT"."HISLOADER" 5.921 KB 3 rows
. . imported "SCOTT"."MYLOADER" 6.023 KB 7 rows
. . imported "SCOTT"."SALGRADE" 5.859 KB 5 rows
. . imported "SCOTT"."BONUS" 0 KB 0 rows
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_FULL_01" successfully completed at Wed Nov 2 14:22:17 2016 elapsed 0 00:00:04
[oracle@enmo dirhome]$
#导入成功。
--查看用户下的表对对象:
SQL> show user
USER is "SCOTT"
SQL> select tname from tab;
TNAME
------------------------------
BONUS
DEPT
EMP
HISLOADER
MYLOADER
SALGRADE
6 rows selected.
--导入MySpace表空间:
--导入策略: impdp system/oracle directory=dump_home dumpfile=myspace.dmp tablespaces=myspace
SQL> select tablespace_name from dba_tablespaces;
TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS
EXAMPLE
MYSPACE
7 rows selected.
#一共7个表空间。
--在Scott用户创建一个表存放到MySpace的表空间数据文件里面:
SQL> create table t1 tablespace myspace
2 as select * from all_objects
3 where rownum <=100;
Table created.
SQL> select table_name from dba_tables
2 where tablespace_name='MYSPACE';
ABLE_NAME
------------------------------
MYTEST
T1
--删除MYSPACE表空间的表:
SQL> drop table scott.t1 purge;
Table dropped.
SQL> drop table suxing.mytest purge;
Table dropped.
--再次查看表空间的表:
SQL> select table_name from dba_tables
2 where tablespace_name='MYSPACE';
no rows selected
--导入表空间:
[oracle@enmo dirhome]$ impdp system/oracle directory=dump_home dumpfile=myspace.dmp tablespaces=myspace
Import: Release 11.2.0.4.0 - Production on Wed Nov 2 15:29:29 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_TABLESPACE_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_TABLESPACE_01": system/******** directory=dump_home dumpfile=myspace.dmp tablespaces=myspace
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "SUXING"."MYTEST" 5.859 KB 1 rows
Job "SYSTEM"."SYS_IMPORT_TABLESPACE_01" successfully completed at Wed Nov 2 15:29:33 2016 elapsed 0 00:00:02
[oracle@enmo dirhome]$
#导入成功。
--查看:
SQL> select table_name from dba_tables
2 where tablespace_name='MYSPACE';
TABLE_NAME
------------------------------
MYTEST
--使用impdp导入全库的的策略:
impdp system/oracle directory=dump_home dumpfile=PROD.dmp full=y