欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Oracle数据块损坏之10231内部事件不完全恢复

程序员文章站 2022-04-03 20:33:46
什么是块损坏: 所谓损坏的数据块,是指块没有采用可识别的 oracle 格式,或者其内容在内部不一致。通常情况下,损坏是由硬件故障或操作系统问题引起的。oracle 数据...

什么是块损坏:

所谓损坏的数据块,是指块没有采用可识别的 oracle 格式,或者其内容在内部不一致。通常情况下,损坏是由硬件故障或操作系统问题引起的。oracle 数据库将损坏的块标识为“逻辑损坏”或“介质损坏”。如果是逻辑损坏,则是 oracle 内部错误。oracle 数据库检测到不一致之后,就将逻辑损坏的块标记为损坏。如果是介质损坏,则是块格式不正确;从磁盘读取的块不包含有意义的信息。实验:某个分区数据块损坏,不完全恢复此分区表数据。

 背景:数据库没有有效备份,某个分区中有数据块损坏。

 要求:最大限度恢复此分区数据。

 环境:rhel 6.4 + oracle 11.2.0.4

下面这篇文章主要给大家介绍了关于oracle数据块损坏之10231内部事件的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:

1. 初始化实验环境

初始化创建模拟实验环境用到的表空间、业务用户、表,并导入测试数据。

本次实验用到表空间dbs_d_jingyu, 业务用户jingyu, 分区表t_part(含两个分区的测试数据)。

-- 数据表空间
create tablespace dbs_d_jingyu datafile '/u02/oradata/jingyu/dbs_d_jingyu01.dbf' size 30m autoextend off;
-- 临时表空间
create temporary tablespace temp_jingyu tempfile '/u02/oradata/jingyu/temp_jingyu01.tmp' size 30m autoextend off;
-- 索引表空间(可选)
create tablespace dbs_i_jingyu datafile '/u02/oradata/jingyu/dbs_i_jingyu01.dbf' size 30m autoextend off;
-- 假设创建用户 jingyu 密码 jingyu,默认临时表空间 temp_jingyu, 默认数据表空间 dbs_d_jingyu。
create user jingyu identified by jingyu
 temporary tablespace temp_jingyu
 default tablespace dbs_d_jingyu
 quota unlimited on dbs_d_jingyu;
-- 赋予普通业务用户权限
grant resource, connect to jingyu;
-- 赋予dba用户权限
grant dba to jingyu;
-- 业务用户登录
conn jingyu/jingyu 
-- 1.1 创建分区表
create table t_part(
id number, 
name varchar2(20), 
start_time date, 
content varchar2(200)
)partition by range(start_time)
(
 partition p20150101 values less than (to_date(' 2015-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss', 'nls_calendar=gregorian'))
 tablespace dbs_d_jingyu,
 partition p20150102 values less than (to_date(' 2015-01-02 00:00:00', 'syyyy-mm-dd hh24:mi:ss', 'nls_calendar=gregorian'))
 tablespace dbs_d_jingyu,
 partition p20150103 values less than (to_date(' 2015-01-03 00:00:00', 'syyyy-mm-dd hh24:mi:ss', 'nls_calendar=gregorian'))
 tablespace dbs_d_jingyu
);

-- 1.2 插入测试数据
--分区p20150102插入10000行数据
begin
 for i in 1..10000 loop
 insert into t_part values (i,'alfred'||i, to_date('2015-01-01','yyyy-mm-dd'), 'aaaaaaaaaa');
 end loop;
 commit;
end;
/
--分区p20150103插入20000行数据
begin
 for i in 10001..30000 loop
 insert into t_part values (i,'alfred'||i, to_date('2015-01-02','yyyy-mm-dd'), 'aaaaaaaaaa');
 end loop;
 commit;
end;
/

-- 1.3查询表数据量和大小
select count(1) from t_part; 
--result: 30000
select count(1) from t_part partition(p20150102); 
--result: 10000
select count(1) from t_part partition(p20150103); 
--result: 20000
--普通表/分区表的每个分区大约__g大小
set linesize 160
col segment_name for a30
select (t.bytes/1024/1024) "mb", t.owner, t.segment_name, t.partition_name, t.tablespace_name from dba_segments t where segment_name = 't_part';
 mb owner  segment_name  partition_name  tablespace_name
