创建一个新的数据文件:
sys@LYON> create tablespace ttbs datafile '/data/lyon/ttbs01.dbf'
2 size 10m uniform. size 1m
3 segment space management auto
4 extent management local;
Tablespace created.
Elapsed: 00:00:00.77
建一个测试表:
sys@LYON> create table ttab(x int) tablespace ttbs;
Table created.
Elapsed: 00:00:00.26
加入一些测试数据
sys@LYON> insert into ttab(x)
2 select rownum from dual
3 connect by rownum <= 10;
10 rows created.
Elapsed: 00:00:00.01
sys@LYON> commit;
Commit complete.
Elapsed: 00:00:00.01
sys@LYON> select * from ttab;
X
----------
1
2
3
4
5
6
7
8
9
10
10 rows selected.
Elapsed: 00:00:00.04
删除数据文件:
sys@LYON> !rm -f ttbs01.dbf
再次查询ttab,发现居然还能查到数据:
sys@LYON> select * from ttab;
X
----------
1
2
3
4
5
6
7
8
9
10
10 rows selected.
Elapsed: 00:00:00.03
清空了下缓存,发现还是可以执行查询:
sys@LYON> alter system flush buffer_cache;
System altered.
Elapsed: 00:00:00.24
sys@LYON> select * from ttab;
X
----------
1
2
3
4
5
6
7
8
9
10
10 rows selected.
Elapsed: 00:00:00.01
难道是undo的缘故?尝试更换一下表空间,看是否会查不到数据:
sys@LYON> create undo tablespace undotbs02 datafile '/data/lyon/undotbs02.dbf' reuse;
Tablespace created.
Elapsed: 00:00:00.55
sys@LYON> alter system set undo_tablespace=undotbs02 scope=memory;
System altered.
Elapsed: 00:00:00.08
发现还是能查询到:
sys@LYON> select * from ttab;
X
----------
1
2
3
4
5
6
7
8
9
10
10 rows selected.
Elapsed: 00:00:00.01
尝试了清空闪回区也不行。
最后不知道过了多少时间可以看到执行出错了:
sys@LYON> select * from ttab;
select * from ttab
*
ERROR at line 1:
ORA-00376: file 5 cannot be read at this time
ORA-01110: data file 5: '/data/lyon/ttbs01.dbf'
Elapsed: 00:00:00.02
看来还是重做日志的缘故
开始恢复数据文件:
首先将该文件offline:
sys@LYON> alter database datafile '/data/lyon/ttbs01.dbf' offline;
Database altered.
Elapsed: 00:00:00.05
然后切换到rman 执行修复(restore)
sys@LYON> !rman target /
Recovery Manager: Release 10.2.0.1.0 - Production on Tue May 25 07:33:47 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
connected to target database: LYON (DBID=1398719310)
RMAN> restore datafile '/data/lyon/ttbs01.dbf';
Starting restore at 25-MAY-10
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=153 devtype=DISK
creating datafile fno=5 name=/data/lyon/ttbs01.dbf
restore not done; all files readonly, offline, or already restored
Finished restore at 25-MAY-10
再执行恢复(这里将文件名的指定换成了文件编号5,其实都是一样的):
RMAN> recover datafile 5;
Starting recover at 25-MAY-10
using channel ORA_DISK_1
starting media recovery
media recovery complete, elapsed time: 00:00:00
Finished recover at 25-MAY-10
RMAN> exit
Recovery Manager complete.
退出恢复管理器,执行查询:
sys@LYON> select * from ttab;
select * from ttab
*
ERROR at line 1:
ORA-00376: file 5 cannot be read at this time
ORA-01110: data file 5: '/data/lyon/ttbs01.dbf'
Elapsed: 00:00:00.01
sys@LYON> alter database datafile 5 online;
Database altered.
Elapsed: 00:00:00.04
sys@LYON> select * from ttab;
X
----------
1
2
3
4
5
6
7
8
9
10
10 rows selected.
Elapsed: 00:00:00.01
至此,归档模式下的数据文件丢失已经完成了。
上面是调用了rman进行恢复的情况,还有一种是不用rman,直接用sqlplus进行恢复:
删除数据文件:
[oracle@localhost lyon]$ rm -f ttbs01.dbf
在sys用户下创建新的数据文件,作为丢失的数据文件:
直接创建会报错(文件在使用中或需要恢复):
sys@LYON> alter database create datafile '/data/lyon/ttbs01.dbf' as '/data/lyon/ttbs01.dbf';
alter database create datafile '/data/lyon/ttbs01.dbf' as '/data/lyon/ttbs01.dbf'
*
ERROR at line 1:
ORA-01182: cannot create database file 5 - file is in use or recovery
ORA-01110: data file 5: '/data/lyon/ttbs01.dbf'
Elapsed: 00:00:00.01
所以首先将该数据文件offline:
sys@LYON> alter database datafile 5 offline;
Database altered.
Elapsed: 00:00:00.08
然后再创建文件:
sys@LYON> alter database create datafile '/data/lyon/ttbs01.dbf' as '/data/lyon/ttbs01.dbf';
Database altered.
Elapsed: 00:00:00.51
假设数据文件是一个装着数据的盒子,那么这个步骤实际上相当于我先按照丢失的那个数据文件的样子做个空盒子
然后在恢复的时候从重做日志中将数据取出来再放到这个空盒子中
如果没有恢复,是不能直接online的:
sys@LYON> alter database datafile 5 online;
alter database datafile 5 online
*
ERROR at line 1:
ORA-01113: file 5 needs media recovery
ORA-01110: data file 5: '/data/lyon/ttbs01.dbf'
Elapsed: 00:00:00.02
开始恢复(将数据取出来放到盒子中):
sys@LYON> recover datafile 5;
Media recovery complete.
sys@LYON> alter database datafile 5 online;
Database altered.
Elapsed: 00:00:00.03
于是,不用rman进行的恢复也完成了。
小结:
这次恢复的前提是要求在归档模式下,且对应的从开始到数据文件丢失前的重做日志都还存在
才能确保在此种模式下正确的恢复数据。
执行恢复的几个主要步骤:
1、将丢失的数据文件offline;
2、使用rman修复数据文件(restore datafile 5)或者创建新的数据文件(alter database create datafile xxx as xxx;)
3、恢复数据文件(recover datafile 5)。
4、将恢复后的数据文件online;
这样恢复就完成了。
另外,之前提到的,数据文件丢失后,短时间内还能对在数据文件中的表进行查询的问题。
不知道是不是我忽略了一个Oracle的某种机制。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12932950/viewspace-663761/,如需转载,请注明出处,否则将追究法律责任。