bbed解决ORA-01190

当我们使用resetlogs方式打开数据库后,发现有数据文件处于offline状态,这个时候很可能就是悲剧降临的时候,很有可能这个文件文件在resetlogs之前就处于offline状态,然后你resetlogs之后,这个文件使用常规方法很难再online,会出现ORA-01190或者ORA-01189之类的错误。
一、模拟offline文件然后resetlogs操作

1.设置datafile 5数据文件offline
2.rman备份数据库
3.关闭原数据库,删除数据文件/当前日志和部分归档日志
4.执行不完全恢复,resetlogs打开数据库(如下面操作)
[oracle@xifenfei ora11g]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Thu Mar 15 07:36:59 2012

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> recover database until cancel;
ORA-00279: change 868870 generated at 03/15/2012 03:32:11 needed for thread 1
ORA-00289: suggestion :
/u01/oracle/oradata/archivelog/ora11g/1_29_777766629.dbf
ORA-00280: change 868870 for thread 1 is in sequence #29


Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
cancel
Media recovery cancelled.
SQL> alter database open resetlogs;

Database altered.

SQL> select file#,online_status,to_char(change#,'999999999999') from v$recover_file;

     FILE# ONLINE_STATUS  TO_CHAR(CHANGE#,'999999999
---------- -------------- --------------------------
         5 OFFLINE               868810

SQL> alter database datafile 5 online;
alter database datafile 5 online
*
ERROR at line 1:
ORA-01190: control file or data file 5 is from before the last RESETLOGS
ORA-01110: data file 5: '/u01/oracle/oradata/ora11g/xifenfei01.dbf'


