同事在家使用VPN连到单位导入一个Oracle dmp文件,说导不进去,让我帮忙看看。
根据他发过来的截图,了解到他在使用PLSQL工具导入dmp文件,而PLSQL会调用本地 imp 工具进行导入。
在PLSQL执行时会弹出 imp 弹窗,对方说到这一步就卡住了。
因为他本地安装的是 10g 客户端,此时将 dmp 导入到 11g 的库里是有问题的。于是我让对方把 dmp 文件发给我看看。
在处理dmp导入时如果无法获取对方是如何导出的,此时就只能先拿到dmp文件,再分析导入时的环境。
Oracle 有两种导入/导出方式,一种是exp/imp,另一种是expdp/impdp,在处理导入时最好知道对方是使用哪种工具导出的。如果能知道对方导出用的命令以及导出时的日志,则对导入工作会非常有帮忙。
此时拿到的 dmp 文件有900M,在家里上传速度只有 30Mbps,所以上传至服务需要先将 dmp 文件压缩一下。同事发给我的是个 RAR 的压缩包,有 300M。我试着重新用 zip 格式重新压缩了一下,发现压缩比不理想,于是还是使用原 RAR 格式上传。
在网上找到Linux版的 RAR 解压缩工具,将工具上传至服务器,在服务器上对压缩包进行解压。Linux 版的 RAR 工具下载地址:
https://www.rarlab.com/download.htm
RAR解压缩命令:
$ chmod +x unrar
$ ./unrar e 1.rar
UNRAR 6.12 freeware Copyright (c) 1993-2022 Alexander Roshal
Extracting from 1.rar
Extracting 1.dmp OK
All OK
复制
对于来源不明的 dmp 文件,在不知道导出版本,导出用户的情况下如何获取信息?此时可以直接执行导入命令,并让命令失败,导入时 imp 会读取 dmp 文件,并打印更多信息。从这些日志输出中我们就可以获取我们想知道的一些信息。
我们先做一下准备工作:
获取当前主机内所有运行的数据库的信息:
$ su - oracle
$ ps -eaf| grep ora_pmon| grep -v grep
oracle 0 2021 ? 00:44:18 ora_pmon_vdb_pacs1
oracle 0 2021 ? 00:48:27 ora_pmon_mdb_20210824144328
oracle 0 2021 ? 00:39:47 ora_pmon_mdb_20211112205034
oracle 0 Jul08 ? 00:03:12 ora_pmon_vdb_E4F2
oracle 0 May13 ? 00:11:17 ora_pmon_mdb_20210824144254
oracle 0 2021 ? 01:06:12 ora_pmon_vdb_his2
oracle 0 Jun08 ? 00:06:45 ora_pmon_pacsdb19c
oracle 0 2021 ? 00:49:57 ora_pmon_mdb_20210824122454
oracle 0 07:18 ? 00:00:07 ora_pmon_vdb_his1
oracle 0 2021 ? 00:50:37 ora_pmon_mdb_20210824144150
oracle 0 May12 ? 00:17:21 ora_pmon_xycdb
oracle 0 Mar21 ? 00:27:04 ora_pmon_vdb_web
oracle 0 Jul08 ? 00:03:18 ora_pmon_vdb_neuhip0707
复制
将数据库切换到我们需要操作的库上,并根据实际情况设置字符集环境变量:
$ export ORACLE_SID=vdb_web
$ export NLS_LANG="SIMPLIFIED CHINESE_CHINA.ZHS16GBK"
复制
做完准备工作,我们尝试使用 imp 测试 dmp 文件,看是否是 exp 导出的。此时只加少量参数,如果导入失败则会输出更多信息。
$ imp \'/ as sysdba\' file=1.dmp
Import: Release 11.2.0.4.0 - Production on ?????? 7?? 23 20:07:52 2022
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
Export file created by EXPORT:V11.02.00 via conventional path
Warning: the objects were exported by BUS_HIS_TMP_NEW, not by you
import done in ZHS16GBK character set and AL16UTF16 NCHAR character set
IMP-00031: Must specify FULL=Y or provide FROMUSER/TOUSER or TABLES arguments
IMP-00000: Import terminated unsuccessfully
复制
从报错信息我们获取到如下重要信息:
dmp文件是由 V11.02.00 导出的
导出的用户为 BUS_HIS_TMP_NEW
同时报 IMP-00031 错误,提示导出命令必须指定以下参数中的其中之一,并且不能同时使用:
FULL=Y
FROMUSER/TOUSER
TABLES
当指定 FULL=Y 时,imp 会将对象导入到当前 imp 命令登录的用户,比如我们当前使用的是 sys 用户,则对象会导入到 sys 用户下。
$ imp \'/ as sysdba\' file=1.dmp full=y
Import: Release 11.2.0.4.0 - Production on ?????? 7?? 23 20:08:25 2022
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
Export file created by EXPORT:V11.02.00 via conventional path
Warning: the objects were exported by BUS_HIS_TMP_NEW, not by you
import done in ZHS16GBK character set and AL16UTF16 NCHAR character set
. importing BUS_HIS_TMP_NEW's objects into SYS
. importing BUS_HIS_TMP_NEW's objects into SYS
. . importing table "INP_CASE_FIRSTPAGE" 105118 rows imported
. . importing table "INP_SETTLE_DETAIL"^C^C
^C^C^C
^C
^C^C^C
IMP-00058: ORACLE error 1013 encountered
ORA-01013: ??????????????????
IMP-00028: partial import of previous table rolled back: 3913784 rows rolled back
. . importing table "INP_SETTLE_MASTER"\ 105118 rows imported
. . importing table "OUTP_SETTLE_DETAIL"^C^C^C^C^C
^C^C^C
IMP-00058: ORACLE error 1013 encountered
ORA-01013: ??????????????????
IMP-00028: partial import of previous table rolled back: 205768 rows rolled back
. . importing table "OUTP_SETTLE_MASTER"^C^C^C^C
IMP-00058: ORACLE error 1013 encountered
ORA-01013: ??????????????????
IMP-00028: partial import of previous table rolled back: 88880 rows rolled back
Import terminated successfully with warnings.
复制
如果发现不想导入到当前用户,此时使用 Ctrl+C 可以中止导入,已经导入的对象会做回退操作,数据不会导入到用户中。
此时正确做法是指定 FROMUSER/TOUSER ,将原用户对象导入到新的用户下,比如:
$ imp \'/ as sysdba\' fromuser=BUS_HIS_TMP_NEW touser=XYHIS file=1.dmp
Import: Release 11.2.0.4.0 - Production on 星期六 7月 23 20:11:53 2022
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
Export file created by EXPORT:V11.02.00 via conventional path
Warning: the objects were exported by BUS_HIS_TMP_NEW, not by you
import done in ZHS16GBK character set and AL16UTF16 NCHAR character set
. importing BUS_HIS_TMP_NEW's objects into XYHIS
. . importing table "INP_CASE_FIRSTPAGE" 105118 rows imported
. . importing table "INP_SETTLE_DETAIL" 27005651 rows imported
. . importing table "INP_SETTLE_MASTER" 105118 rows imported
. . importing table "OUTP_SETTLE_DETAIL" 4265908 rows imported
. . importing table "OUTP_SETTLE_MASTER" 1641721 rows imported
Import terminated successfully without warnings.
复制
如果使用 sys 账户登录数据库可以使用操作系统认证,从而不需要输入密码。如果使用的是其它具有导入权限的用户,则需要输入用户名和密码。在使用 FROMUSER/TOUSER 参数时不能与 FULL=Y 参数一起使用,如果强行使用会报 IMP-00024 错误。
IMP-00024: Only one mode (TABLES, FROMUSER, or FULL) can be specified
复制
全文完。
如果转发本文,文末务必注明:“转自微信公众号:生有可恋”。