---
title: "Oracle数据泵EXPDP/IMPDP工具使用(按用户导出和导入)"
author: "futeng"
date: "2014-05-06T21:57:59Z"
tags: ["Oracle", "expdp", "impdp", "导入", "导出"]
categories: ["数据库"]
slug: "oracle-expdp-impdp"
draft: false
lastmod: "2014-05-13T11:12:37Z"
id: 2062337
url: "https://futeng.iteye.com/blog/2062337"
---

### 1. Oracle环境变量配置

Oracle路径配置  
可通过expdp help查看工具使用环境。通常对于未配置Oracle环境变量，会报以下使用错误：

 

```
UDE-00013: Message 13 not found; No message file for product=RDBMS, facility=UDE
UDE-00019: You may need to set ORACLE_HOME to your Oracle software directory
```

 出现上述问题是因为无法找到$ORACLE\_HOME目录。尝试配置Oracle重要的环境变量，`source /home/oracle/.bash_profile`重新配置环境试试。

 

 

### 2. 创建Directory转储文件目录

该目录用来存放转储文件，目录必须存在且用户得有权限写。

 

```
-- sqlplus
create directory dump_dir as '/home/oracle/oradir';
```

 

 

当然如果使用的是普通用户来操作数据，你还需要给用户赋权限:

 

```
grant read, write directory on direcotry crxj_dump_dir to scott;
```

 **查看当前所有Directory**

 

 

```
select * from dba_directories;
```

 

 

OWNER DIRECTORY\_NAME DIRECTORY\_PATH SYS DUMP\_DIR /home/oracle/oradir

**注意创建的需要确实存在**

当目录不存在会报出如下错误（示例为sys用户）：

```
[oracle@CRXJ_APP_218 bin]$ expdp sys/orcl directory=dump_dir dumpfile=schema_pon.dmp schemas=pon

Export: Release 11.2.0.1.0 - Production on Tue May 6 18:34:36 2014

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

UDE-28009: operation generated ORACLE error 28009
ORA-28009: connection as SYS should be as SYSDBA or SYSOPER

Username: sys as sysdba
Password: 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORA-39002: invalid operation
ORA-39070: Unable to open the log file.
ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation
```

 

此时需要创建该目录

```
[oracle@CRXJ_APP_218 bin]$ mkdir /home/oracle/oradir
[oracle@CRXJ_APP_218 bin]$ ls /home/oracle/oradir
```

 

至此配置好环境，也配置好存放转储文件的路径，接下来就可以导入导出了。

* * *

*示例将使用sys用户，采用按用户模式导出crxj\_collect用户下所有结构和数据，并导入到同用户名的另外一个数据下。*

### 按用户(模式)导出

```
-- expdp username/passwd dirctory=转储文件存放目录 dumpfile=要生成的转储文件名 schemas=用户名(可多个并用逗号分开)
-- 导出CRXJ_COLLECT用户[shell中直接执行]
expdp sys/orcl directory=dump_dir dumpfile=schema_crxj_collect.dmp schemas=crxj_collect
```

 

```
[oracle@CRXJ_APP_218 oradir]$ expdp sys/orcl directory=dump_dir dumpfile=schema_crxj_collect.dmp schemas=crxj_collect

Export: Release 11.2.0.1.0 - Production on Tue May 6 20:48:19 2014

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

UDE-28009: operation generated ORACLE error 28009
ORA-28009: connection as SYS should be as SYSDBA or SYSOPER

Username: sys as sysdba
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Starting "SYS"."SYS_EXPORT_SCHEMA_01":  sys/******** AS SYSDBA directory=dump_dir dumpfile=schema_crxj_collect.dmp schemas=crxj_collect
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 1.259 GB
Processing object type SCHEMA_EXPORT/USER
*****省略N多表******
. . exported "CRXJ_COLLECT"."HOST_SESSION"                   0 KB       0 rows
Master table "SYS"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYS.SYS_EXPORT_SCHEMA_01 is:
  /home/oracle/oradir/schema_crxj_collect.dmp
Job "SYS"."SYS_EXPORT_SCHEMA_01" successfully completed at 20:50:49

[oracle@CRXJ_APP_218 oradir]$
```

 

### 导入到指定用户(模式)

*由于是导入到远程库，所以还需要创建下转储文件所在目录*

```
-- 省略掉如何把导出的转储文件移动到指定远程库目录下
SQL> create directory dump_dir as '/home/oracle/oradir';

Directory created.

SQL>
```

 

```
-- 导入数据
-- impdp 用户名/密码 DIRECTORY=转储文件所在目录 DUMPFILE=带导入的转储文件名 SCHEMAS=待导入的用户(模式)名
impdp sys/orcl DIRECTORY= dump_dir DUMPFILE=schema_crxj_collect.dmp SCHEMAS=CRXJ_COLLECT;
```

 

*因为不要求导出和导入的是同一个用户，所以数据泵这个工具可以方便的在不同用户（模式）见传到数据对象。*  
*数据泵高级操作中可以实现数据对象的逻辑恢复，包括按表、按用户模式（本文即是），按表空间和全库导入导出。*

更多内容有待更新，敬请期待。

转载请标明原文链接。

ifuteng@gmail.com :)