ITPub博客

首页 > 数据库 > Oracle > 【转】因授权信息丢失导致IMP时出现IMP-00041错误的模拟与分析 随缘

【转】因授权信息丢失导致IMP时出现IMP-00041错误的模拟与分析 随缘

Oracle 作者:JPI_WJM 时间:2012-09-14 08:38:37 0 删除 编辑

问题现象是这样的,在IMP数据的过程中出现“IMP-00041: Warning: object created with compilation warnings”错误,因这个错误导致视图导入后无法使用。经分析,发现此问题与“授权信息丢失”有关。
为避免朋友们走弯路,我模拟再现一下这个问题,同时给出一个分析和解决问题的思路。

1.创建两个用户sec1和sec2
sys@ora10g> create user sec1 identified by sec1 default tablespace TBS_SEC_D;

User created.

sys@ora10g> grant connect,resource to sec1;

Grant succeeded.

sys@ora10g> create user sec2 identified by sec2 default tablespace TBS_SEC_D;

User created.

sys@ora10g> grant connect,resource to sec2;

Grant succeeded.

2.在第一个用户sec1中创建表T_SEC1
sys@ora10g> conn sec1/sec1
Connected.
sec1@ora10g> create table t_sec1 (x int);

Table created.

sec1@ora10g> insert into t_sec1 values (1);

1 row created.

3.在第二个用户sec2中创建表T_SEC2
sec1@ora10g> conn sec2/sec2
Connected.
sec2@ora10g> create table t_sec2 (x int);

Table created.

sec2@ora10g> insert into t_sec2 values (2);

1 row created.

4.在sec2中创建sec1用户中t_sec1表的同名
sec2@ora10g> conn / as sysdba
Connected.
sys@ora10g> create synonym sec2.syn_t_sec1 for sec1.t_sec1;

Synonym created.

sys@ora10g> grant insert,delete,update,select on sec1.t_sec1 to sec2;

Grant succeeded.

5.在sec2用户下创建视图v_sec2
该视图同时使用到刚刚在sec2用户下创建的同名SYN_T_SEC1和表T_SEC2。
sec2@ora10g> conn / as sysdba
Connected.
sys@ora10g>
sys@ora10g> grant create view to sec2;

Grant succeeded.

sys@ora10g> conn sec2/sec2
Connected.
sec2@ora10g> create view v_sec2 as select SYN_T_SEC1.x sec1_x, T_SEC2.x sec2_x from SYN_T_SEC1,T_SEC2;

View created.

6.分别看一下各个用户下的数据库对象
sec2@ora10g> conn sec1/sec1
Connected.
sec1@ora10g> select * from cat;

TABLE_NAME TABLE_TYPE
------------------------------ -----------
T_SEC1 TABLE


sec2@ora10g> select * from cat;

TABLE_NAME TABLE_TYPE
------------------------------ -----------
T_SEC2 TABLE
SYN_T_SEC1 SYNONYM
V_SEC2 VIEW

7.我们这里分别生成sec1和sec2用户的备份
ora10g@secDB1 /exp$ exp sec1/sec1 file=sec1.dmp log=sec1.log

Export: Release 10.2.0.3.0 - Production on Fri Feb 26 03:01:40 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SEC1
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SEC1
About to export SEC1's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export SEC1's tables via Conventional Path ...
. . exporting table T_SEC1 1 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.

ora10g@secDB1 /exp$ exp sec2/sec2 file=sec2.dmp log=sec2.log

Export: Release 10.2.0.3.0 - Production on Fri Feb 26 03:01:56 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SEC2
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SEC2
About to export SEC2's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export SEC2's tables via Conventional Path ...
. . exporting table T_SEC2 1 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.


8.将备份文件发送到待导入的服务器secDB2
ora10g@secDB1 /exp$ scp sec1.dmp sec2.dmp 172.17.193.201:/imp
oracle@172.17.193.201's password:
sec1.dmp 100% 16KB 16.0KB/s 00:00
sec2.dmp 100% 16KB 16.0KB/s 00:00

9.在secDB2服务器上创建同样的用户,并进行导入测试
1)创建用户并授权
sys@ora10g> create user sec1 identified by sec1 default tablespace TBS_SEC_D;

User created.

sys@ora10g> grant connect,resource to sec1;

Grant succeeded.

sys@ora10g> create user sec2 identified by sec2 default tablespace TBS_SEC_D;

User created.

sys@ora10g> grant connect,resource,dba to sec2;

Grant succeeded.

2)导入测试
(1)先导入sec1用户
ora10g@secDB2 /imp$ imp sec1/sec1 file=sec1.dmp log=sec1.log ignore=y full=y

Import: Release 10.2.0.3.0 - Production on Fri Feb 26 11:58:09 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning and Data Mining options

