本文介绍 Oracle 10g 及之后版本导出/入工具的步骤,内容如下:
- 开始使用 datapump 导出
- 创建数据库目录
- 再次尝试导出
- 导入另一个数据库
- 网络导入
- 从 PL/SQL 调用
- 导出/入说明
- 更多
- 参考资料
开始用 datapump 导出
$ expdp scott/tiger DIRECTORY=dmpdir DUMPFILE=scott.dmp
Export: Release 10.2.0.1.0 - 64bit Production on Friday, 31 March, 2006 11:36:07
Copyright (c) 2003, 2005, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
ORA-39002: invalid operation
ORA-39070: Unable to open the log file.
ORA-39087: directory name DMPDIR is invalid
说明:
- "dmpdir" 为导出目录;
- "scott.dmp" 为导出的 datadump 文件;
- scott 用户下的表在默认表空间 USERS 和临时表空间 TEMP 下。所以这个命令将 scott 用户把自己的表(对象)导出来;
- 因为,Oracle 已经有这两个表空间,所以,直接导入即可。但是,如果是自己创建的表空间,就先要创建这个表空间,然后再导入。
导出失败了!因为,我们需要先创建目录!
创建数据库目录
执行如下命令,创建一个数据库导出目录。该目录必须指向一个同一个服务器的有效目录作为数据库:
SQL> CREATE DIRECTORY dmpdir AS '/opt/oracle';
Directory created.
SQL> GRANT read, write ON DIRECTORY dmpdir TO scott;
Grant succeeded.
说明:
- "dmpdir" 为导出目录,"/opt/oracle" 为数据库路径(也可以操作系统的绝对路径,如"d:\db_backup");
- 导出目录授权。让 scott 用户对该目录可读写。这样,scott 用户才能正常访问该目录,将数据库内容和导出的日志文件放在这里。
备注:Oracle 从 Oracle 10g R2 开始,引入了一个称为 "DATA_PUMP_DIR" 的默认目录,如下所示:
SQL> SELECT directory_path FROM dba_directories WHERE directory_name = 'DATA_PUMP_DIR';
DIRECTORY_PATH
--------------------------------------------------------------------------------
/app/oracle/product/10.2.0/rdbms/log/
再次尝试导出
$ expdp scott/tiger DIRECTORY=dmpdir DUMPFILE=scott.dmp
Export: Release 10.2.0.1.0 - 64bit Production on Friday, 31 March, 2006 11:41:02
Copyright (c) 2003, 2005, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
Starting "SCOTT"."SYS_EXPORT_SCHEMA_01": scott/******** DIRECTORY=dmpdir DUMPFILE=scott.dmp
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 175.2 MB
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TYPE/TYPE_SPEC
Processing object type SCHEMA_EXPORT/TABLE/TABLE
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/COMMENT
Processing object type SCHEMA_EXPORT/PACKAGE/PACKAGE_SPEC
Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE
Processing object type SCHEMA_EXPORT/PACKAGE/COMPILE_PACKAGE/PACKAGE_SPEC/ALTER_PACKAGE_SPEC
Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE
Processing object type SCHEMA_EXPORT/PACKAGE/PACKAGE_BODY
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type SCHEMA_EXPORT/JOB
. . exported "SCOTT"."BIGEMP" 145.2 MB 3670016 rows
. . exported "SCOTT"."DEPT" 5.656 KB 4 rows
. . exported "SCOTT"."EMP" 7.820 KB 14 rows
. . exported "SCOTT"."ORD_CHARGE_TAB" 5.296 KB 2 rows
. . exported "SCOTT"."SALGRADE" 5.585 KB 5 rows
. . exported "SCOTT"."BONUS" 0 KB 0 rows
. . exported "SCOTT"."NEWOBJECT1_T" 0 KB 0 rows
. . exported "SCOTT"."T1" 0 KB 0 rows
Master table "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_SCHEMA_01 is:
/app/oracle/scott.dmp
Job "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully completed at 11:44:50
导入到另一个数据库
$ impdp system/oracle DIRECTORY=dmpdir DUMPFILE=scott.dmp
Import: Release 10.2.0.1.0 - 64bit Production on Friday, 31 March, 2006 12:00:59
Copyright (c) 2003, 2005, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
Master table "SYSTEM"."SYS_IMPORT_FULL_02" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_FULL_02": system/******** DIRECTORY=dmpdir DUMPFILE=scott.dmp
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TYPE/TYPE_SPEC
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."BIGEMP" 145.2 MB 3670016 rows
. . imported "SCOTT"."DEPT" 5.656 KB 4 rows
. . imported "SCOTT"."EMP" 7.820 KB 14 rows
. . imported "SCOTT"."ORD_CHARGE_TAB" 5.296 KB 2 rows
. . imported "SCOTT"."SALGRADE" 5.585 KB 5 rows
. . imported "SCOTT"."BONUS" 0 KB 0 rows
. . imported "SCOTT"."NEWOBJECT1_T" 0 KB 0 rows
. . imported "SCOTT"."T1" 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/PACKAGE/PACKAGE_SPEC
Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE
Processing object type SCHEMA_EXPORT/PACKAGE/COMPILE_PACKAGE/PACKAGE_SPEC/ALTER_PACKAGE_SPEC
Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE
Processing object type SCHEMA_EXPORT/PACKAGE/PACKAGE_BODY
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type SCHEMA_EXPORT/JOB
Job "SYSTEM"."SYS_IMPORT_FULL_02" successfully completed at 12:02:22
备注:
- expdp 和 impdp 命令都是操作系统下执行;
- 而创建导出目录,并为其授权,都是 SQL PLUS 下执行。
网络导入
通过网络模式导入,不需要任何中间的 dump 文件。数据,通过一个数据库链接(database link)导出,直接导入到目标数据库。例如:
SQL> create user new_scott identified by tiger;
User created.
SQL> grant connect, resource to new_scott;
Grant succeeded.
SQL> grant read, write on directory dmpdir to new_scott;
Grant succeeded.
SQL> grant create database link to new_scott;
Grant succeeded.
SQL> conn new_Scott/tiger
Connected.
SQL> create database link old_scott connect to scott identified by tiger using 'orcl.oracle.com';
Database link created.
$ impdp new_scott/tiger DIRECTORY=dmpdir NETWORK_LINK=old_scott remap_schema=scott:new_scott
所有的工作在目标操作系统上完成。唯一一个到源操作系统的引用是通过数据库链接(database link)。
从 PL/SQL 调用
可以从 PL/SQL 调用 datapump。这对于每天或每周计划用 DBMS_SCHEDULER 导出很方便。
DECLARE
hand NUMBER;
BEGIN
hand := Dbms_DataPump.Open(operation => 'EXPORT',
job_mode => 'FULL',
job_name => 'FULLEXPJOB',
version => 'COMPATIBLE');
Dbms_DataPump.Add_File(handle => hand,
filename => 'expdp_plsql.log',
directory => 'DMPDIR',
filetype => 3);
Dbms_DataPump.Add_File(handle => hand,
filename => 'expdp_plsql.dmp',
directory => 'DMPDIR',
filetype => 1);
-- Dbms_DataPump.Set_Parameter(handle => hand,
-- name => 'ESTIMATE',
-- value => 'STATISTICS');
Dbms_DataPump.Start_Job(hand);
END;
/
导出/入说明
一般情况下,导出/入不会遇到太多问题,只要注意一下几点就行:
- 导出目录必须存在,否则导出工具无法写 export.log 导出文件日志,会报 Ora 错误;
- 导出使用的用户必须对导出目录有读写权限。这个显而易见;
- 导入时,相应的表空间和用户必须已存在。这个也显而易见。
注意这三点,一般都会成功导出/入。
更多
以上例子对于刚开始的你已经足够。更多信息,请参看 Oracle 工具指南。输入下面命令可以列出该命令的所有参数。
$ expdp help=yes
$ impdp help=yes
以上是 Oracle FAQ 给出的导入/出工具的示例。
稍后,在我机器给出演示。
参考资料
- Oracle 10g 以前的导出/入工具 http://www.orafaq.com/wiki/Import_Export_FAQ
- Oracle 工具 http://www.orafaq.com/wiki/Category:Utilities