当前位置: 首页 > Oracle, oracle 10g, oracle 11g > 正文

在UnixLinux系统中使用命名管道来进行EXP及IMPDP的NETWORK_LINK使用

在我以前参与过的电网和联通项目中,数据库都采用非归档模式,备份恢复都是用EXP/IMP,我想还会有很多的系统的数据库也采用非归档模式,也是用EXP/IMP或者数据泵EXPDP/IMPDP来备份恢复,EXP/IMP使用也比较方便,一般也是DBA做数据迁移的首选工具,通常DBA做数据迁移的步骤都是先用EXP命令导出数据库信息,拷贝到目标数据库服务器,再用IMP命令将备份文件导入到目标数据库,或者使用EXP导出数据库信息后,直接在源服务器上使用IMP命令将备份文件加载到目标数据库,这样就使简单的操作复杂化。
在Unix/Linux系统中可以使用命名管道的方式来进行EXP/IMP,这样的好处是没有DMP文件生成,对于需要导出导入大量数据,而又没有足够空闲空间来容纳DMP文件的场合很适用,同时由于没有DMP文件生成,而且导出和导入操作可以同步进行,导出导入的总时间也会减少,下面先演示下同数据库,不同SCHEMA之间利用命名管道的方式进行EXP/IMP的数据迁移过程。
试验环境:OEL 5.7 ORACLE 10gR2
实验步骤:
1.打开一个SHELL,创建命名管道:

[oracle@dbserver1 ~]$ mknod exp.pipe p

此时会在当前目录下生成一个名字为exp.pipe的文件。

[oracle@dbserver1 ~]$ ls
createdatabase111.txt  hangfg   afiedt.buf     exp.pipe

2.使用下面的命令导出数据:

[oracle@dbserver1 ~]$ exp scott/tiger file=exp.pipe

回车以后会显示如下内容,EXP命令会停住,因为命名管道的另一边现在还没有程序接收数据。

Export: Release 10.2.0.1.0 - Production on Fri Jan 6 10:44:42 2012
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
With the Partitioning, OLAP and Data Mining options

3.新起一个SHELL,本实验将SCOTT用户下的所有信息导入到STREAM用户下,使用下面的命令进行导入操作。

[oracle@dbserver1 ~]$ imp stream/stream  fromuser=scott touser=stream
file=exp.pipe full=y

当上述命令回车以后,EXP开始导出数据,同时IMP也开始导入,EXP和IMP几乎同时结束,当上述IMP命令回车后,EXP的终端导出数据的日志如下:

Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses ZHS16GBK character set (possible charset conversion)
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SCOTT
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SCOTT
About to export SCOTT's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export SCOTT's tables via Conventional Path ...
. . exporting table                          BONUS          0 rows exported
. . exporting table                           DEPT          4 rows exported
. . exporting table                            EMP         14 rows exported
. . exporting table                       SALGRADE          5 rows exported
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully without warnings.

同时IMP命令终端显示如下日志:

Import: Release 10.2.0.1.0 - Production on Fri Jan 6 10:46:58 2012
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
With the Partitioning, OLAP and Data Mining options
Export file created by EXPORT:V10.02.01 via conventional path
Warning: the objects were exported by SCOTT, not by you
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses ZHS16GBK character set (possible charset conversion)
. importing SCOTT's objects into STREAM
. . importing table                        "BONUS"          0 rows imported
. . importing table                         "DEPT"          4 rows imported
. . importing table                          "EMP"         14 rows imported
. . importing table                     "SALGRADE"          5 rows imported
About to enable constraints...
Import terminated successfully without warnings.

用STREAM用户登录数据库,可以查看到SCOTT用户下的所有对象已经导入到STREAM用户下,证明数据迁移成功。

[oracle@dbserver1 ~]$ sqlplus stream/stream
SQL*Plus: Release 10.2.0.1.0 - Production on Fri Jan 6 10:48:02 2012
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
Error accessing PRODUCT_USER_PROFILE
Warning:  Product user profile information not loaded!
You may need to run PUPBLD.SQL as SYSTEM
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select * from tab;

TNAME			       TABTYPE	CLUSTERID
------------------------------ ------- ----------
BONUS			       TABLE
DEPT			       TABLE
EMP			           TABLE
SALGRADE		       TABLE

但是还有一个问题,就是这些表所在的表空间还存在USERS内,并没有改为STREAM表空间,迁移数据并修改表的默认表空间一直是EXP工具很麻烦的操作,使用EXPDP就很容易实现。
使用管道命名的方式也可以进行异地数据库的迁移,只是在IMP的时候加上TNS字符串就可以,下面简单演示下从OEL5.7 ORACLE 10gR2环境将数据迁移至WINDOWS XP ORACLE 11gR2数据库。
1.打开一个SHELL,创建命名管道:

[oracle@dbserver1 ~]$ mknod exp.pipe p

2.使用下面的命令导出数据:

[oracle@dbserver1 ~]$ exp scott/tiger file=exp.pipe