Export file created by EXPORT:V10.02.01 via conventional path
import done in AL32UTF8 character set and UTF8 NCHAR character set
. importing SEC1's objects into SEC1
. . importing table "T_SEC1" 1 rows imported
Import terminated successfully without warnings.


(2)导入sec2用户
ora10g@secDB2 /imp$ imp sec2/sec2 file=sec2.dmp log=sec2.log ignore=y full=y

Import: Release 10.2.0.3.0 - Production on Fri Feb 26 21:14:59 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning and Data Mining options

Export file created by EXPORT:V10.02.01 via conventional path
import done in AL32UTF8 character set and UTF8 NCHAR character set
. importing SEC2's objects into SEC2
. . importing table "T_SEC2" 1 rows imported
Import terminated successfully without warnings.


此时是导入成功的!演示还未结束,请继续。

(3)假如此时删除sec2用户(删除的目的可能有很多,比如之前授予的权限不足等),对其进行重新创建后再完成数据导入
sys@
ora10g> drop user sec2 cascade;

User dropped.

sys@
ora10g> create user sec2 identified by sec2 default tablespace TBS_SEC_D;

User created.

sys@
ora10g> grant connect,resource,dba to sec2;

Grant succeeded.

(4)再重新完成sec2用户的IMP导入
ora10g@secDB2 /imp$ imp sec2/sec2 file=sec2.dmp log=sec2.log ignore=y full=y

Import: Release 10.2.0.3.0 - Production on Fri Feb 26 21:20:48 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning and Data Mining options

Export file created by EXPORT:V10.02.01 via conventional path
import done in AL32UTF8 character set and UTF8 NCHAR character set
. importing SEC2's objects into SEC2
. . importing table "T_SEC2" 1 rows imported
IMP-00041: Warning: object created with compilation warnings
"CREATE FORCE VIEW "SEC2"."V_SEC2" ("SEC1_X","SEC"
"2_X") AS "
"select SYN_T_SEC1.x sec1_x, T_SEC2.x sec2_x from SYN_T_SEC1,T_SEC2"
Import terminated successfully with warnings.


此时,问题出现了,此时的“IMP-00041”错误提示内容是创建的视图存在编译错误(这个错误没有太大的指导意义)。

10.验证被导入的内容是否可用
1)导入了三个对象,没有问题。
sec2@ora10g> select * from cat;

TABLE_NAME TABLE_TYPE
------------------------------ -----------
SYN_T_SEC1 SYNONYM
T_SEC2 TABLE
V_SEC2 VIEW

2)T_SEC2表可用
sec2@ora10g> select * from T_SEC2;

X
----------
2

3)SYN_T_SEC1同名可用
sec2@ora10g> select * from SYN_T_SEC1;

X
----------
1

4)此时视图不可用,提示存在错误。原因不详细。
sec2@ora10g> select * from V_SEC2;
select * from V_SEC2
*
ERROR at line 1:
ORA-04063: view "SEC2.V_SEC2" has errors

5)尝试重新编译,无效。
sec2@ora10g> alter view V_SEC2 compile;

Warning: View altered with compilation errors.

6)没有具体的错误提示信息
sec2@ora10g> show errors;
No errors.

11.问题原因
根本原因在于,当删除sec2用户重新创建后,sec2用户原来具有的sec1用户下T_SEC1表授权信息丢失了。
不要着急,我们来分析一下。
在完成sec1用户导入后,其实sec1用户的dmp文件中包含的授权信息已经完成对sec2用户的授权。我们使用“show=y”选项查看一下sec1用户的dmp文件内容。
ora10g@secDB2 /imp$ imp sec1/sec1 file=sec1.dmp log=sec1.log ignore=y full=y show=y

Import: Release 10.2.0.3.0 - Production on Fri Feb 26 21:24:50 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning and Data Mining options

Export file created by EXPORT:V10.02.01 via conventional path
import done in AL32UTF8 character set and UTF8 NCHAR character set
. importing SEC1's objects into SEC1
"BEGIN "
"sys.dbms_logrep_imp.instantiate_schema(schema_name=>SYS_CONTEXT('USERENV','"
"CURRENT_SCHEMA'), export_db_name=>'ORA10G', inst_scn=>'36699433');"
"COMMIT; END;"
"CREATE TABLE "T_SEC1" ("X" NUMBER(*,0)) PCTFREE 10 PCTUSED 40 INITRANS 1 M"
"AXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL"
" DEFAULT) TABLESPACE "TBS_SEC_D" LOGGING NOCOMPRESS"
. . skipping table "T_SEC1"

"GRANT DELETE ON "T_SEC1" TO "SEC2""
"GRANT INSERT ON "T_SEC1" TO "SEC2""
"GRANT SELECT ON "T_SEC1" TO "SEC2""
"GRANT UPDATE ON "T_SEC1" TO "SEC2""
Import terminated successfully without warnings.


注意最后四行的授权信息。这些授权信息是在sec1用户数据导入过程中同时完成的。