---------- ------------------------------ ------------------------------ ------------------------------ ------------------------------
 8 jingyu  t_part  p20150102  dbs_d_jingyu
 8 jingyu  t_part  p20150103  dbs_d_jingyu

2. 模拟分区中有数据块损坏情景

我这里使用bbed制造坏块,修改t_part分区表的分区p20150103中的某个块内容,模拟真实环境中有数据块损坏的情景。

--查询分区p20150103的header_block
select header_file,header_block from dba_segments where segment_name='t_part' and partition_name='p20150103' and owner='jingyu';
sql> select header_file,header_block from dba_segments where segment_name='t_part' and partition_name='p20150103' and owner='jingyu';

header_file header_block
----------- ------------
  5  1169

--查询某一行记录所在的块
select
 rowid,
 dbms_rowid.rowid_relative_fno(rowid)rel_fno,
 dbms_rowid.rowid_block_number(rowid)blockno,
 dbms_rowid.rowid_row_number(rowid) rowno
 from t_part where id = 20000; 

sql> select
 2 rowid,
 3 dbms_rowid.rowid_relative_fno(rowid)rel_fno,
 4 dbms_rowid.rowid_block_number(rowid)blockno,
 5 dbms_rowid.rowid_row_number(rowid) rowno
 6 from t_part where id = 20000;

rowid   rel_fno blockno rowno
------------------ ---------- ---------- ----------
aaavveaafaaaatbabx  5 1217  87

使用bbed工具破坏5号文件1217块内容,

bbed工具:

[oracle@jy-db01 ~]$ bbed parfile=/tmp/bbed.par
password:

bbed: release 2.0.0.0.0 - limited production on tue jan 19 11:37:59 2016

copyright (c) 1982, 2011, oracle and/or its affiliates. all rights reserved.

************* !!! for oracle internal use only !!! ***************

bbed> set dba 5,1217
 dba  0x014004c1 (20972737 5,1217)

bbed> map
 file: /u02/oradata/jingyu/dbs_d_jingyu01.dbf (5)
 block: 1217     dba:0x014004c1
------------------------------------------------------------
 ktb data block (table/cluster)

 struct kcbh, 20 bytes   @0 

 struct ktbbh, 72 bytes   @20 

 struct kdbh, 14 bytes   @100 

 struct kdbt[1], 4 bytes   @114 

 sb2 kdbr[177]    @118 

 ub1 freespace[815]    @472 

 ub1 rowdata[6901]    @1287 

 ub4 tailchk    @8188 


bbed> d /v offset 0 count 128
 file: /u02/oradata/jingyu/dbs_d_jingyu01.dbf (5)
 block: 1217 offsets: 0 to 127 dba:0x014004c1
-------------------------------------------------------
 06a20000 c1044001 52733100 00000106 l ......@.rs1.....
 a18b0000 01000c00 de5b0100 4d733100 l .........[..ms1.
 0000e81f 021f3200 81044001 02001b00 l ......2...@.....
 5d0b0000 fc0fc000 df030600 b1200000 l ]............ ..
 52733100 00000000 00000000 00000000 l rs1.............
 00000000 00000000 00000000 00000000 l ................
 00000000 0001b100 ffff7401 a3042f03 l ..........t.../.
 2f030000 b100711f 4a1f231f fc1ed51e l /.....q.j.#.....

 <16 bytes per line>

bbed> modify /x 19901010 offset 0
 file: /u02/oradata/jingyu/dbs_d_jingyu01.dbf (5)
 block: 1217  offsets: 0 to 127  dba:0x014004c1
------------------------------------------------------------------------
 19901010 c1044001 52733100 00000106 a18b0000 01000c00 de5b0100 4d733100 
 0000e81f 021f3200 81044001 02001b00 5d0b0000 fc0fc000 df030600 b1200000 
 52733100 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 0001b100 ffff7401 a3042f03 2f030000 b100711f 4a1f231f fc1ed51e

 <32 bytes per line>

bbed> sum apply
check value for file 5, block 1217:
current = 0xa9ae, required = 0xa9ae

bbed>

至此破坏了5号文件,1217块。

查询v$database_block_corruption

select * from v$database_block_corruption;

sql> select * from v$database_block_corruption;

 file# block# blocks corruption_change# corruptio
