control中有三種SCN分別爲,system SCN、datafile SCN、last SCN,數據文件頭中有一種SCN start SCN
system scn從視圖v$database中得到,對應checkpoint_change#字段,datafile scn、last scn分別對應視圖v$datafile中的checkpoint_change#,last_change#,而 start scn則從v$datafile_header中checkpoint_change#獲得。
數據庫在正常啓動後下,system scn,datafile scn,start scn會相等,而last scn會被置於無窮大,這裏爲null。
正常關閉後(immediate,noraml,translate),上面四個scn會應執行full checkpoint 而相等。
當系統在非正常關閉後,如shutdown abort,這個時候last scn依然爲無窮大,那麼當從新啓動實例時,系統首先會比較start scn與system scn,若是一致,那麼再比較start scn 與last scan是否同樣大,由於是非正常關閉,這裏會不同大,那麼就須要例程恢復。
若是打開數據庫時發現system scn>datafile scn,那麼覺得着使用舊的備份數據文件,也就是須要介質恢復
若是是system scn<datafile scn,及控制文件scn是舊的,表明使用了老的控制文件,須要recover using backup controlfile進行恢復。
html
1、正常啓動時
SQL> select checkpoint_change# from v$database; --控制文件中的scn數據庫
CHECKPOINT_CHANGE#
------------------
5534071
SQL> select file#,checkpoint_change# from v$datafile_header; --start scnide
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 5534071
2 5534071
3 5534071
4 5534071
5 5534071
SQL> select file#,checkpoint_change#,last_change# from v$datafile; --datafile scn & last scnlua
FILE# CHECKPOINT_CHANGE# LAST_CHANGE#
---------- ------------------ ------------
1 5534071
2 5534071
3 5534071
4 5534071
5 5534071
2、正常關閉後,而後在startup mount;
SQL> shutdown immediate;
數據庫已經關閉。
已經卸載數據庫。
ORACLE 例程已經關閉。
SQL> startup mount;
ORACLE 例程已經啓動。spa
Total System Global Area 319888364 bytes
Fixed Size 453612 bytes
Variable Size 192937984 bytes
Database Buffers 125829120 bytes
Redo Buffers 667648 bytes
數據庫裝載完畢。
SQL> select file#,checkpoint_change# from v$datafile_header;日誌
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 5534485
2 5534485
3 5534485
4 5534485
5 5534485htm
SQL> select checkpoint_change# from v$database;事務
CHECKPOINT_CHANGE#
------------------
5534485get
SQL> select file#,checkpoint_change#,last_change# from v$datafile;it
FILE# CHECKPOINT_CHANGE# LAST_CHANGE#
---------- ------------------ ------------
1 5534485 5534485
2 5534485 5534485
3 5534485 5534485
4 5534485 5534485
5 5534485 5534485
--發現start scn=last scn,證實系統是正常關閉
SQL> alter database open;
數據庫已更改。
3、在正常打開狀態下進行事務操做
SQL> create table t(a number);
表已建立。
SQL> insert into t values (1);
已建立 1 行。
SQL> commit;
提交完成。
SQL> insert into t values(2);
已建立 1 行。
4、非正常關閉
SQL> shutdown abort;
ORACLE 例程已經關閉。
SQL>
5、打開到mount狀態下,觀看scn
SQL> startup mount;
ORACLE 例程已經啓動。
Total System Global Area 319888364 bytes
Fixed Size 453612 bytes
Variable Size 192937984 bytes
Database Buffers 125829120 bytes
Redo Buffers 667648 bytes
數據庫裝載完畢。
SQL> select file#,checkpoint_change#,last_change# from v$datafile;
FILE# CHECKPOINT_CHANGE# LAST_CHANGE#
---------- ------------------ ------------
1 5534486
2 5534486
3 5534486
4 5534486
5 5534486
SQL> select checkpoint_change# from v$database;
CHECKPOINT_CHANGE#
------------------
5534486
SQL> select file#,checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 5534486
2 5534486
3 5534486
4 5534486
5 5534486
--這時發現start scn 與last scn不等,last scn爲無窮大,須要例程恢復
6、改變數據庫狀態爲open,並查看該階段運行日誌
SQL> select * from wen.t;
select * from wen.t
*
ERROR 位於第 1 行:
ORA-01219: 數據庫未打開: 僅容許在固定表/視圖中查詢
SQL> alter database open;
數據庫已更改。
SQL> select * from wen.t;
A
----------
1
--發現沒有提交的事務丟失。
查看日誌以下:
Completed: ALTER DATABASE MOUNT
Wed May 17 21:35:46 2006
alter database open
Wed May 17 21:35:46 2006
Beginning crash recovery of 1 threads --會自動判斷是否須要恢復,這裏開始例程恢復
Wed May 17 21:35:46 2006
Started first pass scan
Wed May 17 21:35:47 2006
Completed first pass scan
206 redo blocks read, 90 data blocks need recovery
Wed May 17 21:35:47 2006
Started recovery at
Thread 1: logseq 167, block 271, scn 0.0
Recovery of Online Redo Log: Thread 1 Group 2 Seq 167 Reading mem 0 --恢復用的在線重作日誌
Mem# 0 errs 0: D:ORACLEORADATADB1REDO02.LOG
Wed May 17 21:35:47 2006
Ended recovery at
Thread 1: logseq 167, block 477, scn 0.5554724
90 data blocks read, 90 data blocks written, 206 redo blocks read
Crash recovery completed successfully --恢復完成
Wed May 17 21:35:47 2006
LGWR: Primary database is in CLUSTER CONSISTENT mode
Thread 1 advanced to log sequence 168
Thread 1 opened at log sequence 168
Current log# 3 seq# 168 mem# 0: D:ORACLEORADATADB1REDO03.LOG
Successful open of redo thread 1.
Wed May 17 21:35:48 2006
SMON: enabling cache recovery
Wed May 17 21:35:48 2006
ARC0: Evaluating archive log 2 thread 1 sequence 167
ARC0: Beginning to archive log 2 thread 1 sequence 167
Creating archive destination LOG_ARCHIVE_DEST_1: 'D:DBBKARC00167.001'
ARC0: Completed archiving log 2 thread 1 sequence 167
Wed May 17 21:35:48 2006
Undo Segment 1 Onlined
Undo Segment 2 Onlined
Undo Segment 3 Onlined
Undo Segment 4 Onlined
Undo Segment 5 Onlined
Undo Segment 6 Onlined