客户提供一个dmp文件,需要导入到其他数据库中,但是导入过程,抛出如下错误:
经检查,是因为dmp文件是从12c环境导出,而目标环境是11g,版本不兼容。
我们知道,考虑了同以前版本的兼容,从低版本数据库exp数据,导入至高版本数据库是没什么问题的,但是从高本数据库exp导出数据,导入至低版本数据库会遇到问题,我们该如何解决?我们通过实验验证一下。
实验环境:
低版本: 11.2.0.4
高版本:19.3.0.0
1.1 低版本到高版本
1.1.1 实验
–1) 源端11204版本 创建测试表,并导出
sql> create table table_11204 as select * from dba_objects;
table created.
[oracle@ora11204 ~]$ exp sxc/sxc file=table_11204.dmp tables=table_11204 log=table_11204_exp.log
export: release 11.2.0.4.0 - production on tue may 9 01:25:23 2023
米乐app官网下载 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 done in zhs16gbk character set and al16utf16 nchar character set
server uses al32utf8 character set (possible charset conversion)
about to export specified tables via conventional path ...
. . exporting table table_11204 78981 rows exported
export terminated successfully without warnings.
–2) 将dmp文件拷贝到目标端
[oracle@ora11204 ~]$ scp table_11204.dmp oracle@172.17.0.3:/home/oracle oracle@172.17.0.3's password: table_11204.dmp 100% 8920kb 8.7mb/s 00:00
–3)目标端19c版本 导入dmp
[oracle@ora19c ~]$ imp sxc/sxc file=table_11204.dmp tables=table_11204 log=table_11204_imp.log fromuser=sxc touser=sxc
import: release 19.0.0.0.0 - production on tue may 9 10:20:21 2023
version 19.3.0.0.0
米乐app官网下载 copyright (c) 1982, 2019, oracle and/or its affiliates. all rights reserved.
connected to: oracle database 19c enterprise edition release 19.0.0.0.0 - production
version 19.3.0.0.0
export file created by export:v11.02.00 via conventional path
import done in zhs16gbk character set and utf8 nchar character set
import server uses al32utf8 character set (possible charset conversion)
export server uses al16utf16 nchar character set (possible ncharset conversion)
imp-00403:
warning: this import generated a separate sql file "table_11204_imp_sys.sql" which contains ddl that failed due to a privilege issue.
. importing sxc's objects into sxc
. . importing table "table_11204" 78981 rows imported
import terminated successfully with warnings.
结论:从低版本导出,高版本导入,成功。
1.2 高版本到低版本
1.2.1 实验
–1) 源端19c版本 创建测试表,并导出
sql> create table table_19c as select * from dba_objects;
table created.
[oracle@ora19c ~]$ exp sxc/sxc file=table_19c.dmp tables=table_19c log=table_19c_exp.log
export: release 19.0.0.0.0 - production on tue may 9 10:23:32 2023
version 19.3.0.0.0
米乐app官网下载 copyright (c) 1982, 2019, oracle and/or its affiliates. all rights reserved.
connected to: oracle database 19c enterprise edition release 19.0.0.0.0 - production
version 19.3.0.0.0
export done in zhs16gbk character set and utf8 nchar character set
server uses al32utf8 character set (possible charset conversion)
about to export specified tables via conventional path ...
. . exporting table table_19c 72375 rows exported
exp-00091: exporting questionable statistics.
export terminated successfully with warnings.
–2) 将dmp文件拷贝到目标端
[oracle@ora19c ~]$ scp table_19c.dmp oracle@172.17.0.2:/home/oracle oracle@172.17.0.2's password: table_19c.dmp 100% 11mb 127.2mb/s 00:00
–3)目标端11.2.0.4版本 导入dmp
[oracle@ora11204 ~]$ imp sxc/sxc file=table_19c.dmp tables=table_19c log=table_19c_imp.log fromuser=sxc touser=sxc
import: release 11.2.0.4.0 - production on tue may 9 02:25:16 2023
米乐app官网下载 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
imp-00010: not a valid export file, header failed verification
imp-00000: import terminated unsuccessfully
结论:从高版本导出,低版本导入,报错,失败。
1.2.2 解决办法
使用低版本客户端,通过tns,连接高版本数据库,然后用exp导出。
--1) 通过11.2.0.4客户端,通过tns,连接到19c数据库,并导出
[oracle@ora11204 ~]$ exp sxc/sxc@ora19c file=table_19c_2.dmp tables=table_19c log=table_19c_exp2.log
export: release 11.2.0.4.0 - production on tue may 9 02:36:50 2023
米乐app官网下载 copyright (c) 1982, 2011, oracle and/or its affiliates. all rights reserved.
connected to: oracle database 19c enterprise edition release 19.0.0.0.0 - production
export done in zhs16gbk character set and utf8 nchar character set
server uses al32utf8 character set (possible charset conversion)
about to export specified tables via conventional path ...
. . exporting table table_19c 72375 rows exported
exp-00091: exporting questionable statistics.
export terminated successfully with warnings.
--2)低版数据库再次导入,成功
[oracle@ora11204 ~]$ imp sxc/sxc file=table_19c_2.dmp tables=table_19c log=table_19c_imp.log fromuser=sxc touser=sxc
import: release 11.2.0.4.0 - production on tue may 9 02:39:08 2023
米乐app官网下载 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
import done in zhs16gbk character set and al16utf16 nchar character set
import server uses al32utf8 character set (possible charset conversion)
export server uses utf8 nchar character set (possible ncharset conversion)
. importing sxc's objects into sxc
. . importing table "table_19c" 72375 rows imported
import terminated successfully without warnings.
思考? 如果使用高版本客户端,通过tns 连接低版本数据库,然后用imp导入,可以吗?
-- 这里我们继续使用从19c直接导出的dmp文件,然后通过19c客户端连接到11g数据库,做导入
[oracle@ora19c ~]$ imp sxc/sxc@ora11204 file=table_19c.dmp tables=table_19c log=table_19c_imp.log fromuser=sxc touser=sxc
import: release 19.0.0.0.0 - production on tue may 9 10:33:17 2023
version 19.3.0.0.0
米乐app官网下载 copyright (c) 1982, 2019, 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:v19.00.00 via conventional path
import done in zhs16gbk character set and al16utf16 nchar character set
import server uses al32utf8 character set (possible charset conversion)
export server uses utf8 nchar character set (possible ncharset conversion)
imp-00003: oracle error 942 encountered
ora-00942: table or view does not exist
imp-00023: import views not installed, please notify your dba
imp-00000: import terminated unsuccessfully
结论:导入仍然报错,这种方式不可行。
2.1 低版本到高版本
2.1.1 实验
–1) 源端11204版本导出
sql> create directory dir_exp as '/home/oracle/dmp';
directory created.
sql> grant read,write on directory dir_exp to public;
grant succeeded.
[oracle@ora11204 dmp]$ expdp sxc/sxc directory=dir_exp dumpfile=expdp_table_11204.dmp tables=table_11204 log=expdp_table_11204.log
export: release 11.2.0.4.0 - production on tue may 9 04:59:27 2023
米乐app官网下载 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
legacy mode active due to the following parameters:
legacy mode parameter: "log=expdp_table_11204.log" location: command line, replaced with: "logfile=expdp_table_11204.log"
legacy mode has set reuse_dumpfiles=true parameter.
starting "sxc"."sys_export_table_01": sxc/******** directory=dir_exp dumpfile=expdp_table_11204.dmp tables=table_11204 logfile=expdp_table_11204.log reuse_dumpfiles=true
estimate in progress using blocks method...
processing object type table_export/table/table_data
total estimation using blocks method: 10 mb
processing object type table_export/table/table
. . exported "sxc"."table_11204" 7.657 mb 78981 rows
master table "sxc"."sys_export_table_01" successfully loaded/unloaded
******************************************************************************
dump file set for sxc.sys_export_table_01 is:
/home/oracle/dmp/expdp_table_11204.dmp
job "sxc"."sys_export_table_01" successfully completed at tue may 9 04:59:38 2023 elapsed 0 00:00:08
–2) 将dmp文件拷贝到目标端
[oracle@ora11204 dmp]$ scp expdp_table_11204.dmp oracle@172.17.0.3:/home/oracle/dmp oracle@172.17.0.3's password: expdp_table_11204.dmp 100% 7940kb 7.8mb/s 00:00
–3)目标端19c版本 导入dmp
-- 因为目标环境表已经存在,这里加上参数 table_exists_action=replace
[oracle@ora19c dmp]$ impdp sxc/sxc directory=dir_exp dumpfile=expdp_table_11204.dmp tables=table_11204 log=impdp_table_11204.log table_exists_action=replace
import: release 19.0.0.0.0 - production on tue may 9 13:03:10 2023
version 19.3.0.0.0
米乐app官网下载 copyright (c) 1982, 2019, oracle and/or its affiliates. all rights reserved.
connected to: oracle database 19c enterprise edition release 19.0.0.0.0 - production
legacy mode active due to the following parameters:
legacy mode parameter: "log=impdp_table_11204.log" location: command line, replaced with: "logfile=impdp_table_11204.log"
master table "sxc"."sys_import_table_01" successfully loaded/unloaded
import done in al32utf8 character set and utf8 nchar character set
export done in al32utf8 character set and al16utf16 nchar character set
warning: possible data loss in character set conversions
starting "sxc"."sys_import_table_01": sxc/******** directory=dir_exp dumpfile=expdp_table_11204.dmp tables=table_11204 logfile=impdp_table_11204.log table_exists_action=replace
processing object type table_export/table/table
processing object type table_export/table/table_data
. . imported "sxc"."table_11204" 7.657 mb 78981 rows
job "sxc"."sys_import_table_01" successfully completed at tue may 9 13:03:22 2023 elapsed 0 00:00:07
结论:从低版本导出,高版本导入,成功。
2.2 高版本到低版本
2.2 .1 实验
–1) 源端19c版本 导出
[oracle@ora19c dmp]$ expdp sxc/sxc directory=dir_exp dumpfile=expdp_table_11204_19c.dmp tables=table_11204 log=expdp_table_11204.log
export: release 19.0.0.0.0 - production on tue may 9 13:04:26 2023
version 19.3.0.0.0
米乐app官网下载 copyright (c) 1982, 2019, oracle and/or its affiliates. all rights reserved.
connected to: oracle database 19c enterprise edition release 19.0.0.0.0 - production
legacy mode active due to the following parameters:
legacy mode parameter: "log=expdp_table_11204.log" location: command line, replaced with: "logfile=expdp_table_11204.log"
legacy mode has set reuse_dumpfiles=true parameter.
starting "sxc"."sys_export_table_01": sxc/******** directory=dir_exp dumpfile=expdp_table_11204_19c.dmp tables=table_11204 logfile=expdp_table_11204.log reuse_dumpfiles=true
processing object type table_export/table/table_data
processing object type table_export/table/statistics/table_statistics
processing object type table_export/table/statistics/marker
processing object type table_export/table/table
. . exported "sxc"."table_11204" 7.657 mb 78981 rows
master table "sxc"."sys_export_table_01" successfully loaded/unloaded
******************************************************************************
dump file set for sxc.sys_export_table_01 is:
/home/oracle/dmp/expdp_table_11204_19c.dmp
job "sxc"."sys_export_table_01" successfully completed at tue may 9 13:04:53 2023 elapsed 0 00:00:23
–2) 将dmp文件拷贝到目标端
[oracle@ora19c dmp]$ scp expdp_table_11204_19c.dmp oracle@172.17.0.2:/home/oracle/dmp oracle@172.17.0.2's password: expdp_table_11204_19c.dmp 100% 8028kb 134.8mb/s 00:00
–3)目标端11.2.0.4版本 导入dmp
-- 因为目标环境表已经存在,这里加上参数 table_exists_action=replace
[oracle@ora11204 dmp]$ impdp sxc/sxc directory=dir_exp dumpfile=expdp_table_11204_19c.dmp tables=table_11204 log=impdp_table_11204.log table_exists_action=replace
import: release 11.2.0.4.0 - production on tue may 9 05:06:28 2023
米乐app官网下载 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-39001: invalid argument value
ora-39000: bad dump file specification
ora-39142: incompatible version number 5.1 in dump file "/home/oracle/dmp/expdp_table_11204_19c.dmp"
结论:从高版本导出,低版本导入,报错,失败。
2.2.2 解决办法
高版本导出时候,加上version参数,version=低版本数据库版本号
--1) 19c 导出时,加上version=11.2.0.4参数
[oracle@ora19c dmp]$ expdp sxc/sxc directory=dir_exp dumpfile=expdp_table_11204_19c_2.dmp tables=table_11204 log=expdp_table_11204.log version=11.2.0.4
export: release 19.0.0.0.0 - production on tue may 9 13:07:58 2023
version 19.3.0.0.0
米乐app官网下载 copyright (c) 1982, 2019, oracle and/or its affiliates. all rights reserved.
connected to: oracle database 19c enterprise edition release 19.0.0.0.0 - production
legacy mode active due to the following parameters:
legacy mode parameter: "log=expdp_table_11204.log" location: command line, replaced with: "logfile=expdp_table_11204.log"
legacy mode has set reuse_dumpfiles=true parameter.
warning: oracle data pump is exporting from a database that supports long identifiers to a version that does not support long identifiers.
starting "sxc"."sys_export_table_01": sxc/******** directory=dir_exp dumpfile=expdp_table_11204_19c_2.dmp tables=table_11204 logfile=expdp_table_11204.log version=11.2.0.4 reuse_dumpfiles=true
processing object type table_export/table/table_data
processing object type table_export/table/table
. . exported "sxc"."table_11204" 7.657 mb 78981 rows
master table "sxc"."sys_export_table_01" successfully loaded/unloaded
******************************************************************************
dump file set for sxc.sys_export_table_01 is:
/home/oracle/dmp/expdp_table_11204_19c_2.dmp
job "sxc"."sys_export_table_01" successfully completed at tue may 9 13:08:14 2023 elapsed 0 00:00:13
--2) 拷贝dmp文件至11g环境
[oracle@ora19c dmp]$ scp expdp_table_11204_19c_2.dmp oracle@172.17.0.2:/home/oracle/dmp
oracle@172.17.0.2's password:
expdp_table_11204_19c_2.dmp 100% 7996kb 101.9mb/s 00:00
--3) 11g环境再次导入dmp,成功
[oracle@ora11204 dmp]$ impdp sxc/sxc directory=dir_exp dumpfile=expdp_table_11204_19c_2.dmp tables=table_11204 log=impdp_table_11204.log table_exists_action=replace
import: release 11.2.0.4.0 - production on tue may 9 05:09:46 2023
米乐app官网下载 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
legacy mode active due to the following parameters:
legacy mode parameter: "log=impdp_table_11204.log" location: command line, replaced with: "logfile=impdp_table_11204.log"
master table "sxc"."sys_import_table_04" successfully loaded/unloaded
starting "sxc"."sys_import_table_04": sxc/******** directory=dir_exp dumpfile=expdp_table_11204_19c_2.dmp tables=table_11204 logfile=impdp_table_11204.log table_exists_action=replace
processing object type table_export/table/table
processing object type table_export/table/table_data
. . imported "sxc"."table_11204" 7.657 mb 78981 rows
job "sxc"."sys_import_table_04" successfully completed at tue may 9 05:09:50 2023 elapsed 0 00:00:03
方法1: 使用dbms_datapump.get_dumpfile_info()存储过程
10g 之后,我们可以通过dbms_datapump.get_dumpfile_info()存储过程来判断dmp文件是通过exp导出还是expdp导出。
dbms_datapump.get_dumpfile_info()存储过程可以从dump文件中获取信息filetype返回以下文件类型:
- 0:unknown
- 1:data pump dump file
- 2:original export dump file
这里,我们知道table_19c.dmp 是我们通过exp方式导出的,expdp_table_11204.dmp 是通过expdp方式导出的。那么如何通过上面的存储过程判断?
1)首先创建一个directory目录用于保存dmp文件
sql> create directory dir_exp as '/home/oracle/dmp';
directory created.
2)然后在sqlplus当中执行如下代码
sql> set serveroutput on
sql> declare
2 v_filetype number; -- 0=unknown 1=expdp 2=exp 3=ext
3 v_info_table sys.ku$_dumpfile_info; -- pl/sql table with file info
4 begin
5 dbms_datapump.get_dumpfile_info(filename => 'table_19c.dmp',
6 directory => upper('dir_exp'),
7 info_table => v_info_table,
8 filetype => v_filetype);
9 dbms_output.put_line('filetype : ' || v_filetype);
10 end;
11 /
filetype : 2
pl/sql procedure successfully completed.
sql> declare
2 v_filetype number; -- 0=unknown 1=expdp 2=exp 3=ext
3 v_info_table sys.ku$_dumpfile_info; -- pl/sql table with file info
4 begin
5 dbms_datapump.get_dumpfile_info(filename => 'expdp_table_11204.dmp',
6 directory => upper('dir_exp'),
7 info_table => v_info_table,
8 filetype => v_filetype);
9 dbms_output.put_line('filetype : ' || v_filetype);
10 end;
11 /
filetype : 1
pl/sql procedure successfully completed.
结论:可以看到,table_19c.dmp 返回 filetype : 2 ;expdp_table_11204.dmp 返回 filetype : 1。
注意:保证dmp文件的权限是正确,否则会抛出如下错误:
error at line 1: ora-39211: unable to retrieve dumpfile information as specified ora-06512: at "sys.dbms_sys_error", line 79 ora-06512: at "sys.dbms_datapump", line 5906 ora-31640: unable to open dump file "/home/oracle/dmp/expdp_table_11204_19c_2.dmp" for read ora-06512: at "sys.kupf$file", line 5956 ora-27041: unable to open file linux-x86_64 error: 13: permission denied additional information: 9 ora-06512: at "sys.kupf$file_int", line 287 ora-06512: at "sys.kupf$file", line 5642 ora-06512: at "sys.dbms_datapump", line 5898 ora-06512: at line 5
方法2:使用strings查看dmp文件信息
如果觉得上面的方法,比较麻烦,可以直接通过strings命令,查看dmp文件头信息来判断,但是上面的方法更加官方一点。
--1) 查看exp的dmp文件
[oracle@ora19c dmp]$ strings table_19c.dmp|more
texport:v19.00.00
dsxc
rtables
8192
tue may 9 10:23:33 2023table_19c.dmp
#g#g
#g#g
00:00
--2) 查看expdp的dmp文件
[oracle@ora19c dmp]$ strings expdp_table_11204.dmp |more
"sxc"."sys_export_table_01"
x86_64/linux 2.4.xx
lhr11g
al32utf8
11.02.00.04.00
001:001:000001:000001
结论:expdp的dmp文件,可以看到"sys_export_table_01" 关键信息,我们知道sys_export_table_01表是expdp导出过程中自动生成的,所以expdp_table_11204.dmp 可以判断是expdp导出的dmp文件。