ORA-01113問題的簡單分析
在啟動資料庫的時候,open階段總是可能出現各種各樣的問題,比如讓人膽戰心驚的錯誤。ORA-01113: file 1 needs media recovery
自己留意了一下,其實還是有蠻多的情境會出現這個問題,有些細節可能沒有注意到就會出現這個問題,
比如我們重建控制檔案的時候。
在重建控制檔案之前做了shutdown abort的操作。
SQL> shutdown abort
Oracle instance shut down.
SQL> startup nomount
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1261564 bytes
Variable Size 163577860 bytes
Database Buffers 142606336 bytes
Redo Buffers 7127040 bytes
SQL> CREATE CONTROLFILE REUSE DATABASE "TEST10G" NORESETLOGS NOARCHIVELOG
.....
26 '/u02/oracle/oradata/data02.dbf'
27 CHARACTER SET US7ASCII
28 ;
Control file created.
嘗試啟動資料庫的時候就會拋出這個錯誤。
SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01113: file 1 needs media recovery
ORA-01110: data file 1: '/u02/oracle/oradata/TEST10G/disk5/system01.dbf'
其實這個時候簡單分析一下就會明白,上次是shutdown abort的方式,則對應的檢查點資訊無法寫入資料檔案,在open階段smon會做這個校正。
開始在後台做資料的前滾,然後應用redo日誌的資料,對某些操作做相應的復原。
從下面的地方可以看出 last_change#沒有任何值,表明上次斷電重啟後檢查點資訊沒有寫入。
SQL> select file#,checkpoint_change#,last_change# from v$datafile;
FILE# CHECKPOINT_CHANGE# LAST_CHANGE#
---------- ------------------ ------------
1 858940
2 858940
3 858940
4 858940
5 858940
SQL> select file#,checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 858940
2 858940
3 858940
4 858940
5 858940
SQL> select checkpoint_change# from v$database;
CHECKPOINT_CHANGE#
------------------
858939
這個時候嘗試恢複資料庫,再次觀察,則相應的檢查點資訊就做了校正。
SQL> recover database;
Media recovery complete.
SQL> select checkpoint_change# from v$database;
CHECKPOINT_CHANGE#
------------------
858939
SQL> select file#,checkpoint_change#,last_change# from v$datafile;
FILE# CHECKPOINT_CHANGE# LAST_CHANGE#
---------- ------------------ ------------
1 859017 859017
2 859017 859017
3 859017 859017
4 859017 859017
5 859017 859017
SQL> alter database open;
Database altered.
資料庫啟動之後,last_change#的值又迴歸零。等待稍後的檢查點寫入。
SQL> select file#,checkpoint_change#,last_change# from v$datafile;
FILE# CHECKPOINT_CHANGE# LAST_CHANGE#
---------- ------------------ ------------
1 879019
2 879019
3 879019
4 879019
5 879019
SQL> select checkpoint_change# from v$database;
CHECKPOINT_CHANGE#
------------------
879019
其實這個過程中,恢複的基準就是檢查點,也就是SCN.
當然在有些操作依賴于歸檔模式,介質恢複還是依賴於一些歸檔檔案的。
像在非歸檔模式嘗試下面的操作就不可行。
SQL> alter tablespace data offline immediate;
alter tablespace data offline immediate
*
ERROR at line 1:
ORA-01145: offline immediate disallowed unless media recovery enabled
SQL> alter tablespace data begin backup;
alter tablespace data begin backup
*
ERROR at line 1:
ORA-01123: cannot start online backup; media recovery not enabled
這個時候還是啟用歸檔。
SQL> shut immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1261564 bytes
Variable Size 163577860 bytes
Database Buffers 142606336 bytes
Redo Buffers 7127040 bytes
Database mounted.
SQL> alter database archivelog ;
Database altered.
SQL> alter database open;
Database altered.
如果在歸檔模式中,使用熱備份,需要聲明begin backup,為了能夠修複在熱備份過程中可能產生的分裂資料區塊。
SQL> alter tablespace data begin backup;
Tablespace altered.
這個時候停庫就會拋錯。
SQL> shutdown immediate;
ORA-01149: cannot shutdown - file 5 has online backup set
ORA-01110: data file 5: '/u02/oracle/oradata/data02.dbf'
如果發生斷電現象,則在重啟的時候出現ORA-01113: file 5 needs media recovery就需要明辨這個錯誤背後的意思
SQL> shutdown abort
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1261564 bytes
Variable Size 163577860 bytes
Database Buffers 142606336 bytes
Redo Buffers 7127040 bytes
Database mounted.
ORA-01113: file 5 needs media recovery
ORA-01110: data file 5: '/u02/oracle/oradata/data02.dbf'
其實碰到這個錯誤,還是需要結合多種情境來考慮,比如查看是否熱備份已經正常完成,之前的操作是什麼樣的?
SQL> desc v$backup
Name Null? Type
----------------------------------------- -------- ----------------------------
FILE# NUMBER
STATUS VARCHAR2(18)
CHANGE# NUMBER
TIME DATE
SQL> select *from v$backup;
FILE# STATUS CHANGE# TIME
---------- ------------------ ---------- ---------
1 NOT ACTIVE 0
2 NOT ACTIVE 0
3 NOT ACTIVE 0
4 NOT ACTIVE 0
5 ACTIVE 879352 20-JUL-15
可以從SCN的地方看出和重建控制檔案的情境還是存在一定的差別。
SQL> select file#,checkpoint_change#,last_change# from v$datafile;
FILE# CHECKPOINT_CHANGE# LAST_CHANGE#
---------- ------------------ ------------
1 879275
2 879275
3 879275
4 879275
5 879352
SQL> select file#,checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 879275
2 879275
3 879275
4 879275
5 879352
SQL> select checkpoint_change# from v$database;
CHECKPOINT_CHANGE#
------------------
879275
明白了問題,修複就很容易了,果斷結束熱備份。
SQL> alter tablespace data end backup;
Tablespace altered.
SQL> select file#,checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 879275
2 879275
3 879275
4 879275
5 879352
SQL> select checkpoint_change# from v$database;
CHECKPOINT_CHANGE#
------------------
879275
這個時候啟動資料庫就沒有問題了。
SQL> alter database open;
Database altered.
SQL> select checkpoint_change# from v$database;
CHECKPOINT_CHANGE#
------------------
899361
SQL> select file#,checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 899361
2 899361
3 899361
4 899361
5 899361
其實熱備份的這個錯誤也可以這麼來處理。
SQL> alter tablespace data begin backup;
Tablespace altered.
SQL> shutdown abort
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1261564 bytes
Variable Size 163577860 bytes
Database Buffers 142606336 bytes
Redo Buffers 7127040 bytes
Database mounted.
ORA-01113: file 5 needs media recovery
ORA-01110: data file 5: '/u02/oracle/oradata/data02.dbf'
SQL> recover datafile 5;
Media recovery complete.
SQL> alter database open;
Database altered.
SQL> select *from v$backup;
FILE# STATUS CHANGE# TIME
---------- ------------------ ---------- ---------
1 NOT ACTIVE 0
2 NOT ACTIVE 0
3 NOT ACTIVE 0
4 NOT ACTIVE 0
5 NOT ACTIVE 899488 20-JUL-15
總之解決問題就行,SCN的部分著實是需要關注的一個重點,這也是備份恢複的基石。