一、实验环境
序号 | 主机名 | ip地址 | 系统版本 |
---|---|---|---|
1 | ora11g | 192.168.116.3 | centos7.4 |
二、一键安装oracle
为了快速实验,省去安装的繁琐步骤。本次实验直接一键安装oracle(含安装数据库软件、创建监听、创建实例)
[root@oracle11g soft]# pwd
/soft
[root@oracle11g soft]# ll
total 1900
-rwxrwxr-x 1 oracle oinstall 651780 nov 5 2021 bison-2.4.1-5.el6.x86_64.rpm
-rwxrwxr-x 1 oracle oinstall 19112 jul 11 16:15 compat-libcap1-1.10-7.el7.x86_64.rpm
-rwxrwxr-x 1 oracle oinstall 228841 nov 5 2021 compat-libstdc -33-3.2.3-47.3.x86_64.rpm
-rw-r--r-- 1 oracle oinstall 502 aug 1 11:16 db.rsp
-rwxrwxr-x 1 oracle oinstall 54070 jun 1 2022 elfutils-libelf-0.137-3.el5.x86_64.rpm
-rwxrwxr-x 1 oracle oinstall 24980 jun 1 2022 elfutils-libelf-devel-0.137-3.el5.x86_64.rpm
-rwxrwxr-x 1 oracle oinstall 64118 jun 1 2022 elfutils-libelf-devel-static-0.137-3.el5.x86_64.rpm
-rwxrwxr-x 1 oracle oinstall 291380 nov 5 2021 flex-2.5.35-9.el6.x86_64.rpm
-rwxrwxr-x 1 oracle oinstall 17327 aug 1 11:22 oracleallsilent_20230801111436.log
-rwxrwxr-x 1 oracle oinstall 166252 nov 5 2021 oracle-install.sh
-rwxrwxr-x 1 oracle oinstall 210877 nov 5 2021 pdksh-5.2.14-37.el5_8.1.x86_64.rpm
-rwxrwxr-x 1 oracle oinstall 59332 nov 5 2021 telnet-0.17-48.el6.x86_64.rpm
-rwxrwxr-x 1 oracle oinstall 124812 nov 5 2021 xinetd-2.3.14-40.el6.x86_64.rpm
[root@oracle11g soft]# ./oracle-install.sh -i 192.168.116.3
---------------------------------------------------------------------------------------------
安装过程略
---------------------------------------------------------------------------------------------
disconnected from oracle database 11g enterprise edition release 11.2.0.4.0 - 64bit production
with the partitioning, olap, data mining and real application testing options
congratulations, install successful! please reboot later.
[oracle@orcl:/home/oracle]$ sqlplus / as sysdba
sql> shutdown immediate
sql> startup mount;
sql> alter database archivelog;
sql> alter database open;
sql> alter system switch logfile;
sql> archive log list;
database log mode archive mode
automatic archival enabled
archive destination /archivelog
oldest online log sequence 3
next log sequence to archive 5
current log sequence 5
三、数据库坏块实验
3.1、坏块介绍
出现坏块现象是指:在oracle数据库的一个或多个数据块(一个数据块的容量在创建数据库时由db_block_size参数指定,缺省为8k)内出现内容混乱的现象。由于正常的数据块都有固定的合法内容格式,坏块的出现,导致数据库进程无法正常解析数据块的内容,进而使数据库进程报错乃至挂起,并级联导致整个数据库实例出现异常。
对于 oracle 数据块物理损坏的情形,通常可以分为两种情况:
-
有备份,通过 rman 恢复
有备份的情况下,这是很理想的情形,可以直接通过 rman 块介质恢复(block media recovery)功能来完成受损块的恢复。
-
无备份,通过 oracle自带的dbms_repair 包来修复
📢 注意: 使用 dbms_repair 包来修复,并非完全恢复,而是标记坏块,然后不对起进行访问,这部分被标记的数据也就丢失了,这是无法避免的。
1,坏块产生的原因
坏块产生的原因大致有以下几种:
-
硬件问题
oracle进程在处理一个数据块时,首先将其读入物理内存空间,在处理完成后,再由特定进程将其写回磁盘;如果在这个过程中,出现内存故障,cpu计算失误,都会导致内存数据块的内容混乱,最后反映到写回磁盘的数据块内容有误。同样,如果存储子系统出现异常,数据块损坏也就随之出现了。
-
操作系统bug
由于oracle进程对数据块的读写,都是以操作系统内核调用(system call)的方式完成的,如果操作系统在内核调用存在问题,必然导致oracle进程写入非法的内容。
-
操作系统的i/o错误或缓冲问题
-
内存或paging问题
-
oraclebug
oracle软件特定版本上,可能出现导致数据块的内容出现异常bug。
-
非oracle进程扰乱oracle共享内存区域
如上文所述,在当数据块的内容被读入主机的物理内存时,如果其他非oracle进程,对oracle使用的共享内存区域形成了扰乱,最终导致写回磁盘的数据块内容混乱。
-
异常关机,掉电,终止服务
异常关机,掉电,终止服务使进程异常终止,而破坏数据块的完整性,导致坏块产生。注:这也是为什么突然断电会导致数据库无法启动
由上可见,坏块的形成原因复杂。当出现坏块时,为了找到确切的原因,需要大量的分析时间和排查操作,甚至需要多次重现才能找出根本原因。但当故障发生在生产系统上,我们为了减少停机时间,会尽快实施应急权变措施以保证系统的可用性,这样就破坏了故障现场,对根本原因的分析因而也更加困难了。
2,坏块的预防
坏块问题破坏性大,但并非不可预防。
-
在metalink.oracle.com网站,oracle定期发布基于特定软件版本的“已知问题(known issues)说明”。对于可能导致坏块的oracle软件bug,在oracle公司内部,是作为高严重级别的问题进行处理,在“已知问题(known issues)说明”中,这些bug以严重(noticable)问题标出(标记为*或 ),部分问题,oracle还会发布警告(alert)通告。在文档中,oracle会提供相应的补丁或应对措施。
-
oracle提供恢复工具-recovery manager,提供了扫描文件检查坏块的功能。
在recovery manager界面中,使用:
rman> backup check logical validate datafile n ;
可以检查数据文件是否包含坏块,同时并不产生实际的备份输出。
-
dbv工具检查
因为dbv要求file后面跟的必须是一个文件扩展名,所以如果用裸设备存储的,就必须使用ln链接裸设备到一个文件,然后再用dbv对这个链接文件进行检查。analyze table tablename validate structure cascade它执行坏块的检查,但是不会标记坏块为corrupt,检测的结果保存在user_dump_dest目录下的用户trace文件中。
-
利用exp工具导出整个数据库可以检测坏块
对以下情况的坏块是检测不出来的:
hwm以上的坏块是不会发现的
索引中存在的坏块是不会发现的
数据字典中的坏块是不会发现的
结合数据库性能综合考虑db_block_checksum和db_blockchecking参数。
当我们使用recovery manager进行实际的数据库备份时,同时也就进行了坏块检查。但要注意的是,在线使用recovery manager扫描坏块和备份时,需要数
据库运行在归档模式(archive log),否则只能在数据库未打开的情况下进行。对于操作系统问题和硬件故障,则需要相应厂商的配合支持。同时,避免在数
据库主机运行其他用户进程,避免异常停机,也会减少坏块发生的几率。
3,坏块故障的识别
遇到坏块问题时,数据库的异常表现通常有:
报告ora-01578错误。
报告ora-1110错误。
报告ora-00600错误,其中,第一个参数为2000-8000,cache layer 2000 – 4000,transaction layer 4000 – 6000, layer 6000 - 8000。
trace文件中出现corrupt block dba: 0x160c5958 . found。
分析对象失败。
后台进程,如dbwr,lgwr出现长时间异常等待,如“lgwr wait for redo copy”。
3.2、模拟标记坏块方法的恢复
1,准备数据
--创建用于演示的data file
sql> create tablespace tbs_tmp datafile '/oradata/orcl/tbs_tmp.dbf' size 10m autoextend on;
sql> create user test identified by test;
sql> grant dba to test;
sql> conn test/test
--基于新的数据文件创建对象tb_tmp
sql> create table tb_tmp tablespace tbs_tmp as select * from dba_objects;
sql> select count(*) from tb_tmp;
count(*)
----------
86953
sql> col file_name format a60
sql> select file_id,file_name from dba_data_files where tablespace_name='tbs_tmp';
file_id file_name
---------- ------------------------------------------------------------
6 /oradata/orcl/tbs_tmp.dbf
--表对象tb_tmp上的信息,包含对应的文件信息,头部块,总块数
sql> col segment_name for a20
sql> select segment_name , header_file , header_block,blocks
from dba_segments
where segment_name = 'tb_tmp' and owner='test';
segment_name header_file header_block blocks
-------------------- ----------- ------------ ----------
tb_tmp 6 130 1280
2,模拟坏块
此处模拟物理坏块,注意,在没有备份的情况下,物理坏块基本无救,但是我们需要对坏块进行跳过处理。
破坏137 数据块的内容seek=n从输出文件开头跳过 n个blocks 个块后再开始复制。
conv=notrunc不截短输出文件
[oracle@orcl:/home/oracle]$ dd if=/dev/zero of=/oradata/orcl/tbs_tmp.dbf bs=8192 conv=notrunc seek=137 count=1
1 0 records in
1 0 records out
8192 bytes (8.2 kb) copied, 0.000214285 s, 38.2 mb/s
--清空buffer cache
sql> alter system flush buffer_cache;
--查询表对相 tb_tmp,收到ora-01578的报错
sql> select count(*) from tb_tmp;
error at line 1:
ora-01578: oracle data block corrupted (file # 6, block # 137)
ora-01110: data file 6: '/oradata/orcl/tbs_tmp.dbf'
同时,alert文件也收到对应的报错信息
hex dump of (file 6, block 137) in trace file /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_44345.trc
corrupt block relative dba: 0x01800089 (file 6, block 137)
completely zero block found during multiblock buffer read
reading datafile '/oradata/orcl/tbs_tmp.dbf' for corruption at rdba: 0x01800089 (file 6, block 137)
reread (file 6, block 137) found same corrupt data (no logical check)
tue aug 01 14:30:34 2023
corrupt block found
tsn = 7, tsname = tbs_tmp
rfn = 6, blk = 137, rdba = 25165961
objn = 88605, objd = 88605, object = tb_tmp, subobject =
segment owner = test, segment type = table segment
corrupt block found
tsn = 7, tsname = tbs_tmp
rfn = 6, blk = 137, rdba = 25165961
objn = 88605, objd = 88605, object = tb_tmp, subobject =
segment owner = test, segment type = table segment
errors in file /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_44345.trc (incident=5025):
ora-01578: oracle data block corrupted (file # 6, block # 137)
ora-01110: data file 6: '/oradata/orcl/tbs_tmp.dbf'
incident details in: /u01/app/oracle/diag/rdbms/orcl/orcl/incident/incdir_5025/orcl_ora_44345_i5025.trc
tue aug 01 14:30:35 2023
sweep [inc][5025]: completed
hex dump of (file 6, block 137) in trace file /u01/app/oracle/diag/rdbms/orcl/orcl/incident/incdir_5025/orcl_m000_44572_i5025_a.trc
corrupt block relative dba: 0x01800089 (file 6, block 137)
completely zero block found during validation
reread of blocknum=137, file=/oradata/orcl/tbs_tmp.dbf. found same corrupt data
reread of blocknum=137, file=/oradata/orcl/tbs_tmp.dbf. found same corrupt data
reread of blocknum=137, file=/oradata/orcl/tbs_tmp.dbf. found same corrupt data
reread of blocknum=137, file=/oradata/orcl/tbs_tmp.dbf. found same corrupt data
reread of blocknum=137, file=/oradata/orcl/tbs_tmp.dbf. found same corrupt data
tue aug 01 14:30:35 2023
dumping diagnostic data in directory=[cdmp_20230801143035], requested by (instance=1, osid=44345), summary=[incident=5025].
checker run found 1 new persistent data failures
--此时dbv命令已经可以检查出坏块了:
[oracle@orcl:/home/oracle]$ dbv file=/oradata/orcl/tbs_tmp.dbf
dbverify: release 11.2.0.4.0 - production on tue aug 1 14:32:15 2023
米乐app官网下载 copyright (c) 1982, 2011, oracle and/or its affiliates. all rights reserved.
dbverify - verification starting : file = /oradata/orcl/tbs_tmp.dbf
page 137 is marked corrupt
corrupt block relative dba: 0x01800089 (file 6, block 137)
completely zero block found during dbv:
dbverify - verification complete
total pages examined : 1408
total pages processed (data) : 1241
total pages failing (data) : 0
total pages processed (index): 0
total pages failing (index): 0
total pages processed (other): 155
total pages processed (seg) : 0
total pages failing (seg) : 0
total pages empty : 11
total pages marked corrupt : 1
total pages influx : 0
total pages encrypted : 0
highest block scn : 997436 (0.997436)
--通过backup validate检查数据文件,看是否还有其它block损坏
rman> backup validate check logical datafile 6;
starting backup at 01-aug-23
using target database control file instead of recovery catalog
allocated channel: ora_disk_1
channel ora_disk_1: sid=39 device type=disk
channel ora_disk_1: starting full datafile backup set
channel ora_disk_1: specifying datafile(s) in backup set
input datafile file number=00006 name=/oradata/orcl/tbs_tmp.dbf
channel ora_disk_1: backup set complete, elapsed time: 00:00:01
list of datafiles
=================
file status marked corrupt empty blocks blocks examined high scn
---- ------ -------------- ------------ --------------- ----------
6 failed 0 139 1536 997839
file name: /oradata/orcl/tbs_tmp.dbf
block type blocks failing blocks processed
---------- -------------- ----------------
data 0 1241
index 0 0
other 1 156
validate found one or more corrupt blocks
see trace file /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_44745.trc for details
finished backup at 01-aug-23
--查询视图v$database_block_corruption,提示有坏块,可以看到该文件只有一个数据块损坏。注意该视图可能不会返回任何数据,如无返回,先在rman执行backup validate。
sql> select * from v$database_block_corruption;
file# block# blocks corruption_change# corruptio
---------- ---------- ---------- ------------------ ---------
6 137 1 0 all zero
--查看坏块的对象,这里很明显是个表对象,如果是索引可以重建,如果是表,则有可能会丢数据
sql> set linesize 200
sql> select owner, segment_name, segment_type, tablespace_name
from dba_extents a
where file_id = 6
and 137 between block_id and block_id blocks - 1
/
owner segment_name segment_type tablespace_name
------------------------------ -------------------- ------------------ ------------------------------
test tb_tmp table tbs_tmp
3,跳过坏块
--产生坏块的时候,exp导出的时候会发现坏块同时报错无法导出
[oracle@orcl:/home/oracle]$ exp test/test tables=tb_tmp file=tb_tmp.dump
exp-00056: oracle error 1578 encountered
ora-01578: oracle data block corrupted (file # 6, block # 137)
ora-01110: data file 6: '/oradata/orcl/tbs_tmp.dbf'
export terminated successfully with warnings.
--我们可以参考mos中提供的几种方法,任选一种来处理:
sql> select owner,table_name,skip_corrupt from dba_tables where table_name='tb_tmp';
owner table_name skip_cor
------------------------------ --------------- --------
test tb_tmp disabled
--这里先采用dbms_repair.skip_corrupt_blocks程序来将表上进行skip corrupt标记跳过。
[oracle@orcl:/archivelog]$ sqlplus / as sysdba
begin
dbms_repair.skip_corrupt_blocks
(
schema_name => 'test',
object_name => 'tb_tmp',
object_type => dbms_repair.table_object,
flags => dbms_repair.skip_flag
);
end;
/
pl/sql procedure successfully completed.
sql> select owner,table_name,skip_corrupt from dba_tables where table_name='tb_tmp';
owner table_name skip_cor
------------------------------ ------------------------------ --------
test tb_tmp enabled
4,验证
上面将tb_tmp表标记坏块后,查询之前报错的sql句就正常了。
sql> select count(*) from tb_tmp;
count(*)
----------
86873
再次导出,没有报错
[oracle@orcl:/home/oracle]$ exp test/test tables=tb_tmp file=tb_tmp.dump
about to export specified tables via conventional path ...
. . exporting table tb_tmp 86873 rows exported
export terminated successfully without warnings.
--取消表上的skip corrupt标记
[oracle@orcl:/archivelog]$ sqlplus / as sysdba
begin
dbms_repair.skip_corrupt_blocks
(
schema_name => 'test',
object_name => 'tb_tmp',
flags => dbms_repair.noskip_flag
);
end;
/
pl/sql procedure successfully completed.
sql> select count(*) from tb_tmp;
select count(*) from tb_tmp
*
error at line 1:
ora-01578: oracle data block corrupted (file # 6, block # 137)
ora-01110: data file 6: '/oradata/orcl/tbs_tmp.dbf'
后台alert继续报错
--删除表
sql> conn test/test
sql> drop table tb_tmp;
--导入表
[oracle@orcl:/home/oracle]$ imp test/test tables=tb_tmp file=tb_tmp.dump
export file created by export:v11.02.00 via conventional path
import done in al32utf8 character set and al16utf16 nchar character set
. importing test's objects into test
. importing test's objects into test
. . importing table "tb_tmp" 86873 rows imported
import terminated successfully without warnings.
sql> select count(*) from tb_tmp;
count(*)
----------
86873
3.3、模拟blockrecover方法的恢复
1,准备数据
--创建用于演示的data file
[oracle@orcl:/home/oracle]$ sqlplus / as sysdba
sql> create tablespace rman_tmp datafile '/oradata/orcl/rman_tmp.dbf' size 100m autoextend on;
sql> create user test01 identified by test01;
sql> grant dba to test01;
sql> conn test01/test01;
--基于新的数据文件创建表对象tb_rman
sql> create table tb_rman tablespace rman_tmp as select * from dba_objects;
sql> select count (*) from tb_rman;
count(*)
----------
86953
sql> col file_name format a60
sql> select file_id,file_name from dba_data_files where tablespace_name='rman_tmp';
file_id file_name
---------- ------------------------------------------------------------
6 /oradata/orcl/rman_tmp.dbf
--表对象tb_rman上的信息,包含对应的文件信息,头部块,总块数
sql> col segment_name for a20
sql> select segment_name , header_file , header_block,blocks
from dba_segments
where segment_name = 'tb_rman' and owner='test01';
segment_name header_file header_block blocks
-------------------- ----------- ------------ ----------
tb_rman 6 130 1280
2,rman备份数据库
[oracle@orcl:/home/oracle]$ rman target /
rman> backup full database;
piece handle=/u01/app/oracle/fast_recovery_area/orcl/backupset/2023_08_01/o1_mf_nnndf_tag20230801t160510_ldkh5q28_.bkp tag=tag20230801t160510 comment=none
channel ora_disk_1: backup set complete, elapsed time: 00:00:15
piece handle=/u01/app/oracle/fast_recovery_area/orcl/backupset/2023_08_01/o1_mf_ncsnf_tag20230801t160510_ldkh674r_.bkp tag=tag20230801t160510 comment=none
channel ora_disk_1: backup set complete, elapsed time: 00:00:01
finished backup at 01-aug-23
3,模拟单个坏块
[oracle@orcl:/home/oracle]$ dd if=/dev/zero of=/oradata/orcl/rman_tmp.dbf bs=8192 conv=notrunc seek=130 count=1
1 0 records in
1 0 records out
8192 bytes (8.2 kb) copied, 0.000152197 s, 53.8 mb/s
--清空buffer cache
sql> alter system flush buffer_cache;
--查询表tb_rman,收到ora-01578报错
sql> select count (*) from tb_rman;
select count (*) from tb_rman
*
error at line 1:
ora-01578: oracle data block corrupted (file # 6, block # 130)
ora-01110: data file 6: '/oradata/orcl/rman_tmp.dbf'
--同时alert也有报错日志输出
hex dump of (file 6, block 130) in trace file /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_46480.trc
corrupt block relative dba: 0x01800082 (file 6, block 130)
completely zero block found during buffer read
reading datafile '/oradata/orcl/rman_tmp.dbf' for corruption at rdba: 0x01800082 (file 6, block 130)
reread (file 6, block 130) found same corrupt data (no logical check)
tue aug 01 16:35:04 2023
corrupt block found
tsn = 8, tsname = rman_tmp
rfn = 6, blk = 130, rdba = 25165954
objn = 88611, objd = 88611, object = tb_rman, subobject =
segment owner = test01, segment type = table segment
errors in file /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_46480.trc (incident=5027):
ora-01578: oracle data block corrupted (file # 6, block # 130)
ora-01110: data file 6: '/oradata/orcl/rman_tmp.dbf'
incident details in: /u01/app/oracle/diag/rdbms/orcl/orcl/incident/incdir_5027/orcl_ora_46480_i5027.trc
tue aug 01 16:35:05 2023
sweep [inc][5027]: completed
hex dump of (file 6, block 130) in trace file /u01/app/oracle/diag/rdbms/orcl/orcl/incident/incdir_5027/orcl_m000_46980_i5027_a.trc
corrupt block relative dba: 0x01800082 (file 6, block 130)
completely zero block found during validation
reread of blocknum=130, file=/oradata/orcl/rman_tmp.dbf. found same corrupt data
reread of blocknum=130, file=/oradata/orcl/rman_tmp.dbf. found same corrupt data
reread of blocknum=130, file=/oradata/orcl/rman_tmp.dbf. found same corrupt data
reread of blocknum=130, file=/oradata/orcl/rman_tmp.dbf. found same corrupt data
reread of blocknum=130, file=/oradata/orcl/rman_tmp.dbf. found same corrupt data
hex dump of (file 6, block 130) in trace file /u01/app/oracle/diag/rdbms/orcl/orcl/incident/incdir_5027/orcl_m000_46980_i5027_a.trc
corrupt block relative dba: 0x01800082 (file 6, block 130)
completely zero block found during buffer read
reading datafile '/oradata/orcl/rman_tmp.dbf' for corruption at rdba: 0x01800082 (file 6, block 130)
reread (file 6, block 130) found same corrupt data (no logical check)
corrupt block found
tsn = 8, tsname = rman_tmp
rfn = 6, blk = 130, rdba = 25165954
objn = -1, objd = 88611, object = rman_tmp, subobject =
segment owner = , segment type = temporary segment
errors in file /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_m000_46980.trc (incident=5049):
ora-01578: oracle data block corrupted (file # 6, block # 130)
ora-01110: data file 6: '/oradata/orcl/rman_tmp.dbf'
incident details in: /u01/app/oracle/diag/rdbms/orcl/orcl/incident/incdir_5049/orcl_m000_46980_i5049.trc
tue aug 01 16:35:06 2023
dumping diagnostic data in directory=[cdmp_20230801163506], requested by (instance=1, osid=46480), summary=[incident=5027].
checker run found 1 new persistent data failures
dumping diagnostic data in directory=[cdmp_20230801163507], requested by (instance=1, osid=46980 (m000)), summary=[incident=5049].
----查询视图v$database_block_corruption,提示有坏块,注意该视图可能不会返回任何数据,如无返回,先执行backup validate
sql> select * from v$database_block_corruption;
file# block# blocks corruption_change# corruptio
---------- ---------- ---------- ------------------ ---------
6 130 1 0 all zero
4,blockrecover恢复单个坏块
[oracle@orcl:/home/oracle]$ rman target /
rman> blockrecover datafile 6 block 130;
starting recover at 01-aug-23
using target database control file instead of recovery catalog
allocated channel: ora_disk_1
channel ora_disk_1: sid=47 device type=disk
channel ora_disk_1: restoring block(s)
channel ora_disk_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00006
channel ora_disk_1: reading from backup piece /u01/app/oracle/fast_recovery_area/orcl/backupset/2023_08_01/o1_mf_nnndf_tag20230801t162916_ldkjlwgs_.bkp
channel ora_disk_1: piece handle=/u01/app/oracle/fast_recovery_area/orcl/backupset/2023_08_01/o1_mf_nnndf_tag20230801t162916_ldkjlwgs_.bkp tag=tag20230801t162916
channel ora_disk_1: restored block(s) from backup piece 1
channel ora_disk_1: block restore complete, elapsed time: 00:00:01
starting media recovery
media recovery complete, elapsed time: 00:00:01
finished recover at 01-aug-23
--再次查询表tb_rman正常无报错
sql> select count (*) from tb_rman;
count(*)
----------
86953
5,模拟多个数据坏块
[oracle@orcl:/home/oracle]$ dd if=/dev/zero of=/oradata/orcl/rman_tmp.dbf bs=8192 conv=notrunc seek=133 count=1
[oracle@orcl:/home/oracle]$ dd if=/dev/zero of=/oradata/orcl/rman_tmp.dbf bs=8192 conv=notrunc seek=143 count=1
[oracle@orcl:/home/oracle]$ dd if=/dev/zero of=/oradata/orcl/rman_tmp.dbf bs=8192 conv=notrunc seek=153 count=1
sql> alter system flush buffer_cache;
--下面提示块133被损坏,注意我们损坏了多块数据块,但查询时,从块号最小的开始提示,如133被修复后还有坏块则继续提示133之后的坏块
sql> select count (*) from tb_rman;
select count (*) from tb_rman
*
error at line 1:
ora-01578: oracle data block corrupted (file # 6, block # 133)
ora-01110: data file 6: '/oradata/orcl/rman_tmp.dbf'
后台alert也只报了133块的错误
ora-01578: oracle data block corrupted (file # 6, block # 133)
ora-01110: data file 6: '/oradata/orcl/rman_tmp.dbf'
--查询视图v$database_block_corruption无任何记录
sql> select * from v$database_block_corruption;
no rows selected
--下面使用backup validate来校验数据文件
list of datafiles
=================
file status marked corrupt empty blocks blocks examined high scn
---- ------ -------------- ------------ --------------- ----------
6 failed 0 11403 12800 1002692
file name: /oradata/orcl/rman_tmp.dbf
block type blocks failing blocks processed
---------- -------------- ----------------
data 0 1239
index 0 0
other 3 158
--再次查询v$database_block_corruption,表明有3个损坏的块
sql> select * from v$database_block_corruption;
file# block# blocks corruption_change# corruptio
---------- ---------- ---------- ------------------ ---------
6 133 1 0 all zero
6 143 1 0 all zero
6 153 1 0 all zero
--使用dbv工具来校验数据文件,查出3个坏块
[oracle@orcl:/home/oracle]$ dbv file=/oradata/orcl/rman_tmp.dbf
dbverify: release 11.2.0.4.0 - production on tue aug 1 16:48:21 2023
米乐app官网下载 copyright (c) 1982, 2011, oracle and/or its affiliates. all rights reserved.
dbverify - verification starting : file = /oradata/orcl/rman_tmp.dbf
page 133 is marked corrupt
corrupt block relative dba: 0x01800085 (file 6, block 133)
completely zero block found during dbv:
page 143 is marked corrupt
corrupt block relative dba: 0x0180008f (file 6, block 143)
completely zero block found during dbv:
page 153 is marked corrupt
corrupt block relative dba: 0x01800099 (file 6, block 153)
completely zero block found during dbv:
dbverify - verification complete
total pages examined : 12800
total pages processed (data) : 1239
total pages failing (data) : 0
total pages processed (index): 0
total pages failing (index): 0
total pages processed (other): 155
total pages processed (seg) : 0
total pages failing (seg) : 0
total pages empty : 11403
total pages marked corrupt : 3
total pages influx : 0
total pages encrypted : 0
highest block scn : 1002692 (0.1002692)
6,blockrecover恢复多个坏块
--下面直接使用blockrecover corruption list来恢复,刚刚被校验的坏块都会被恢复
[oracle@orcl:/home/oracle]$ rman target /
rman> blockrecover corruption list;
starting recover at 01-aug-23
using target database control file instead of recovery catalog
allocated channel: ora_disk_1
channel ora_disk_1: sid=42 device type=disk
channel ora_disk_1: restoring block(s)
channel ora_disk_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00006
channel ora_disk_1: reading from backup piece /u01/app/oracle/fast_recovery_area/orcl/backupset/2023_08_01/o1_mf_nnndf_tag20230801t162916_ldkjlwgs_.bkp
channel ora_disk_1: piece handle=/u01/app/oracle/fast_recovery_area/orcl/backupset/2023_08_01/o1_mf_nnndf_tag20230801t162916_ldkjlwgs_.bkp tag=tag20230801t162916
channel ora_disk_1: restored block(s) from backup piece 1
channel ora_disk_1: block restore complete, elapsed time: 00:00:01
starting media recovery
media recovery complete, elapsed time: 00:00:01
finished recover at 01-aug-23
--校验结果,查询表tb_rman无报错
sql> select count (*) from tb_rman;
count(*)
----------
86953
sql> col owner for a20
sql> col object_name for a20
sql> set linesize 200
sql> select dbms_rowid.rowid_object(rowid) object_id,dbms_rowid.rowid_relative_fno(rowid) file_id,
dbms_rowid.rowid_block_number(rowid) block_id,owner,object_name,object_id
from test01.tb_rman where dbms_rowid.rowid_block_number(rowid)=163 and rownum<=2;
object_id file_id block_id owner object_name object_id
---------- ---------- ---------- -------------------- -------------------- ----------
88611 6 163 public gv$backup_set 2364
88611 6 163 sys gv_$backup_piece 2365
3.4、坏块的对象定位与影响
--下面我们查询块号为163上的对象
sql> set linesize 200
sql> col object_name for a20
sql> select dbms_rowid.rowid_object(rowid) object_id,dbms_rowid.rowid_relative_fno(rowid) file_id,
dbms_rowid.rowid_block_number(rowid) block_id,owner,object_name,object_id
from test01.tb_rman where dbms_rowid.rowid_block_number(rowid)=163 and rownum<=2;
object_id file_id block_id owner object_name object_id
---------- ---------- ---------- ------------------------------ -------------------- ----------
88611 6 163 public gv$backup_set 2364
88611 6 163 sys gv_$backup_piece 2365
--使用上面的方法,我们损块块163,173
[oracle@orcl:/home/oracle]$ dd if=/dev/zero of=/oradata/orcl/rman_tmp.dbf bs=8192 conv=notrunc seek=163 count=1
1 0 records in
1 0 records out
8192 bytes (8.2 kb) copied, 0.000175403 s, 46.7 mb/s
[oracle@orcl:/home/oracle]$ dd if=/dev/zero of=/oradata/orcl/rman_tmp.dbf bs=8192 conv=notrunc seek=173 count=1
1 0 records in
1 0 records out
8192 bytes (8.2 kb) copied, 0.000140643 s, 58.2 mb/s
--对于坏块对象无法进行聚合汇总等操作
sql> alter system flush buffer_cache;
sql> select count(*) from test01.tb_rman;
select count(*) from test01.tb_rman
*
error at line 1:
ora-01578: oracle data block corrupted (file # 6, block # 163)
ora-01110: data file 6: '/oradata/orcl/rman_tmp.dbf'
--对于坏块上的记录无法被查询,我们使用基于之前查询到的object_id来查询
sql> select owner,object_name,object_id from test01.tb_rman where object_id in(2364,2365);
select owner,object_name,object_id from test01.tb_rman where object_id in(2364,2365)
*
error at line 1:
ora-01578: oracle data block corrupted (file # 6, block # 163)
ora-01110: data file 6: '/oradata/orcl/rman_tmp.dbf'
--如下面的查询,位于损坏块上的数据无法被查询到,但对于未损坏的依旧可以查询。下面的查询时块161上的对象
sql> select owner,object_name,object_id from test01.tb_rman
2 where dbms_rowid.rowid_block_number(rowid)=161 and rownum<3;
owner object_name object_id
------------------------------ -------------------- ----------
sys gv_$latchname 2203
public gv$latchname 2204
--定位受损块所对应的对象
sql> col segment_name for a20
sql> run get_obj_name_from_corrupt_block
1 select tablespace_name,
2 segment_type,
3 owner,
4 segment_name,
5 partition_name
6 from dba_extents
7* where file_id = &file_id and &block_id between block_id and block_id blocks - 1
enter value for file_id: 6
enter value for block_id: 163
old 7: where file_id = &file_id and &block_id between block_id and block_id blocks - 1
new 7: where file_id = 6 and 163 between block_id and block_id blocks - 1
tablespace_name segment_type owner segment_name partition_name
------------------------------ ------------------ ------------------------------ -------------------- ------------------------------
rman_tmp table test01 tb_rman
sql> run get_obj_name_from_corrupt_block
1 select tablespace_name,
2 segment_type,
3 owner,
4 segment_name,
5 partition_name
6 from dba_extents
7* where file_id = &file_id and &block_id between block_id and block_id blocks - 1
enter value for file_id: 6
enter value for block_id: 173
old 7: where file_id = &file_id and &block_id between block_id and block_id blocks - 1
new 7: where file_id = 6 and 173 between block_id and block_id blocks - 1
tablespace_name segment_type owner segment_name partition_name
------------------------------ ------------------ ------------------------------ -------------------- --------------------------
rman_tmp table test01 tb_rman
--对于损坏的数据文件,缺省情况下,不能对其进行备份,如下
rman> backup datafile 6 tag='corruption';
starting backup at 02-aug-23
using target database control file instead of recovery catalog
allocated channel: ora_disk_1
channel ora_disk_1: sid=40 device type=disk
channel ora_disk_1: starting full datafile backup set
channel ora_disk_1: specifying datafile(s) in backup set
input datafile file number=00006 name=/oradata/orcl/rman_tmp.dbf
channel ora_disk_1: starting piece 1 at 02-aug-23
rman-00571: ===========================================================
rman-00569: =============== error message stack follows ===============
rman-00571: ===========================================================
rman-03009: failure of backup command on ora_disk_1 channel at 08/02/2023 09:51:08
ora-19566: exceeded limit of 0 corrupt blocks for file /oradata/orcl/rman_tmp.dbf
--需要设定允许损坏块的数量之后才能进行备份
rman> run{
2> set maxcorrupt for datafile 6 to 2;
3> backup datafile 6 tag='corruption';
4> }
executing command: set max corrupt
starting backup at 02-aug-23
using channel ora_disk_1
channel ora_disk_1: starting full datafile backup set
channel ora_disk_1: specifying datafile(s) in backup set
input datafile file number=00006 name=/oradata/orcl/rman_tmp.dbf
channel ora_disk_1: starting piece 1 at 02-aug-23
channel ora_disk_1: finished piece 1 at 02-aug-23
piece handle=/u01/app/oracle/fast_recovery_area/orcl/backupset/2023_08_02/o1_mf_nnndf_corruption_ldmfpw3v_.bkp tag=corruption comment=none
channel ora_disk_1: backup set complete, elapsed time: 00:00:01
finished backup at 02-aug-23
--查看备份信息如下,应在修复坏块后重新备份以避免由于保留策略导致先前可用的备份被aged out
rman> list backup summary;
list of backups
===============
key ty lv s device type completion time #pieces #copies compressed tag
------- -- -- - ----------- --------------- ------- ------- ---------- ---
1 b f a disk 01-aug-23 1 1 no tag20230801t160510
2 b f a disk 01-aug-23 1 1 no tag20230801t160510
3 b f a disk 01-aug-23 1 1 no tag20230801t162916
4 b f a disk 01-aug-23 1 1 no tag20230801t162916
5 b f a disk 02-aug-23 1 1 no corruption
3.5、总结
- 对于受损的数据块,仅仅坏块上的数据无法被查询或读取,其余正常块的数据依旧可以使用。
- 对于受损的表对象进行聚合等相关运算时收到错误提示,因为坏块上的数据无法被统计。如果你聚合的是索引列,索引未损坏的情形则可正常返回。
- 可以基于rman可用的备份文件实现块介质恢复,其数据文件无需offline,开销最小,影响最小。
- 对于多个数据块的损坏,先执行backup validate校验数据库或相应的数据文件以便标记受损的坏块后,填充v$database_block_corruption以及后续恢复。
- 对于使用backup validate 校验后的情形,坏块恢复时可以直接使用blockrecover corruption list一次性恢复所有的坏块。
- 缺省情况下,存在坏块的数据文件无法成功备份,也会导致自动备份脚本失败。