---------- ---------- ---------- ------------------ ---------
  5 1217  1   0 corrupt

--此时查询分区表t_part
alter system flush buffer_cache;
select count(1) from t_part;
--查询报错ora-01578
select count(1) from t_part partition(p20150102);
--查询正常,即分区p20150102未受影响
select count(1) from t_part partition(p20150103);
--查询报错ora-01578

--尝试逻辑导出表数据失败
[oracle@jy-db01 ~]$ exp jingyu/jingyu tables=t_part file=t_part.dmp log=exp_t_part.log

export: release 11.2.0.4.0 - production on tue jan 19 11:52:21 2016

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, automatic storage management, olap, data mining
and real application testing options
export done in zhs16gbk character set and al16utf16 nchar character set

about to export specified tables via conventional path ...
. . exporting table    t_part
. . exporting partition   p20150101  0 rows exported
. . exporting partition   p20150102 10000 rows exported
. . exporting partition   p20150103
exp-00056: oracle error 1578 encountered
ora-01578: oracle data block corrupted (file # 5, block # 1217)
ora-01110: data file 5: '/u02/oradata/jingyu/dbs_d_jingyu01.dbf'
export terminated successfully with warnings.
[oracle@jy-db01 ~]$

3. 尝试使用oracle内部事件10231进行不完全恢复

使用oracle 10231内部事件可以跳过坏块

--启用10231内部事件
alter system set events='10231 trace name context forever,level 10';
--关闭10231内部事件
alter system set events='10231 trace name context off';

测试设置10231事件后是否可以逻辑导出:

[oracle@jy-db01 ~]$ sqlplus / as sysdba

sql*plus: release 11.2.0.4.0 production on tue jan 19 14:01:43 2016

copyright (c) 1982, 2013, oracle. all rights reserved.


connected to:
oracle database 11g enterprise edition release 11.2.0.4.0 - 64bit production
with the partitioning, automatic storage management, olap, data mining
and real application testing options

sql> alter system set events='10231 trace name context forever,level 10';

system altered.

sql> exit
disconnected from oracle database 11g enterprise edition release 11.2.0.4.0 - 64bit production
with the partitioning, automatic storage management, olap, data mining
and real application testing options
[oracle@jy-db01 ~]$ exp jingyu/jingyu tables=t_part file=t_part.dmp log=exp_t_part.log

export: release 11.2.0.4.0 - production on tue jan 19 14:01:57 2016

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, automatic storage management, olap, data mining
and real application testing options
export done in zhs16gbk character set and al16utf16 nchar character set

about to export specified tables via conventional path ...
. . exporting table    t_part
. . exporting partition   p20150101  0 rows exported
. . exporting partition   p20150102 10000 rows exported
. . exporting partition   p20150103 19823 rows exported
export terminated successfully without warnings.

--成功导出后记得要关闭10231内部事件
alter system set events='10231 trace name context off';

20000 - 19823 = 177行,也就是说该数据块损坏直接导致了177行数据丢失。不过还好,保住了大部分数据。

实际上设置10231内部事件后,如果上面逻辑导出没问题,这种情况自然还可以把数据直接导出到临时表,更加方便。

sql> select count(1) from t_part;
select count(1) from t_part
*
error at line 1:
ora-01578: oracle data block corrupted (file # 5, block # 1217)
ora-01110: data file 5: '/u02/oradata/jingyu/dbs_d_jingyu01.dbf'

sql> alter system set events='10231 trace name context forever,level 10';

system altered.

sql> select count(1) from t_part;

 count(1)
----------
 29823

sql> create table temp_t_part_20150103 as select * from t_part partition(p20150103);

table created.

sql> alter system set events='10231 trace name context off';

system altered.

sql> select count(1) from t_part partition(p20150103);
select count(1) from t_part partition(p20150103)
*
error at line 1:
ora-01578: oracle data block corrupted (file # 5, block # 1217)
ora-01110: data file 5: '/u02/oradata/jingyu/dbs_d_jingyu01.dbf'

sql> select count(1) from temp_t_part_20150103;

 count(1)
----------
 19823

reference

•http://blog.csdn.net/tianlesoftware/article/details/5024966

•http://blog.csdn.net/seertan/article/details/8507045

•http://blog.csdn.net/coolyl/article/details/195919

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。