用户创建表空间时误将数据文件放到了本地盘,重启数据库时一个实例启动不了,只能offline该表空间后启动数据库。现用户想知道怎样能把这个表空间数据文件中的数据恢复出来。
测试目的:验证RAC中误将数据文件创建在本地盘时的修复办法 环境说明: 两节点RAC,数据库名为db10g 版本10.2.0.5 使用了ASM作为共享存储解决方案。
1,场景准备
1)节点2:创建表空间test1,数据文件不放到ASM,而是放到本地盘:
SQL> create tablespace test1 datafile '/home/Oracle/test1.dbf' size 10m;
Tablespace created.
SQL> select name,status from v$datafile;
NAME STATUS
----------------------------------------------------------- -----------------------
DG/db10g/datafile/system.256.821723567 SYSTEM DG/db10g/datafile/undotbs1.258.821723569 ONLINE DG/db10g/datafile/sysaux.257.821723569 ONLINE DG/db10g/datafile/users.259.821723569 ONLINE DG/db10g/datafile/undotbs2.264.821723755 ONLINE /home/oracle/test1.dbf ONLINE
6 rows selected.
2)节点2:在表空间test1中创建表没问题
SQL> create table test1 (id int) tablespace test1;
Table created.
SQL> create table test2 tablespace test1 as select * from dba_tables;
Table created.
3)节点1:能查到表空间test1,但创建表报错
SQL> select name ,status from v$datafile;
NAME STATUS
--------------------------------------------------------- ------------------ DG/db10g/datafile/system.256.821723567 SYSTEM DG/db10g/datafile/undotbs1.258.821723569 ONLINE DG/db10g/datafile/sysaux.257.821723569 ONLINE DG/db10g/datafile/users.259.821723569 ONLINE DG/db10g/datafile/undotbs2.264.821723755 ONLINE /home/oracle/test1.dbf ONLINE
6 rows selected.
SQL> create table test1 (id int) tablespace test1;
create table test1 (id int) tablespace test1
* ERROR at line 1:
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: '/home/oracle/test1.dbf'
4)重启数据库,会发现节点1实例起不来,因为节点1无法读取节点2本地盘上的/home/oracle/test1.dbf
[oracle@rac10g2 ~]$ srvctl stop database -d db10g
[oracle@rac10g2 ~]$ srvctl start database -d db10g
PRKP-1001 : Error starting instance db10g1 on node rac10g1
CRS-0215: Could not start resource 'ora.db10g.db10g1.inst'.
[oracle@rac10g2 ~]$ crs_stat -t
Name Type Target State Host
------------------------------------------------------------
ora.db10g.db application ONLINE ONLINE rac10g1
ora....g1.inst application ONLINE OFFLINE
ora....g2.inst application ONLINE ONLINE rac10g2
ora....SM1.asm application ONLINE ONLINE rac10g1
ora....G1.lsnr application ONLINE ONLINE rac10g1
ora....0g1.gsd application ONLINE ONLINE rac10g1
ora....0g1.ons application ONLINE ONLINE rac10g1
ora....0g1.vip application ONLINE ONLINE rac10g1
ora....SM2.asm application ONLINE ONLINE rac10g2
ora....G2.lsnr application ONLINE ONLINE rac10g2
ora....0g2.gsd application ONLINE ONLINE rac10g2
ora....0g2.ons application ONLINE ONLINE rac10g2
ora....0g2.vip application ONLINE ONLINE rac10g2
2,处理过程
由于该过程中需要从本地盘把数据文件迁移到ASM共享存储,ASM文件的访问无法通过操作系统级别直接进行。
在10gR2中,我们可以使用RMAN命令备份和恢复ASM文件,使用ASMCMD命令可以浏览和操纵目录结构。不过, Oracle 10g包中的DBMS_FILE_TRANSFER是处理ASM的另一种方式。 DBMS_FILE_TRANSFER可以在同一台Oracle 服务器上或两台Oracle 服务器之间复制文件。它使用目录对象来指定源目录和目的目录,因为目录对象支持ASM路径 名称,所以DBMS_FILE_TRANSFER也支持ASM路径名。这使得从常规文件系统的ASM存储区移入和移出文件变得十分 简单,使用它可以完成如下的迁移:
ASM->ASM、ASM->OS Flie、OS File->ASM、OS File->OS File。
建错的表空间test1数据文件在节点2,所以只能从节点2上打开。可在节点2上将表空间offline之后使用dbms_file_transfer将数据 文件移到ASM共享存储(如使用的是集群文件系统,直接拷贝数据文件即可)。
1)为两个数据文件路径创建目录
节点2:创建两个directory,一个指向本地盘该数据文件目录;一个指向ASM数据文件目录。
SQL> create directory test1 as '/home/oracle';
Directory created.
SQL> create directory test2 as ' DG/db10g/datafile';
Directory created.
2)offline表空间
节点2:offline表空间test1
SQL> alter tablespace test1 offline;
Tablespace altered.
3)拷贝数据文件到ASM
节点2:使用dbms_file_transfer拷贝该数据文件到ASM
SQL> exec dbms_file_transfer.copy_file('TEST1','test1.dbf','TEST2','test1.dbf');
PL/SQL procedure successfully completed.
4)修改控制文件中的数据文件路径
节点2:
SQL> alter database rename file '/home/oracle/test1.dbf' to ' DG/db10g/datafile/test1.dbf'
SQL> /
Database altered.
5)online表空间test1
节点2:online表空间test1
SQL> alter tablespace test1 online;
Tablespace altered.
6)启动实例1
[oracle@rac10g2 ~]$ srvctl start instance -d db10g -i db10g1
[oracle@rac10g2 ~]$ crs_stat -t
Name Type Target State Host
------------------------------------------------------------
ora.db10g.db application ONLINE ONLINE rac10g1
ora....g1.inst application ONLINE ONLINE rac10g1
ora....g2.inst application ONLINE ONLINE rac10g2
ora....SM1.asm application ONLINE ONLINE rac10g1
ora....G1.lsnr application ONLINE ONLINE rac10g1
ora....0g1.gsd application ONLINE ONLINE rac10g1
ora....0g1.ons application ONLINE ONLINE rac10g1
ora....0g1.vip application ONLINE ONLINE rac10g1
ora....SM2.asm application ONLINE ONLINE rac10g2
ora....G2.lsnr application ONLINE ONLINE rac10g2
ora....0g2.gsd application ONLINE ONLINE rac10g2
ora....0g2.ons application ONLINE ONLINE rac10g2
ora....0g2.vip application ONLINE ONLINE rac10g2
7)节点1:检查数据文件状态和表空间内数据
SQL> select name ,status from v$datafile;
NAME STATUS
---------------------------------------------------- ------------------
DG/db10g/datafile/system.256.821723567 SYSTEM DG/db10g/datafile/undotbs1.258.821723569 ONLINE DG/db10g/datafile/sysaux.257.821723569 ONLINE DG/db10g/datafile/users.259.821723569 ONLINE DG/db10g/datafile/undotbs2.264.821723755 ONLINE DG/db10g/datafile/test1.dbf ONLINE
6 rows selected.
SQL> select count(*) from test2;
COUNT(*)
----------
1522
3、备注
以上迁移数据文件时是采用 dbms_file_transfer.copy_file迁移数据文件的方法,也可以使用RMAN来做:
SQL>select tablespace_name,file_name,status,online_status from dba_data_files;
需要对表空间进行OFFLINE 登录RMAN,
RMAN> sql "alter tablespace test1 offline";
RMAN> copy datafile '/home/oracle/test1.dbf' to ' DG/rac10g/datafile/test1.dbf';
SQL> alter database rename file '/home/oracle/test1.dbf' to ' DG/rac10g/datafile/test1.dbf';
SQL> alter tablespace test1 online;