SQL> select file#,to_char(checkpoint_change#,'999999999999'),
  2  to_char(last_change#,'999999999999') from v$datafile;

     FILE# TO_CHAR(CHECKPOINT_CHANGE# TO_CHAR(LAST_CHANGE#,'9999
---------- -------------------------- --------------------------
         1        868874
         2        868874
         3        868874
         4        868874
         5        868810                     868874

--可以看到offline的数据文件,没有因为resetlogs操作而改变
--CHECKPOINT_CHANGE#和RESETLOGS_CHANGE#信息
SQL> select file#,to_char(checkpoint_change#,'999999999999'),
  2  to_char(RESETLOGS_CHANGE#,'999999999999')
  3  from v$datafile_header;

     FILE# TO_CHAR(CHECKPOINT_CHANGE# TO_CHAR(RESETLOGS_CHANGE#,
---------- -------------------------- --------------------------
         1        868874                     868871
         2        868874                     868871
         3        868874                     868871
         4        868874                     868871
         5        868810                     787897

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

二、bbed修改相关项

下面两项与resetlogs相关
kcvfhrlc表示 reset logs count
kcvfhrls表示 resetlogs scn
下面四项与数据库文件scn相关
kscnbas (at offset 140) – SCN of last change to the datafile.
kcvcptim (at offset 148) - Time of the last change to the datafile.
kcvfhcpc (at offset 176) – Checkpoint count.
kcvfhccc (at offset 184) – Unknown, but is always 1 less than thecheckpoint point count.


BBED> set filename '/u01/oracle/oradata/ora11g/system01.dbf'
        FILENAME        /u01/oracle/oradata/ora11g/system01.dbf

BBED> p kcvfhrlc
ub4 kcvfhrlc                                @112      0x2e5eed37

BBED> p kcvfhrls
struct kcvfhrls, 8 bytes                    @116     
   ub4 kscnbas                              @116      0x000d4207
   ub2 kscnwrp                              @120      0x0000

       

BBED>  p kcvcpscn
struct kcvcpscn, 8 bytes                 @484 
   ub4 kscnbas                           @484      0x000d4495
   ub2 kscnwrp                           @488      0x0000
   
BBED> p kcvfhcpc
ub4 kcvfhcpc                                @140      0x00000078

BBED> p kcvfhccc
ub4 kcvfhccc                                @148      0x00000077

BBED> set filename '/u01/oracle/oradata/ora11g/xifenfei01.dbf'
        FILENAME        /u01/oracle/oradata/ora11g/xifenfei01.dbf

BBED> p kcvfhrlc
ub4 kcvfhrlc                                @112      0x2e5bc6e5

BBED> set mode edit
        MODE            Edit

BBED> m /x 37ed5e2e
Warning: contents of previous BIFILE will be lost. Proceed? (Y/N) y

BBED> p kcvfhrlc
ub4 kcvfhrlc                                @112      0x2e5eed37

BBED> p kcvfhrls
struct kcvfhrls, 8 bytes                    @116     
   ub4 kscnbas                              @116      0x000c05b9
   ub2 kscnwrp                              @120      0x0000

BBED> m /x 07420d00

BBED> p kcvfhrls
struct kcvfhrls, 8 bytes                    @116     
   ub4 kscnbas                              @116      0x000d4207
   ub2 kscnwrp                              @120      0x0000

BBED>  p kcvcpscn
struct kcvcpscn, 8 bytes                 @484 
   ub4 kscnbas                           @484      0x000d41ca
   ub2 kscnwrp                           @488      0x0000
 
BBED> set offset 484
        OFFSET          484

BBED> m /x 95440d00
BBED-00209: invalid number (95440d00)

BBED> m /x 9544

BBED> set offset +2
        OFFSET          486

BBED> m /x 0d00

BBED> p kcvfhcpc
ub4 kcvfhcpc                                @140      0x00000003

BBED> m /x 78000000

BBED> p kcvfhcpc
ub4 kcvfhcpc                                @140      0x00000078

BBED> p kcvfhccc
ub4 kcvfhccc                                @148      0x00000002

BBED> m /x 77000000

BBED> p kcvfhccc
ub4 kcvfhccc                                @148      0x00000077

BBED> sum
Check value for File 0, Block 1:
current = 0xe079, required = 0x5940

BBED> sum apply
Check value for File 0, Block 1:
current = 0x5940, required = 0x5940
1

三、数据文件online

[oracle@xifenfei ora11g]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Thu Mar 15 07:48:48 2012

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

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area  368263168 bytes
Fixed Size                  1345016 bytes
Variable Size             301992456 bytes
Database Buffers           58720256 bytes
Redo Buffers                6205440 bytes
Database mounted.
Database opened.
SQL> select file#,to_char(checkpoint_change#,'999999999999'),
  2  to_char(RESETLOGS_CHANGE#,'999999999999')
  3  from v$datafile_header;

     FILE# TO_CHAR(CHECKPOINT_CHANGE# TO_CHAR(RESETLOGS_CHANGE#,
---------- -------------------------- --------------------------
         1        869528                     868871
         2        869528                     868871
         3        869528                     868871
         4        869528                     868871
         5        869525                     868871

SQL> recover datafile 5;
Media recovery complete.
SQL> alter database datafile 5 online;

Database altered.

姊妹篇:_allow_resetlogs_corruption和adjust_scn解决ORA-01190

使用bbed解决ORA-00600[2662]

一、数据库启动报ORA-00600[2662]

[oracle@node1 ora11g]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Thu Dec 22 14:37:00 2011

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

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area 2137886720 bytes
Fixed Size                  2230072 bytes
Variable Size            1493174472 bytes
Database Buffers          637534208 bytes
Redo Buffers                4947968 bytes
Database mounted.
ORA-01092: ORACLE instance terminated. Disconnection forced
ORA-00600: internal error code, arguments: [2662], [2], [2147510731], [2],
[2164287937], [4194432], [], [], [], [], [], []
Process ID: 16829
Session ID: 96 Serial number: 3

二.alert日志错误显示

Thu Dec 22 14:37:09 2011
ALTER DATABASE OPEN
LGWR: STARTING ARCH PROCESSES
Thu Dec 22 14:37:09 2011
ARC0 started with pid=20, OS id=16831 
ARC0: Archival started
LGWR: STARTING ARCH PROCESSES COMPLETE
ARC0: STARTING ARCH PROCESSES
Thu Dec 22 14:37:10 2011
ARC1 started with pid=21, OS id=16833 
Thu Dec 22 14:37:10 2011
ARC2 started with pid=22, OS id=16835 
Thu Dec 22 14:37:10 2011
ARC3 started with pid=23, OS id=16837 
ARC1: Archival started
ARC2: Archival started
ARC2: Becoming the 'no FAL' ARCH
ARC2: Becoming the 'no SRL' ARCH
ARC1: Becoming the heartbeat ARCH
Thread 1 opened at log sequence 17
  Current log# 2 seq# 17 mem# 0: /opt/oracle/oradata/ora11g/redo02.log
Successful open of redo thread 1
MTTR advisory is disabled because FAST_START_MTTR_TARGET is not set
SMON: enabling cache recovery
Errors in file /opt/oracle/diag/rdbms/ora11g/ora11g/trace/ora11g_ora_16829.trc  (incident=36156):
ORA-00600: internal error code, arguments: [2662], [2], [2147510731], [2], [2164287937], [4194432], [], [], [], [], [], []
Incident details in: /opt/oracle/diag/rdbms/ora11g/ora11g/incident/incdir_36156/ora11g_ora_16829_i36156.trc
ARC3: Archival started
ARC0: STARTING ARCH PROCESSES COMPLETE
Use ADRCI or Support Workbench to package the incident.
See Note 411.1 at My Oracle Support for error and packaging details.
Errors in file /opt/oracle/diag/rdbms/ora11g/ora11g/trace/ora11g_ora_16829.trc  (incident=36157):
ORA-00600: internal error code, arguments: [2662], [2], [2147510731], [2], [2164287937], [4194432], [], [], [], [], [], []
Incident details in: /opt/oracle/diag/rdbms/ora11g/ora11g/incident/incdir_36157/ora11g_ora_16829_i36157.trc
Dumping diagnostic data in directory=[cdmp_20111222143713], requested by (instance=1, osid=16829), summary=[incident=36156].
Use ADRCI or Support Workbench to package the incident.
See Note 411.1 at My Oracle Support for error and packaging details.
Undo initialization errored: err:600 serial:0 start:176607884 end:176611234 diff:3350 (33 seconds)
Errors in file /opt/oracle/diag/rdbms/ora11g/ora11g/trace/ora11g_ora_16829.trc:
ORA-00600: internal error code, arguments: [2662], [2], [2147510731], [2], [2164287937], [4194432], [], [], [], [], [], []
Errors in file /opt/oracle/diag/rdbms/ora11g/ora11g/trace/ora11g_ora_16829.trc:
ORA-00600: internal error code, arguments: [2662], [2], [2147510731], [2], [2164287937], [4194432], [], [], [], [], [], []
Error 600 happened during db open, shutting down database
USER (ospid: 16829): terminating the instance due to error 600
Instance terminated by USER, pid = 16829
ORA-1092 signalled during: ALTER DATABASE OPEN...
opiodr aborting process unknown ospid (16829) as a result of ORA-1092
Thu Dec 22 14:37:15 2011
ORA-1092 : opitsk aborting process

三.分析日志
ORA-00600[2662]主要参数说明见:ORA-00600 [2662]
这里补充说明:e表示出现异常问题的数据块的DBA,这里的4194432就是一个数据块的DBA

--通过DBA地址查询数据块和文件号
SQL> select dbms_utility.data_block_address_block(4194432) "blick",
  2    dbms_utility.data_block_address_file(4194432) "file" from dual;

     blick       file
---------- ----------
       128          1

--当前数据库SCN
SQL> select to_char(2147510731,'xxxxxxxxxxx') from dual;

TO_CHAR(2147
------------
    800069cb

--当前数据块SCN
SQL> select to_char(2164287937,'xxxxxxxxxxx') from dual;

TO_CHAR(2164
------------
    810069c1

四.bbed查看相关SCN

[oracle@node1 ora11g]$ bbed
Password: 
BBED-00113: Invalid password. Please rerun utility with the correct password.

[oracle@node1 ora11g]$ bbed
Password: 

BBED: Release 2.0.0.0.0 - Limited Production on Thu Dec 22 14:49:24 2011

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

************* !!! For Oracle Internal Use only !!! ***************

BBED> set filename "/opt/oracle/oradata/ora11g/system01.dbf"
        FILENAME        /opt/oracle/oradata/ora11g/system01.dbf

BBED> set block 1
        BLOCK#          1

BBED> p kcvfhckp
struct kcvfhckp, 36 bytes                   @484     
   struct kcvcpscn, 8 bytes                 @484     
      ub4 kscnbas                           @484      0x800069c8
      ub2 kscnwrp                           @488      0x0002
   ub4 kcvcptim                             @492      0x2dedee96
   ub2 kcvcpthr                             @496      0x0001
   union u, 12 bytes                        @500     
      struct kcvcprba, 12 bytes             @500     
         ub4 kcrbaseq                       @500      0x00000011
         ub4 kcrbabno                       @504      0x0000210f
         ub2 kcrbabof                       @508      0x0010
   ub1 kcvcpetb[0]                          @512      0x02
   ub1 kcvcpetb[1]                          @513      0x00
   ub1 kcvcpetb[2]                          @514      0x00
   ub1 kcvcpetb[3]                          @515      0x00
   ub1 kcvcpetb[4]                          @516      0x00
   ub1 kcvcpetb[5]                          @517      0x00
   ub1 kcvcpetb[6]                          @518      0x00
   ub1 kcvcpetb[7]                          @519      0x00

BBED> set block 128
        BLOCK#          128

BBED> p bas_kcbh
ub4 bas_kcbh                                @8        0x810069c1

BBED> p wrp_kcbh
ub2 wrp_kcbh                                @12       0x0002

这里看到的SCN(16进制)和我们在alert日志中看到的有一定的出入原因是在数据库启动的时候,当前SCN增加了,但是因为数据库直接abort,没有写入到数据文件中。导致数据文件头部的SCN比alert中显示的稍微小一点(还有可能,系统当前的scn比system01.dbf的scn大一点)。通过对比数据块和数据文件头部的SCN也可以说明当数据块的SCN>数据块当前SCN导致ORA-00600[2662]

五.bbed修改数据块的SCN

BBED> set offset 8
        OFFSET          8

BBED> m /x c8690080
BBED-00215: editing not allowed in BROWSE mode


BBED> set mode edit
        MODE            Edit

BBED> m /x c8690080
BBED-00209: invalid number (c8690080)
--分开修改,曲线救国策略

BBED> m /x c869
Warning: contents of previous BIFILE will be lost. Proceed? (Y/N) y
 File: /opt/oracle/oradata/ora11g/system01.dbf (0)
 Block: 128              Offsets:    8 to  519           Dba:0x00000000
------------------------------------------------------------------------
 c8690081 02000104 2f8f0000 00000000 00000000 00000000 00000000 06000000 
 2f000000 20100000 00000000 00000000 07000000 81004000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 06000000 00000000 00000000 
 00000040 81004000 07000000 88004000 08000000 10024000 08000000 18024000 
 08000000 20024000 08000000 28024000 08000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 

 <32 bytes per line>

BBED> set offset +2
        OFFSET          10

BBED> m /x 0080
 File: /opt/oracle/oradata/ora11g/system01.dbf (0)
 Block: 128              Offsets:   10 to  521           Dba:0x00000000
------------------------------------------------------------------------
 00800200 01042f8f 00000000 00000000 00000000 00000000 00000600 00002f00 
 00002010 00000000 00000000 00000700 00008100 40000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000600 00000000 00000000 00000000 
 00408100 40000700 00008800 40000800 00001002 40000800 00001802 40000800 
 00002002 40000800 00002802 40000800 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 

 <32 bytes per line>

BBED> p tailchk
ub4 tailchk                                 @8188     0x69c10e01

BBED> set offset 8188
        OFFSET          8188

BBED> m /x 010ec869
 File: /opt/oracle/oradata/ora11g/system01.dbf (0)
 Block: 128              Offsets: 8188 to 8191           Dba:0x00000000
------------------------------------------------------------------------
 010ec869 

 <32 bytes per line>

BBED> p tailchk
ub4 tailchk                                 @8188     0x69c80e01

BBED> p bas_kcbh
ub4 bas_kcbh                                @8        0x800069c8

BBED> sum apply
Check value for File 0, Block 128:
current = 0x8e2f, required = 0x8e2f

BBED> exit

六.启动数据库

[oracle@node1 ora11g]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Thu Dec 22 14:58:10 2011

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

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area 2137886720 bytes
Fixed Size                  2230072 bytes
Variable Size            1493174472 bytes
Database Buffers          637534208 bytes
Redo Buffers                4947968 bytes
Database mounted.
Database opened.

七.补充说明
一般遇到ORA-00600[2662]都是使用alter session set events ‘10015 trace name adjust_scn level N’;方法处理,但是有时候会遇到ORA-01031错误,那就需要请bbed帮忙处理

OS Pid: 30268 executed alter session set events '10051 trace name adjust_scn level 2'
Thu Dec 22 12:04:07 2011
Errors in file /ora101/diag/rdbms/ora11/ora11/trace/ora11_ora_30268.trc:
ORA-01031: insufficient privileges
Thu Dec 22 12:04:43 2011
Errors in file /ora101/diag/rdbms/ora11/ora11/trace/ora11_ora_846.trc:
ORA-01031: insufficient privileges

ORA-00600 [ktbdchk1: bad dscn] 解决

启动数据库报错
SQL> startup
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1260720 bytes
Variable Size             150995792 bytes
Database Buffers            8388608 bytes
Redo Buffers                7127040 bytes
Database mounted.
ORA-01092: ORACLE instance terminated. Disconnection forced

alert.log导错
Wed Aug 10 12:31:11 2011
Errors in file /u01/admin/xienfei/udump/xff_ora_8568.trc:
ORA-00600: internal error code, arguments: [ktbdchk1: bad dscn], [], [], [], [], [], [], []

xff_ora_8568.trc内容
[ktbdchk] -- readers_dsz -- bad dscn
scn: 0x0000.b1e60c00scn: 0x0000.0011fca1
*** 2011-08-10 12:31:11.998
ksedmp: internal or fatal error
ORA-00600: internal error code, arguments: [ktbdchk1: bad dscn], [], [], [], [], [], [], []
Current SQL statement for this session:
select ctime, mtime, stime from obj$ where obj# = :1
根据上面错误判断,错误的scn为b1e60c00,不是整个数据文件的scn错误
而应该是一个对象的scn错误,所以继续在xff_ora_8568.trc文件中查找b1e60c00
找到结果如下:
Block header dump:  0x0040007a
 Object id on Block? Y
 seg/obj: 0x12  csc: 0x00.b1e60c00  itc: 1  flg: -  typ: 1 - DATA
     fsl: 0  fnx: 0x0 ver: 0x01

 Itl           Xid                  Uba         Flag  Lck        Scn/Fsc
0x01   0x0008.02a.000001d9  0x00802341.01bb.04  ----    1  fsc 0x0000.0011ae7c

data_block_dump,data header at 0x20fd6044
===============
tsiz: 0x1fb8
hsiz: 0xea
pbl: 0x20fd6044
bdba: 0x0040007a
     76543210
flag=--------
ntab=1
nrow=108
frre=-1
fsbo=0xea
fseo=0x453
avsp=0x369
tosp=0x369
0xe:pti[0]      nrow=108        offs=0

根据这个提示,发现dba为:0040007a的对象异常,查找对应的file_id,block
SQL> SELECT DBMS_UTILITY.data_block_address_file (TO_NUMBER ('40007a', 'XXXXXXXX')) file_id,
  2          DBMS_UTILITY.data_block_address_block (TO_NUMBER ('40007a', 'XXXXXXXX')) block_id
  3    FROM DUAL;

   FILE_ID   BLOCK_ID
---------- ----------
         1        122

使用bbed查看file=1,block=122的scn情况
BBED> p ktbbh
struct ktbbh, 48 bytes                      @20      
   ub1 ktbbhtyp                             @20       0x01 (KDDBTDATA)
   union ktbbhsid, 4 bytes                  @24      
      ub4 ktbbhsg1                          @24       0x00000012
      ub4 ktbbhod1                          @24       0x00000012
   struct ktbbhcsc, 8 bytes                 @28      
      ub4 kscnbas                           @28       0xb1e60c00
      ub2 kscnwrp                           @32       0x0000
   b2 ktbbhict                              @36       1
   ub1 ktbbhflg                             @38       0x02 (NONE)
   ub1 ktbbhfsl                             @39       0x00
   ub4 ktbbhfnx                             @40       0x00000000
   struct ktbbhitl[0], 24 bytes             @44      
      struct ktbitxid, 8 bytes              @44      
         ub2 kxidusn                        @44       0x0008
         ub2 kxidslt                        @46       0x002a
         ub4 kxidsqn                        @48       0x000001d9
      struct ktbituba, 8 bytes              @52      
         ub4 kubadba                        @52       0x00802341
         ub2 kubaseq                        @56       0x01bb
         ub1 kubarec                        @58       0x04
      ub2 ktbitflg                          @60       0x0001 (NONE)
      union _ktbitun, 2 bytes               @62      
         b2 _ktbitfsc                       @62       0
         ub2 _ktbitwrp                      @62       0x0000
      ub4 ktbitbas                          @64       0x0011ae7c

果然发现scn为0xb1e60c00,现在把其修改为:0x00124ac6(注意规则,一般linux下都是倒序)
BBED> set offset 28
        OFFSET          28

BBED> m /x c64a1200
BBED-00209: invalid number (c64a1200)
小技巧,一次性修改报错,尝试一次修改一点

BBED> m /x c64a
 File: /u01/oradata/xienfei/system01.dbf (0)
 Block: 122              Offsets:   28 to   43           Dba:0x00000000
------------------------------------------------------------------------
 c64ae6b1 00000000 01000200 00000000 

 <32 bytes per line>

BBED> set offset +2
        OFFSET          30

BBED> m /x 1200
 File: /u01/oradata/xienfei/system01.dbf (0)
 Block: 122              Offsets:   30 to   45           Dba:0x00000000
------------------------------------------------------------------------
 12000000 00000100 02000000 00000800 

 <32 bytes per line>

BBED> set offset -2
        OFFSET          28

BBED> dump
 File: /u01/oradata/xienfei/system01.dbf (0)
 Block: 122              Offsets:   28 to   43           Dba:0x00000000
------------------------------------------------------------------------
 c64a1200 00000000 01000200 00000000 

 <32 bytes per line>

BBED> sum apply
Check value for File 0, Block 122:
current = 0x3a4e, required = 0x3a4e

SQL> startup
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1260720 bytes
Variable Size             150995792 bytes
Database Buffers            8388608 bytes
Redo Buffers                7127040 bytes
Database mounted.
Database opened.

bbed 修改datafile header

SQL> startup
ORACLE instance started.

Total System Global Area  236000356 bytes
Fixed Size                   451684 bytes
Variable Size             201326592 bytes
Database Buffers           33554432 bytes
Redo Buffers                 667648 bytes
Database mounted.
ORA-01113: file 2 needs media recovery
ORA-01110: data file 2: '/opt/oracle/oradata/xifenfei/xff01.dbf'
提示数据文件2需要恢复,首选是recover datafile 2;,如果失败,可以考虑bbed修改scn的办法

SQL> select file#,to_char(checkpoint_change#,'999999999999') from v$datafile;

     FILE# TO_CHAR(CHECK
---------- -------------
         1  107374278108
         2  107374278108
         3  107374278108
         4  107374278108
         5  107374278108
         6  107374278108
         7  107374278108
         8  107374278108
         9  107374278108
        10  107374278108
        11  107374278108

11 rows selected.

SQL> select file#,online_status,to_char(change#,'999999999999') from v$recover_file;

     FILE# ONLINE_ TO_CHAR(CHANG
---------- ------- -------------
         2 ONLINE   107374277136

[oracle@localhost tmp]$ bbed parfile=/tmp/parfile.cnf
Password: 

BBED: Release 2.0.0.0.0 - Limited Production on Mon Aug 22 06:05:34 2011

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.

************* !!! For Oracle Internal Use only !!! ***************

BBED> info
 File#  Name                                                        Size(blks)
 -----  ----                                                        ----------
     1  /opt/oracle/oradata/xifenfei/system01.dbf                         1280
     2  /opt/oracle/oradata/xifenfei/xff01.dbf                            1280

BBED> set dba 1,1
        DBA             0x00400001 (4194305 1,1)

BBED> p kcvfhckp
struct kcvfhckp, 36 bytes                   @140     
   struct kcvcpscn, 8 bytes                 @140     
*    ub4 kscnbas                           @140      0x000175dc
      ub2 kscnwrp                           @144      0x0019
* ub4 kcvcptim                             @148      0x2d49fbbb
   ub2 kcvcpthr                             @152      0x0001
   union u, 12 bytes                        @156     
      struct kcvcprba, 12 bytes             @156     
         ub4 kcrbaseq                       @156      0x00000005
         ub4 kcrbabno                       @160      0x00008e05
         ub2 kcrbabof                       @164      0x0010
      struct kcvcptr, 12 bytes              @156     
         struct kcrtrscn, 8 bytes           @156     
            ub4 kscnbas                     @156      0x00000005
            ub2 kscnwrp                     @160      0x8e05
         ub4 kcrtrtim                       @164      0x09110010
   ub1 kcvcpetb[0]                          @168      0x02
   ub1 kcvcpetb[1]                          @169      0x00
   ub1 kcvcpetb[2]                          @170      0x00
   ub1 kcvcpetb[3]                          @171      0x00
   ub1 kcvcpetb[4]                          @172      0x00
   ub1 kcvcpetb[5]                          @173      0x00
   ub1 kcvcpetb[6]                          @174      0x00
   ub1 kcvcpetb[7]                          @175      0x00

BBED> p kcvfhcpc
*ub4 kcvfhcpc                                @176      0x0000007a

BBED> p kcvfhccc
*ub4 kcvfhccc                                @184      0x00000079
星号表示使用bbed修改datafile header scn需要考虑的地方

SQL> select to_char(to_number('19000175dc','xxxxxxxxxxxx'),'999999999999') from dual;

TO_CHAR(TO_NU
-------------
 107374278108
证实system01.dbf的scn为107374278108和v$datafile查询到的一致

BBED> set dba 2,1
        DBA             0x00800001 (8388609 2,1)

BBED> p kcvfhckp
struct kcvfhckp, 36 bytes                   @140     
   struct kcvcpscn, 8 bytes                 @140     
      ub4 kscnbas                           @140      0x00017210
      ub2 kscnwrp                           @144      0x0019
   ub4 kcvcptim                             @148      0x2d49fa27
   ub2 kcvcpthr                             @152      0x0001
   union u, 12 bytes                        @156     
      struct kcvcprba, 12 bytes             @156     
         ub4 kcrbaseq                       @156      0x00000005
         ub4 kcrbabno                       @160      0x00006f18
         ub2 kcrbabof                       @164      0x0010
      struct kcvcptr, 12 bytes              @156     
         struct kcrtrscn, 8 bytes           @156     
            ub4 kscnbas                     @156      0x00000005
            ub2 kscnwrp                     @160      0x6f18
         ub4 kcrtrtim                       @164      0x09110010
   ub1 kcvcpetb[0]                          @168      0x02
   ub1 kcvcpetb[1]                          @169      0x00
   ub1 kcvcpetb[2]                          @170      0x00
   ub1 kcvcpetb[3]                          @171      0x00
   ub1 kcvcpetb[4]                          @172      0x00
   ub1 kcvcpetb[5]                          @173      0x00
   ub1 kcvcpetb[6]                          @174      0x00
   ub1 kcvcpetb[7]                          @175      0x00

BBED> p kcvfhcpc
ub4 kcvfhcpc                                @176      0x00000034

BBED> p kcvfhccc
ub4 kcvfhccc                                @184      0x00000033


SQL> select to_char(to_number('1900017210','xxxxxxxxxxxx'),'999999999999') from dual;

TO_CHAR(TO_NU
-------------
 107374277136
和v$recover_file视图中查询出来一致

BBED> set dba 2,1 offset 140
        DBA             0x00800001 (8388609 2,1)
        OFFSET          140

BBED> show 
        FILE#           2
        BLOCK#          1
        OFFSET          140
        DBA             0x00800001 (8388609 2,1)
        FILENAME        /opt/oracle/oradata/xifenfei/xff01.dbf
        BIFILE          bifile.bbd
        LISTFILE        /tmp/list
        BLOCKSIZE       8192
        MODE            Edit
        EDIT            Unrecoverable
        IBASE           Dec
        OBASE           Dec
        WIDTH           80
        COUNT           32
        LOGFILE         log.bbd
        SPOOL           No

BBED> m /x dc750100
 File: /opt/oracle/oradata/xifenfei/xff01.dbf (2)
 Block: 1                Offsets:  140 to  171           Dba:0x00800001
------------------------------------------------------------------------
 dc750100 19000000 27fa492d 01000000 05000000 186f0000 10001109 02000000 

 <32 bytes per line>

BBED> m /x bbfb492d offset 158
 File: /opt/oracle/oradata/xifenfei/xff01.dbf (2)
 Block: 1                Offsets:  158 to  189           Dba:0x00800001
------------------------------------------------------------------------
 bbfb492d 00001000 11090200 00000000 00003400 00004cc0 492d3300 00000000 

 <32 bytes per line>

BBED> m /x 7a000000 offset 176
 File: /opt/oracle/oradata/xifenfei/xff01.dbf (2)
 Block: 1                Offsets:  176 to  207           Dba:0x00800001
------------------------------------------------------------------------
 7a000000 4cc0492d 33000000 00000000 00000000 00000000 00000000 00000000 

 <32 bytes per line>

BBED> m /x 79000000 offset 184
 File: /opt/oracle/oradata/xifenfei/xff01.dbf (2)
 Block: 1                Offsets:  184 to  215           Dba:0x00800001
------------------------------------------------------------------------
 79000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 

 <32 bytes per line>
注意:p打印出来的值和m修改的值可能不是完全一致(和cpu的计算类型有关,常用的intel cpu中是倒序存储),可以选择先dump正确的相关值,然后m修改
如:p打印出来的值为:80084d1f时,m修改时要为: 1f4d0880

BBED> sum apply dba 2,1
Check value for File 2, Block 1:
current = 0x7ece, required = 0x7ece


SQL> shutdown immediate
ORA-01109: database not open

Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area  236000356 bytes
Fixed Size                   451684 bytes
Variable Size             201326592 bytes
Database Buffers           33554432 bytes
Redo Buffers                 667648 bytes
Database mounted.
ORA-01122: database file 2 failed verification check
ORA-01110: data file 2: '/opt/oracle/oradata/xifenfei/xff01.dbf'
ORA-01207: file is more recent than controlfile - old controlfile
提示控制文件太老,需要重建控制文件

SQL> shutdown abort
ORACLE instance shut down.
SQL> STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "XIFENFEI" NORESETLOGS  ARCHIVELOG
    MAXLOGFILES 50
    MAXLOGMEMBERS 5
    MAXDATAFILES 100
    MAXINSTANCES 1
    MAXLOGHISTORY 226
LOGFILE
  GROUP 1 '/opt/oracle/oradata/xifenfei/redo01.log'  SIZE 100M,
  GROUP 2 '/opt/oracle/oradata/xifenfei/redo02.log'  SIZE 100M,
  GROUP 3 '/opt/oracle/oradata/xifenfei/redo03.log'  SIZE 100M
DATAFILE
  '/opt/oracle/oradata/xifenfei/system01.dbf',
  '/opt/oracle/oradata/xifenfei/xff01.dbf',
  '/opt/oracle/oradata/xifenfei/cwmlite01.dbf',
  '/opt/oracle/oradata/xifenfei/drsys01.dbf',
  '/opt/oracle/oradata/xifenfei/example01.dbf',
  '/opt/oracle/oradata/xifenfei/indx01.dbf',
  '/opt/oracle/oradata/xifenfei/odm01.dbf',
  '/opt/oracle/oradata/xifenfei/tools01.dbf',
  '/opt/oracle/oradata/xifenfei/users01.dbf',
  '/opt/oracle/oradata/xifenfei/xdb01.dbf',
  '/opt/oracle/oradata/xifenfei/UNDOTBS01.dbf'
CHARACTER SET ZHS16GBK
;
ORACLE instance started.

Total System Global Area  236000356 bytes
Fixed Size                   451684 bytes
Variable Size             201326592 bytes
Database Buffers           33554432 bytes
Redo Buffers                 667648 bytes
SQL>   2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   
Control file created.

SQL> alter database open;

Database altered.

SQL> ALTER TABLESPACE TEMP ADD TEMPFILE '/opt/oracle/oradata/xifenfei/temp01.dbf'
  2       SIZE 32505856  REUSE AUTOEXTEND ON NEXT 655360  MAXSIZE 32767M;
重建控制文件后,需要添加临时文件

补充说明:
1、datafile 的file header 存储在第一个block里
2、Oracle considers four attributes of this data structure when determining if a datafile is sync with the other data files of the database:(不同oracle版本offset可能不同)
(1)kscnbas (at offset 140) – SCN of last change to the datafile.
(2)kcvcptim (at offset 148) -Time of the last change to the datafile.
(3)kcvfhcpc (at offset 176) – Checkpoint count.
(4)kcvfhccc (at offset 184) – Unknown, but is always 1 less than thecheckpoint point count.
Oracle有4个属性来判断datafile 是否和其他的datafile 一致,如果都一致,可以正常操作,如果不一致,那么会报ORA-01113错误

通过bbed查看数据块结构

BBED> map /v
 File: /opt/oracle/oradata/xifenfei/xff01.dbf (2)
 Block: 530                                   Dba:0x00800212
------------------------------------------------------------
 KTB Data Block (Table/Cluster)

 struct kcbh, 20 bytes                      @0       
    ub1 type_kcbh                           @0       
    ub1 frmt_kcbh                           @1       
    ub1 spare1_kcbh                         @2       
    ub1 spare2_kcbh                         @3       
    ub4 rdba_kcbh                           @4       
    ub4 bas_kcbh                            @8       
    ub2 wrp_kcbh                            @12      
    ub1 seq_kcbh                            @14      
    ub1 flg_kcbh                            @15      
    ub2 chkval_kcbh                         @16      
    ub2 spare3_kcbh                         @18      

 struct ktbbh, 72 bytes                     @20      
    ub1 ktbbhtyp                            @20      
    union ktbbhsid, 4 bytes                 @24      
    struct ktbbhcsc, 8 bytes                @28      
    b2 ktbbhict                             @36      
    ub1 ktbbhflg                            @38      
    ub1 ktbbhfsl                            @39      
    ub4 ktbbhfnx                            @40      
    struct ktbbhitl[2], 48 bytes            @44      

 struct kdbh, 14 bytes                      @92      
    ub1 kdbhflag                            @92      
    b1 kdbhntab                             @93      
    b2 kdbhnrow                             @94      
    sb2 kdbhfrre                            @96      
    sb2 kdbhfsbo                            @98      
    sb2 kdbhfseo                            @100     
    b2 kdbhavsp                             @102     
    b2 kdbhtosp                             @104     

 struct kdbt[1], 4 bytes                    @106     
    b2 kdbtoffs                             @106     
    b2 kdbtnrow                             @108     

 sb2 kdbr[101]                              @110     

 ub1 freespace[4270]                        @312     

 ub1 rowdata[3606]                          @4582    

 ub4 tailchk                                @8188    


BBED> 


////////////////////////////////////////////////////////////////////////////////////////
//////////// Block Header Structure,  20 bytes//////////////////////////////////////////
BBED> p kcbh
struct kcbh, 20 bytes                       @0       
    ub1 type_kcbh                           @0    -- Block Type 
                                                  -- 01 - Undo segment header
                                                  -- 02 - Undo data block
                                                  -- 03 - Save undo header
                                                  -- 04 - Save undo data block
                                                  -- 05 - Data segment header  
                                                  -- 06 - Trans data, KTB managed data block(with ITL)
                                                  -- 07 - Temp table data block (no ITL)
                                                  -- 08 - Sort key
                                                  -- 09 - Sort Run
                                                  -- 10 - Segment free list block
                                                  -- 11 - Data file header
    ub1 frmt_kcbh                           @1    -- Block Format 1=Oracle7, 2=Oracle8+   
    ub1 spare1_kcbh                         @2    -- Not used, filler field   
    ub1 spare2_kcbh                         @3    -- Not used, filler field   
    ub4 rdba_kcbh                           @4    -- RDBA (4 bytes) - Relative Data Block Address   
    ub4 bas_kcbh                            @8    -- SCN Base (4 bytes)  
    ub2 wrp_kcbh                            @12   -- SCN Wrap (2 bytes)   
    ub1 seq_kcbh                            @14   -- Sequence Number, incremented for every change made to the block at the same SCN    
    ub1 flg_kcbh                            @15   -- Flag:   
                                                  -- 0x01 New Block
                                                  -- 0x02 Delayed Logging Chang advanced SCN/seq 
                                                  -- 0x04 Check value saved - block XOR's to Zero
                                                  -- 0x08 Temporary block
    ub2 chkval_kcbh                         @16   -- Optional block checksum (if DB_BLOCK_CHECKSUM=TRUE)   
    ub2 spare3_kcbh                         @18   -- Not used, filler field  
 
 /////////////////////////////////////////////////////////////////////////////////////////
/////////Transaction Fixed Header Structure, 72 Bytes////////////////////////////////////
BBED> p ktbbh
 struct ktbbh, 72 bytes                     @20      
    ub1 ktbbhtyp                            @20     -- Block type (1=DATA, 2=INDEX) 
    union ktbbhsid, 4 bytes                 @24     -- Segment/Object ID
    struct ktbbhcsc, 8 bytes                @28     -- SCN at last block cleanout 
    b2 ktbbhict                             @36     -- Number of ITL slots 
    ub1 ktbbhflg                            @38     -- 0=on the freelist 
    ub1 ktbbhfsl                            @39     -- ITL TX freelist slot 
    ub4 ktbbhfnx                            @40     -- DBA of next block on the freelist 
    struct ktbbhitl[2], 48 bytes            @44     -- ITL list index, each ITL takes up 24 bytes 

//////////////////////////////////////////////////////////////////////////////////////////
///////////////Data Header Structure, 14 bytes////////////////////////////////////////////
BBED> p kdbh
 struct kdbh, 14 bytes                      @100     
    ub1 kdbhflag                            @100    -- N=pctfree hit(clusters)
                                                    -- F=do not put on freelist
                                                    -- K=flushable cluster keys 
    b1 kdbhntab                             @101    -- Number of tables (>1 in clusters) 
    b2 kdbhnrow                             @102    -- Number of rows (2 bytes)
    sb2 kdbhfrre                            @104    -- First free row entry index; -1=you have to add one 
    sb2 kdbhfsbo                            @106    -- Freespace begin offset 
    sb2 kdbhfseo                            @108    -- Freespace end offset 
    b2 kdbhavsp                             @110    -- Available space in the block 
    b2 kdbhtosp                             @112    -- Total available space when all TXs commit 

////////////////////////////////////////////////////////////////////////////////////////
/////////////////////Table Directory Entry Structure, 4 bytes///////////////////////////
BBED> p kdbt
 struct kdbt[1], 4 bytes                    @114     
    b2 kdbtoffs                             @114     
    b2 kdbtnrow                             @116     

////////////////////////////////////////////////////////////////////////////////////////
////////////////// Row Directory ///////////////////////////////////////////////////////
BBED> p kdbr[100]
 sb2 kdbr[100]                                @310     


////////////////////////////////////////////////////////////////////////////////////////
///////////////// Free Space ///////////////////////////////////////////////////////////
BBED> p freespace[4269]
 ub1 freespace[4269]                        @4581     

///////////////////////////////////////////////////////////////////////////////////////
/////////////////////Row Data//////////////////////////////////////////////////////////
BBED> p rowdata[3605]
ub1 rowdata[3605]                           @8187     0x00   
 
//////////////////////////////////////////////////////////////////////////////////////
/////////////////////Block Tail Check, 4 bytes////////////////////////////////////////
BBED> p tailchk
ub4 tailchk                                 @8188     0x24500601  

说明事宜:
1、tailchk=Lower order two bytes of SCN Base(bas_kcbh) + Block Type(type_kcbh) + SCN Seq(seq_kcbh)
2、块的scn为:scn=wrp_kcbh+bas_kcbh
求scn语句:select to_char(to_number(‘scn’,’xxxxxxxxxx’),’999999999999′) from dual;
3、dba求文件号,块号为:

set serveroutput on
declare
   p_dba   VARCHAR2 (255) :='0x00800212';
   l_str   VARCHAR2 (255) DEFAULT NULL;
BEGIN
    l_str :=
         'datafile# is:'
      || DBMS_UTILITY.data_block_address_file (TO_NUMBER (LTRIM (p_dba, '0x'),'xxxxxxxx'))
      || chr(10)||'datablock is:'
      || DBMS_UTILITY.data_block_address_block (TO_NUMBER (LTRIM (p_dba, '0x'),'xxxxxxxx'));
   dbms_output.put_line(l_str);
END;