12.问题处理
既然知道了问题的出处,处理就简单了。以sys用户显示的将sec1用户下t_sec1表的操作权限授予sec2用户,然后再重新对问题视图进行编译(当然,重新创建这个视图亦可)。
sec2@ora10g> conn / as sysdba
Connected.
sys@ora10g> grant insert,delete,update,select on sec1.t_sec1 to sec2;

Grant succeeded.

sys@ora10g> conn sec2/sec2
Connected.
sec2@ora10g> alter view V_SEC2 compile;

View altered.

sec2@ora10g> select * from V_SEC2;

SEC1_X SEC2_X
---------- ----------
1 2

1 row selected.

OK,问题到此处理完毕。

13.另外一种导致这个问题的场景演示
还用一种可能出现这种问题的可能性,如果是按照下面的顺序在secDB2服务器上完成用户的创建和导入,一样会报上面的错误。
创建sec1用户,完成对sec1用户的导入;
创建sec2用户,完成对sec2用户的导入。

因为在完成sec1用户导入后,授权信息的授予对象sec2还不存在!

为保证信息的完整性和正确性,赘述在此。
1)删除sec1和sec2用户
sys@
ora10g> drop user sec1 cascade;

User dropped.

sys@
ora10g> drop user sec2 cascade;

User dropped.

2)创建sec1用户,完成对sec1用户的导入
sys@
ora10g> create user sec1 identified by sec1 default tablespace TBS_SEC_D;

User created.

sys@
ora10g> grant connect,resource to sec1;

Grant succeeded.

sys@
ora10g> exit

ora10g@secDB2 /imp$ imp sec1/sec1 file=sec1.dmp log=sec1.log ignore=y full=y

Import: Release 10.2.0.3.0 - Production on Fri Feb 26 21:47:11 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning and Data Mining options

Export file created by EXPORT:V10.02.01 via conventional path
import done in AL32UTF8 character set and UTF8 NCHAR character set
. importing SEC1's objects into SEC1
. . importing table "T_SEC1" 1 rows imported
IMP-00017: following statement failed with ORACLE error 1917:
"GRANT DELETE ON "T_SEC1" TO "SEC2""
IMP-00003: ORACLE error 1917 encountered
ORA-01917: user or role 'SEC2' does not exist
IMP-00017: following statement failed with ORACLE error 1917:
"GRANT INSERT ON "T_SEC1" TO "SEC2""
IMP-00003: ORACLE error 1917 encountered
ORA-01917: user or role 'SEC2' does not exist
IMP-00017: following statement failed with ORACLE error 1917:
"GRANT SELECT ON "T_SEC1" TO "SEC2""
IMP-00003: ORACLE error 1917 encountered
ORA-01917: user or role 'SEC2' does not exist
IMP-00017: following statement failed with ORACLE error 1917:
"GRANT UPDATE ON "T_SEC1" TO "SEC2""
IMP-00003: ORACLE error 1917 encountered
ORA-01917: user or role 'SEC2' does not exist
Import terminated successfully with warnings.


此处的授权信息执行失败的提示信息已经说明了问题。

3)创建sec2用户,完成对sec2用户的导入
ora10g@secDB2 /imp$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.3.0 - Production on Fri Feb 26 21:47:43 2010

Copyright (c) 1982, 2006, Oracle. All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning and Data Mining options

sys@
ora10g> create user sec2 identified by sec2 default tablespace TBS_SEC_D;

User created.

sys@
ora10g> grant connect,resource,dba to sec2;

Grant succeeded.

sys@
ora10g> exit

ora10g@secDB2 /imp$ imp sec2/sec2 file=sec2.dmp log=sec2.log ignore=y full=y

Import: Release 10.2.0.3.0 - Production on Fri Feb 26 21:48:17 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning and Data Mining options

Export file created by EXPORT:V10.02.01 via conventional path
import done in AL32UTF8 character set and UTF8 NCHAR character set
. importing SEC2's objects into SEC2
. . importing table "T_SEC2" 1 rows imported
IMP-00041: Warning: object created with compilation warnings
"CREATE FORCE VIEW "SEC2"."V_SEC2" ("SEC1_X","SEC"
"2_X") AS "
"select SYN_T_SEC1.x sec1_x, T_SEC2.x sec2_x from SYN_T_SEC1,T_SEC2"
Import terminated successfully with warnings.


问题又一次再现,错误原因与我们前面分析的结果相同。

14.小结
我们使用EXP/IMP工具在不同用户间存在较复杂的授权关系的情况下完成数据迁移时,需要特别注意他们的先后顺序。
为避免此类错误的发生,建议在多用户数据迁移场景下,使用sys用户一次性完成(使用OWNER参数)数据迁移工作。

 

 

 

注:很感谢这篇文章的原著作者,在日常处理此类问题上很有帮助!

<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22332250/viewspace-1115516/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2009-08-13