实验前提:已经做好备份。
SQL>
col file_name for a50
select file_id,file_name from dba_data_files;
FILE_ID FILE_NAME
---------- --------------------------------------------------
4 /home/oracle/app/oradata/orcl/users01.dbf
2 /home/oracle/app/oradata/orcl/sysaux01.dbf
1 /home/oracle/app/oradata/orcl/system01.dbf
5 /home/oracle/app/oradata/orcl/tbtb01.dbf
6 /home/oracle/app/oradata/orcl/ogg01.dbf
7 /home/oracle/app/oradata/andy01.dbf
8 /home/oracle/app/oradata/orcl/bbb.dbf
9 /home/oracle/app/oradata/orcl/andy02.dbf
10 /home/oracle/app/oradata/orcl/andy03.dbf
11 /home/oracle/app/oradata/orcl/tts01.dbf
12 /home/oracle/app/oradata/orcl/ttsind01.dbf
FILE_ID FILE_NAME
---------- --------------------------------------------------
13 /home/oracle/app/oradata/orcl/fda01.dbf
14 /home/oracle/app/oradata/orcl/fda02.dbf
15 /home/oracle/app/oradata/orcl/undotbs02.dbf
14 rows selected.
--模拟数据文件损坏
[[email protected] ~]$ mv /home/oracle/app/oradata/orcl/andy02.dbf /home/oracle/app/oradata/orcl/andy02.dbf.bak
SQL> alter system checkpoint;
alter system checkpoint
*
ERROR at line 1:
ORA-03113: end-of-file on communication channel
Process ID: 2432
Session ID: 1 Serial number: 5
-- 查看报错信息
[[email protected] trace]$ cat alert_orcl.log |grep ORA-
ORA-01116: error in opening database file 9
ORA-01110: data file 9: ‘/home/oracle/app/oradata/orcl/andy02.dbf‘
ORA-27041: unable to open file
ORA-63999: data file suffered media failure
SQL> startup mount;
SQL> set lin 400
SQL> col error for a40
SQL> select * from v$recover_file;
FILE# ONLINE ONLINE_ ERROR CHANGE# TIME
---------- ------- ------- ---------------------------------------- ---------- ---------
9 ONLINE ONLINE FILE NOT FOUND 0
--利用备份恢复
(如果数据文件损坏,发现及时,数据库没有宕机,则立刻offline对应文件,避免宕机。如果已经宕机,则可跳过offline)
RMAN> sql ‘alter database datafile 9 offline‘;
RMAN> restore datafile 9;
RMAN> recover datafile 9;
RMAN> sql ‘alter database datafile 9 online‘;
RMAN> alter database open;
database opened
补充:
在11.2.0.2之前,如果数据库运行在归档模式下,并且写错误发生在非SYSTEM表空间文件,则数据库会将发生错误的文件离线,在从11.2.0.2开始,数据库会Crash实例以替代Offline。注意:在非归档模式下或者SYSTEM遭受错误时,数据库会直接崩溃。
如果我们不想尝试这个新特性,可以通过将 _DATAFILE_WRITE_ERRORS_CRASH_INSTANCE 设置为FALSE来屏蔽该行为。
该参数是一个动态参数: alter system set "_datafile_write_errors_crash_instance"=false;