3.新起一个SHELL,本实验将SCOTT用户下的所有信息导入到STREAM用户下,使用下面的命令进行导入操作。

[oracle@dbserver1 ~]$ imp dbdream/dbdream@192.168.56.1/stream file=exp.pipe  full=y

当上述命令回车以后,EXP开始导出数据,同时IMP也开始导入,EXP也是和IMP几乎同时结束。
这种命名管道方式使用EXP的方式适用于10g之前的版本和不喜欢用数据泵(EXPDP/IMPDP)的用户,10g之后版本,ORACLE推出了数据泵功能,EXPDP的NETWORK_LINK可以很简单的实现这个功能,大数据量数据库使用数据泵的效率要比EXP高很多,而且数据泵的其他功能也比EXP/IMP要强大得多,比如数据泵的REMAP_TABLESPACE可以很简单的实现表空间的转换。
ORACLE高版本的客户端连接低版本的数据库会有问题,使用IMPDP的NETWORK_LINK将10g的数据直接迁移至11g的数据库会报如下错误:

D: >impdp dbdream/dbdream directory=exp network_link=l_imp schemas=scott
remap_schema=scott:dbdream
Import: Release 11.2.0.1.0 - Production on 星期二 2月 14 16:12:00 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORA-39006: 内部错误
ORA-39113: 无法确定数据库版本
ORA-04052: 在查找远程对象 SYS.DBMS_UTILITY@L_IMP 时出错
ORA-00604: 递归 SQL 级别 3 出现错误
ORA-06544: PL/SQL: internal error, arguments: [55916],[],[],[],[],[],[],[]
ORA-06553: PLS-801: internal error [55916]
ORA-02063: 紧接着 2 lines (起自 L_IMP)
ORA-39097: 数据泵作业出现意外的错误 -4052

下面演示下使用数据泵的NETWORK_LINK将OEL6.0 ORACLE 11gR2数据库中STORAGE用户下的所有对象迁移到OEL5.7 ORACLE 11gR2数据库中STORAGE用户下。
1.创建TNS字符串

FHACDB =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.20.101)(PORT = 1523))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = fhacdb)
    )
  )

2.创建DIRECTORY并授权

SQL> create directory imp as '/home/oracle';

Directory created.
SQL> grant read,write on directory imp to storage;

Grant succeeded.

3.创建DB_LINK

SQL> create public database link l_storage connect to storage identified by storage using 'fhacdb';

Database link created.

4.使用IMPDP的NETWORK_LINK进行迁移操作

[oracle@fhacdb admin]$ impdp storage/storage directory=imp
network_link=l_storage
Import: Release 11.2.0.2.0 - Production on Wed Feb 15 11:02:18 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0
With the Partitioning, OLAP, Data Mining and Real Application Testing options
...
. . 导入了 "STORAGE"."T_DICTIONARY"           123371行
. . 导入了 "STORAGE"."CHN_WEST_DATE"          198702行
. . 导入了 "STORAGE"."T_FILE_INFO_RAW"         25176行
. . 导入了 "STORAGE"."T_FILE_INFO"              6657行
. . 导入了 "STORAGE"."T_LOG_INFO"               6476行
. . 导入了 "STORAGE"."T_BATCH_QUALITY"          4760行
...

本文固定链接: http://www.dbdream.com.cn/2012/02/%e5%9c%a8unixlinux%e7%b3%bb%e7%bb%9f%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%91%bd%e5%90%8d%e7%ae%a1%e9%81%93%e6%9d%a5%e8%bf%9b%e8%a1%8cexp%e5%8f%8aimpdp%e7%9a%84network_link%e4%bd%bf%e7%94%a8/ | 信春哥,系统稳,闭眼上线不回滚!

该日志由 dbdream 于2012年02月15日发表在 Oracle, oracle 10g, oracle 11g 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 在UnixLinux系统中使用命名管道来进行EXP及IMPDP的NETWORK_LINK使用 | 信春哥,系统稳,闭眼上线不回滚!
关键字: , , , ,

在UnixLinux系统中使用命名管道来进行EXP及IMPDP的NETWORK_LINK使用:目前有2 条留言

  1. 板凳
    谭龙:

    1.你所说的不同schema之间的用户导入导出,就是单指scott和dbdream这两个不同用户下的相同表?
    2.你上面的测试意思是高版本的oracle使用impdp导入到低版本的才可以,相反是不行的是吗?而且是与平台无关的是吗?

    2012-03-16 10:08 [回复]
  2. 沙发
    streamsong:

    @谭龙
    1.我做的实验是将SCOTT用户的数据迁移到DBDREAM用户下面,这个在生产环境中是可能遇到的情况。
    2.如果在将10g的数据迁移到11g的数据库,在11g的客户端上直接做IMPDP是不可以的,反之,用10g的客户端将11g的数据迁移到10g的数据库是可以的。

    2012-03-16 10:57 [回复]

发表评论

快捷键:Ctrl+Enter