關於控制檔案與資料檔案頭資訊的說明(zt)

tolywang發表於2007-09-27
為了回答一些疑問,引出本系列文章,討論連結參考:

http://www.itpub.net/609499.html

當我們使用file_hdrs事件來轉儲資料檔案頭資訊時,Oracle會轉儲兩部分資訊,一部分來自控制檔案,一部分來自資料檔案,在資料庫啟動過程中,這兩部分資訊要用來進行啟動驗證。


在資料庫open的過程中,Oracle要進行檢查中包含以下兩個過程:

第一次檢查資料檔案頭中的Checkpoint cnt是否與對應控制檔案中的Checkpoint cnt一致.如果相等,進行第二次檢查.

第二次檢查資料檔案頭的開始SCN和對應控制檔案中的結束SCN是否一致如果結束SCN等於開始SCN,則不需要對那個檔案進行恢復.對每個資料檔案都完成檢查後,開啟資料庫.同時將每個資料檔案的結束SCN設定為無窮大.

透過以下過程我們來進一步說明一下這個內容。

我們來看以下來自控制檔案部分(選取一個檔案測試):

DATA FILE #4:
(name #4) /opt
creation size=0 block size=8192 status=0xe head=4 tail=4 dup=1
tablespace 4, index=4 krfil=4 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:58 scn: 0x0000.002ac8ee 08/11/2006 09:48:29
Stop scn: 0x0000.002ac8ee 08/11/2006 09:48:29
Creation Checkpointed at scn: 0x0000.0015078d 06/06/2006 09:41:54
thread:0 rba:(0x0.0.0)
................
aux_file is NOT DEFINED

這部分中包含的重要資訊有:
檢查點計數: Checkpoint cnt:58
檢查點SCN: scn: 0x0000.002ac8ee 08/11/2006 09:48:29
資料檔案Stop SCN:Stop scn: 0x0000.002ac8ee 08/11/2006 09:48:29

我們再看來自資料檔案頭的資訊:

FILE HEADER:
Software vsn=153092096=0x9200000, Compatibility Vsn=134217728=0x8000000
Db ID=1407686520=0x53e79778, Db Name='EYGLE'
Activation ID=0=0x0
Control Seq=979=0x3d3, File size=1280=0x500
File Number=4, Blksiz=8192, File Type=3 DATA
Tablespace #4 - EYGLE rel_fn:4
Creation at scn: 0x0000.0015078d 06/06/2006 09:41:54
Backup taken at scn: 0x0000.00000000 01/01/1988 00:00:00 thread:0
reset logs count:0x232bee1f scn: 0x0000.0007c781 recovered at 08/10/2006 20:57:53
status:0x0 root dba:0x00000000 chkpt cnt: 58 ctl cnt:57
begin-hot-backup file size: 0
Checkpointed at scn: 0x0000.002ac8ee 08/11/2006 09:48:29
....................... [Page]

這部分中包含的重要資訊有:
檢查點SCN: Checkpointed at scn: 0x0000.002ac8ee 08/11/2006 09:48:29
檢查點計數: chkpt cnt: 58 ctl cnt:57

這兩者都和控制檔案中所記錄的一致。如果這兩者一致,資料庫啟動時就能透過驗證,啟動資料庫。

那麼如果不一致呢?
Oracle則請求進行恢復。
我們看,從備份中恢復eygle01.dbf檔案.
首先第一部分從控制檔案中獲得的資訊是相同的:

DATA FILE #4:
(name #4) /opt
creation size=0 block size=8192 status=0xe head=4 tail=4 dup=1
tablespace 4, index=4 krfil=4 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:58 scn: 0x0000.002ac8ee 08/11/2006 09:48:29
Stop scn: 0x0000.002ac8ee 08/11/2006 09:48:29
Creation Checkpointed at scn: 0x0000.0015078d 06/06/2006 09:41:54
...................
aux_file is NOT DEFINED

檢查點計數: Checkpoint cnt:58
檢查點SCN: scn: 0x0000.002ac8ee 08/11/2006 09:48:29
資料檔案Stop SCN:Stop scn: 0x0000.002ac8ee 08/11/2006 09:48:29

而從檔案頭中獲得的備份檔案資訊則是:

FILE HEADER:
Software vsn=153092096=0x9200000, Compatibility Vsn=134217728=0x8000000
Db ID=1407686520=0x53e79778, Db Name='EYGLE'
Activation ID=0=0x0
Control Seq=973=0x3cd, File size=1280=0x500
File Number=4, Blksiz=8192, File Type=3 DATA
Tablespace #4 - EYGLE rel_fn:4
Creation at scn: 0x0000.0015078d 06/06/2006 09:41:54
Backup taken at scn: 0x0000.00000000 01/01/1988 00:00:00 thread:0
reset logs count:0x232bee1f scn: 0x0000.0007c781 recovered at 08/10/2006 20:57:53
status:0x0 root dba:0x00000000 chkpt cnt: 53 ctl cnt:52
begin-hot-backup file size: 0
Checkpointed at scn: 0x0000.002ac5f9 08/10/2006 20:58:21
...................................

我們看到此時備份檔案的資訊:
檢查點是:Checkpointed at scn: 0x0000.002ac5f9 08/10/2006 20:58:21
檢查點計數為:chkpt cnt: 53 ctl cnt:52

這兩者不再一致,首先是檢查點技術不一致,當前檔案的chkpt cnt為53,小於控制檔案中記錄的58,Oracle可以判斷檔案是從備份中恢復的,或者檔案故障,需要進行介質恢復。

我們看如果此時我們試圖開啟資料庫,則Oracle提示檔案需要介質恢復:

SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01113: file 4 needs media recovery
ORA-01110: data file 4: '/opt'

執行恢復:

SQL> recover datafile 4;
Media recovery complete.

我們看看恢復完成之後,控制檔案和資料檔案的變化.
首先看控制檔案的變化:

DATA FILE #4:
(name #4) /opt
creation size=0 block size=8192 status=0xe head=4 tail=4 dup=1
tablespace 4, index=4 krfil=4 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:59 scn: 0x0000.002ac8ee 08/11/2006 09:48:29
Stop scn: 0x0000.002ac8ed 08/11/2006 09:48:29
Creation Checkpointed at scn: 0x0000.0015078d 06/06/2006 09:41:54
......................

檢查點計數: Checkpoint cnt:59
執行了恢復之後,檢查點計數較前增加了1

檢查點SCN: scn: 0x0000.002ac8ee 08/11/2006 09:48:29
資料檔案Stop scn: 0x0000.002ac8ed 08/11/2006 09:48:29
資料檔案Stop scn和資料檔案進行了同步。

資料檔案頭資訊:

FILE HEADER:
Software vsn=153092096=0x9200000, Compatibility Vsn=134217728=0x8000000
Db ID=1407686520=0x53e79778, Db Name='EYGLE'
Activation ID=0=0x0
Control Seq=983=0x3d7, File size=1280=0x500
File Number=4, Blksiz=8192, File Type=3 DATA
Tablespace #4 - EYGLE rel_fn:4
Creation at scn: 0x0000.0015078d 06/06/2006 09:41:54
Backup taken at scn: 0x0000.00000000 01/01/1988 00:00:00 thread:0
reset logs count:0x232bee1f scn: 0x0000.0007c781 recovered at 08/11/2006 10:11:26
status:0x0 root dba:0x00000000 chkpt cnt: 59 ctl cnt:58
begin-hot-backup file size: 0
Checkpointed at scn: 0x0000.002ac8ed 08/11/2006 09:48:29
..........................

我們看到此時資料檔案的資訊:
檢查點是:Checkpointed at scn: 0x0000.002ac8ed 08/11/2006 09:48:29
這個檢查點和控制檔案中記錄的stop scn一致,資料庫啟動可以順利進行。

檢查點計數為:chkpt cnt: 59 ctl cnt:58

我們開啟資料庫:

SQL> alter database open;
Database altered.
SQL> alter session set events 'immediate trace name file_hdrs level 10';
Session altered.

此時資料庫恢復正常執行。
控制檔案資訊如下:

DATA FILE #4:
(name #4) /opt
creation size=0 block size=8192 status=0xe head=4 tail=4 dup=1
tablespace 4, index=4 krfil=4 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:60 scn: 0x0000.002ac8ef 08/11/2006 10:19:30
Stop scn: 0xffff.ffffffff 08/11/2006 09:48:29
Creation Checkpointed at scn: 0x0000.0015078d 06/06/2006 09:41:54

此時stop scn被置為無窮大。
資料檔案頭資訊如下:

FILE HEADER:
Software vsn=153092096=0x9200000, Compatibility Vsn=134217728=0x8000000
Db ID=1407686520=0x53e79778, Db Name='EYGLE'
Activation ID=0=0x0
Control Seq=984=0x3d8, File size=1280=0x500
File Number=4, Blksiz=8192, File Type=3 DATA
Tablespace #4 - EYGLE rel_fn:4
Creation at scn: 0x0000.0015078d 06/06/2006 09:41:54
Backup taken at scn: 0x0000.00000000 01/01/1988 00:00:00 thread:0
reset logs count:0x232bee1f scn: 0x0000.0007c781 recovered at 08/11/2006 10:11:26
status:0x4 root dba:0x00000000 chkpt cnt: 60 ctl cnt:59
begin-hot-backup file size: 0
Checkpointed at scn: 0x0000.002ac8ef 08/11/2006 10:19:30

我們可以想象,如果控制檔案是從備份中恢復的,那麼資料庫在open過程中又將如何呢?

首先備份控制檔案,開啟資料庫,增進檢查點:

[oracle@jumper eygle]$ cp control01.ctl control01.ctl.bak
[oracle@jumper eygle]$ sqlplus "/ as sysdba"
SQL*Plus: Release 9.2.0.4.0 - Production on Fri Aug 11 10:46:05 2006
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to an idle instance.
SQL> startup
ORACLE instance started.
Total System Global Area 139531744 bytes
Fixed Size 452064 bytes
Variable Size 121634816 bytes
Database Buffers 16777216 bytes
Redo Buffers 667648 bytes
Database mounted.
Database opened.
SQL> alter system checkpoint;
System altered.
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> exit
Disconnected from Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning option
JServer Release 9.2.0.4.0 - Production

然後恢復舊的控制檔案,mount資料庫,轉儲資料檔案頭:

[oracle@jumper eygle]$ mv control01.ctl control01.ctl.n
[oracle@jumper eygle]$ mv control01.ctl.bak control01.ctl
[oracle@jumper eygle]$ sqlplus "/ as sysdba"
SQL*Plus: Release 9.2.0.4.0 - Production on Fri Aug 11 10:46:50 2006
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to an idle instance.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 139531744 bytes
Fixed Size 452064 bytes
Variable Size 121634816 bytes
Database Buffers 16777216 bytes
Redo Buffers 667648 bytes
Database mounted.
SQL> alter session set events 'immediate trace name file_hdrs level 10';
Session altered.
SQL> ![Page]

我們看控制檔案的資訊(選擇一個檔案):

DATA FILE #4:
(name #4) /opt
creation size=0 block size=8192 status=0xe head=4 tail=4 dup=1
tablespace 4, index=4 krfil=4 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:61 scn: 0x0000.002acb1e 08/11/2006 10:44:38
Stop scn: 0x0000.002acb1e 08/11/2006 10:44:38
Creation Checkpointed at scn: 0x0000.0015078d 06/06/2006 09:41:54

再看資料檔案頭資訊:

FILE HEADER:
Software vsn=153092096=0x9200000, Compatibility Vsn=134217728=0x8000000
Db ID=1407686520=0x53e79778, Db Name='EYGLE'
Activation ID=0=0x0
Control Seq=989=0x3dd, File size=1280=0x500
File Number=4, Blksiz=8192, File Type=3 DATA
Tablespace #4 - EYGLE rel_fn:4
Creation at scn: 0x0000.0015078d 06/06/2006 09:41:54
Backup taken at scn: 0x0000.00000000 01/01/1988 00:00:00 thread:0
reset logs count:0x232bee1f scn: 0x0000.0007c781 recovered at 08/11/2006 10:11:26
status:0x0 root dba:0x00000000 chkpt cnt: 64 ctl cnt:63
begin-hot-backup file size: 0
Checkpointed at scn: 0x0000.002acb98 08/11/2006 10:46:24

我們注意到資料檔案的chkpt cnt: 64 要大約控制檔案的Checkpoint cnt:61,也就是說控制檔案是舊的。此時嘗試開啟資料庫就會出現如下錯誤:

[oracle@jumper udump]$ sqlplus "/ as sysdba"
SQL*Plus: Release 9.2.0.4.0 - Production on Fri Aug 11 10:51:20 2006
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning option
JServer Release 9.2.0.4.0 - Production
SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01122: database file 1 failed verification check
ORA-01110: data file 1: '/opt'
ORA-01207: file is more recent than controlfile - old controlfile

Oracle告訴我們,控制檔案是舊的。此時我們可以透過重建控制檔案或者從舊的資料備份開始恢復。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/35489/viewspace-84915/,如需轉載,請註明出處,否則將追究法律責任。

相關文章