共计 5861 个字符,预计需要花费 15 分钟才能阅读完成。
这篇文章主要为大家展示了“Oracle 中如何使用 DBMS_REPAIR 修复坏块”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让丸趣 TV 小编带领大家一起研究并学习一下“Oracle 中如何使用 DBMS_REPAIR 修复坏块”这篇文章吧。
对于 Oracle 数据块物理损坏的情形,在我们有备份的情况下可以直接使用备份来恢复。对于通过备份恢复,Oracel 为我们提供了很多种方式,冷备,基于用户管理方式,RMAN 方式等等。对于这几种方式我们需要实现基于数据库以及文件级别的恢复。RMAN 同时也提供了基于块介质方式的恢复。也就是说我们根本不需要还原数据文件,而是直接从备份文件基于块来提取以实现联机恢复。可参考基于 RMAN 实现坏块介质恢复 (blockrecover) 。这是比较理想的情形。如果没有任何备份怎么办?我们可以使用 Oracle 自带的 DBMS_REPAIR 包来实现修复。注意,不要被文章题目有所误导。这里的修复是有损修复也就是说将受损的数据块标记为坏块,不对其进行访问而已。就好比我们磁盘有坏道,找个磁盘修复工具将坏道标出来不使用,同理。那受损的数据岂不是无力回天啦,呜呜 …… 要记得随时备份阿。
1、DBMS_REPAIR 包所含的过程
Procedure_Name Description
—————– ————————————
ADMIN_TABLES Provides administrative functions (create, drop, purge) for repair or orphan key tables.
Note: These tables are always created in the SYS schema.
CHECK_OBJECT Detects and reports corruptions in a table or index
DUMP_ORPHAN_KEYS Reports on index entries that point to rows in corrupt data blocks
FIX_CORRUPT_BLOCKS Marks blocks as software corrupt that have been previously identified as corrupt by the CHECK_OBJECT procedure
REBUILD_FREELISTS Rebuilds the free lists of the object
SEGMENT_FIX_STATUS Provides the capability to fix the corrupted state of a bitmap entry when segment space management is AUTO
SKIP_CORRUPT_BLOCKS When used, ignores blocks marked corrupt during table and index scans.
If not used, you get error ORA-01578 when encountering blocks marked corrupt.
2、DBMS_REPAIR 的一些局限性
Tables with LOB data types, nested tables, and varrays are supported, but the out-of-line columns are ignored.
Clusters are supported in the SKIP_CORRUPT_BLOCKS and REBUILD_FREELISTS procedures, but not in the CHECK_OBJECT procedure.
Index-organized tables and LOB indexes are not supported.
The DUMP_ORPHAN_KEYS procedure does not operate on bitmap indexes or function-based indexes.
The DUMP_ORPHAN_KEYS procedure processes keys that are no more than 3,950 bytes long.
3. 创建演示环境
– 创建表空间
SQL show user;
USER is SCOTT
SQL create tablespace tbs_tmp datafile /u01/app/oracle/oradata/DBdb/tbs_tmp.dbf size 10m autoextend on;
Tablespace created.
– 创建表对象 tb_obj 及其索引
SQL create table tb_obj tablespace tbs_tmp as select * from dba_objects;
Table created.
SQL create index i_tb_obj on tb_obj(object_id);
Index created.
– 表段上的相关信息
SQL select segment_name , header_file , header_block,blocks from dba_segments where segment_name = TB_OBJ
SEGMENT_NAME HEADER_FILE HEADER_BLOCK BLOCKS
——————————————————————————— ———– ———— ———-
TB_OBJ 6 130 1280
– 使用 linux 自带的 dd 命令来损坏数据块
[oracle@wang ~]$ dd of=/u01/app/oracle/oradata/DBdb/tbs_tmp.dbf bs=8192 conv=notrunc seek=131 eof
Corrupt me!
EOF
0+1 records in
0+1 records out
13 bytes (13 B) copied, 0.000117639 s, 111 kB/s
[oracle@wang ~]$
– 下面的查询收到了错误提示
SQL select count(*) from tb_obj;
select count(*) from tb_obj
*
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 6, block # 131)
ORA-01110: data file 6: /u01/app/oracle/oradata/DBdb/tbs_tmp.dbf
SQL
4. 使用 DBMS_REPAIR 修复坏块
Step a 创建相应的表对象 :
– 使用 DBMS_REPAIR.ADMIN_TABLES 过程创建一个表对象,用于记录需要被修复的表
SQL BEGIN
DBMS_REPAIR.ADMIN_TABLES (
TABLE_NAME = REPAIR_TABLE ,
TABLE_TYPE = dbms_repair.repair_table,
ACTION = dbms_repair.create_action,
TABLESPACE = USERS
END;
/
PL/SQL procedure successfully completed.
SQL
– 使用 DBMS_REPAIR.ADMIN_TABLES 过程创建一个表对象,用于记录在表块损坏后那些孤立索引,也就是指向坏块的那些索引
SQL BEGIN
DBMS_REPAIR.ADMIN_TABLES
(
TABLE_NAME = ORPHAN_KEY_TABLE ,
TABLE_TYPE = DBMS_REPAIR.ORPHAN_TABLE,
ACTION = DBMS_REPAIR.CREATE_ACTION,
TABLESPACE = USERS
);
END;
/
PL/SQL procedure successfully completed.
Step b 校验受损的对象 :
– 使用 DBMS_REPAIR.CHECK_OBJECT 来检测对象上受损的情形,并返回受损块数
SQL SET SERVEROUTPUT ON
SQL DECLARE num_corrupt INT;
BEGIN
num_corrupt := 0;
DBMS_REPAIR.CHECK_OBJECT (
SCHEMA_NAME = SCOTT ,
OBJECT_NAME = TB_OBJ ,
REPAIR_TABLE_NAME = REPAIR_TABLE ,
CORRUPT_COUNT = num_corrupt);
DBMS_OUTPUT.PUT_LINE(number corrupt: || TO_CHAR (num_corrupt));
END;
/
number corrupt: 1
PL/SQL procedure successfully completed.
– 下面我们可以从 repair_table 查询到受损的块
– 从下面的查询中可以看出列 marked_corrupt 全部为 true,表明我们在 CHECK_OBJECT 已经标注了坏块
COLUMN object_name FORMAT a10
COLUMN repair_description FORMAT a28
SET LINES 10000
SELECT object_name, block_id, corrupt_type,marked_corrupt,repair_description FROM repair_table;
OBJECT_NAM BLOCK_ID CORRUPT_TYPE MARKED_COR REPAIR_DESCRIPTION
———- ———- ———— ———- —————————-
TB_OBJ 131 6148 TRUE mark block software corrupt
Step c 标记坏块
– 过程 FIX_CORRUPT_BLOCKS 用于标记坏块,在这个演示中,我们在 CHECK_OBJECT 已经被标注了,如没有执行下面的过程
– 由于上一步已经标注,所以下面的输出为 0
SQL SET SERVEROUTPUT ON
SQL DECLARE num_fix INT;
BEGIN
num_fix := 0;
DBMS_REPAIR.FIX_CORRUPT_BLOCKS (
SCHEMA_NAME = SCOTT ,
OBJECT_NAME= TB_OBJ ,
OBJECT_TYPE = dbms_repair.table_object,
REPAIR_TABLE_NAME = REPAIR_TABLE ,
FIX_COUNT= num_fix);
DBMS_OUTPUT.PUT_LINE(num fix: || TO_CHAR(num_fix));
END;
/
num fix: 0
PL/SQL procedure successfully completed.
Step d DUMP 孤立的索引键值
– 使用 DUMP_ORPHAN_KEYS 过程将那些指向坏块的索引键值填充到 ORPHAN_KEY_TABLE,I_TB_OBJ 索引名
SQL SET SERVEROUTPUT ON
SQL DECLARE num_orphans INT;
BEGIN
num_orphans := 0;
DBMS_REPAIR.DUMP_ORPHAN_KEYS (
SCHEMA_NAME = SCOTT ,
OBJECT_NAME = I_TB_OBJ ,
OBJECT_TYPE = dbms_repair.index_object,
REPAIR_TABLE_NAME = REPAIR_TABLE ,
ORPHAN_TABLE_NAME= ORPHAN_KEY_TABLE ,
KEY_COUNT = num_orphans);
DBMS_OUTPUT.PUT_LINE(orphan key count: || TO_CHAR(num_orphans));
END;
/
orphan key count: 88
PL/SQL procedure successfully completed.
– 下面的查询可以看到正好等于上面返回的数量也就是 88 条记录
SQL select count(*) from orphan_key_table;
COUNT(*)
———-
88
– 验证对象是否可以查询,下面的结果显示依旧无法查询
SQL show user
USER is SCOTT
SQL SELECT COUNT(*) FROM TB_OBJ;
SELECT COUNT(*) FROM TB_OBJ
*
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 6, block # 131)
ORA-01110: data file 6: /u01/app/oracle/oradata/DBdb/tbs_tmp.dbf
SQL
Step e 跳过坏块
– 使用 SKIP_CORRUPT_BLOCKS 来告知 Oracle 哪些坏块需要被跳过
SQL BEGIN
DBMS_REPAIR.SKIP_CORRUPT_BLOCKS (
SCHEMA_NAME = SCOTT ,
OBJECT_NAME = TB_OBJ ,
OBJECT_TYPE = dbms_repair.table_object,
FLAGS = dbms_repair.skip_flag);
END;
/
PL/SQL procedure successfully completed.
– 由于索引键上存在孤立索引,因此我们重建索引
SQL alter index scott.i_tb_obj rebuild;
Index altered.
– 验证结果
SQL show user
USER is SCOTT
SQL
SQL select count(*) from tb_obj;
COUNT(*)
———-
86952
SQL
以上是“Oracle 中如何使用 DBMS_REPAIR 修复坏块”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注丸趣 TV 行业资讯频道!