20160331数据文件offline与open resetlogs

[20160331]数据文件offline与open resetlogs.txt

--昨天不小心导致一个数据文件offline,而归档日志已经删除,无法在通过recover恢复到online,通过手工修改online数据文件。
--链接http://blog.itpub.net/267265/viewspace-2071731/

--这让我想起来节前跟别人聊天提到一个恢复操作,数据库无法open,安全起见,通过dg来恢复。设置只读模式,停止日志应用,拷贝文
--件到新机器,建立新的控制文件,打开使用open resetlogs,悲剧发生,有一个数据文件在做恢复时,已经处在offline状态。结果
--resetlog时,没有改变数据文件的RESETLOGS_CHANGE#.提示:

ORA-01190: control file or data file XX is from before the last RESETLOGS

--我当时问出现这种情况如何恢复,我记得对方讲通过提升scn来恢复,我当时觉得很奇怪,提到scn能解决这个问题吗?数据文件里面记
--录RESETLOGS_CHANGE#就能改变吗?当时由于时间关系,这个测试我一直做,今天比较有空做一些测试:

1.环境:

SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SYS@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header;
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS  CHECKPOINT_COUNT FUZ NAME                             TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ------- ---------------- --- -------------------------------- ----------------
    1        13227498083 2016-03-31 08:34:53                7       13227286650 ONLINE              1000 YES /mnt/ramdisk/book/system01.dbf   SYSTEM
    2        13227498083 2016-03-31 08:34:53             1834       13227286650 ONLINE               996 YES /mnt/ramdisk/book/sysaux01.dbf   SYSAUX
    3        13227498083 2016-03-31 08:34:53           923328       13227286650 ONLINE               916 YES /mnt/ramdisk/book/undotbs01.dbf  UNDOTBS1
    4        13227498083 2016-03-31 08:34:53            16143       13227286650 ONLINE              1000 YES /mnt/ramdisk/book/users01.dbf    USERS
    5        13227498083 2016-03-31 08:34:53           952916       13227286650 ONLINE               913 YES /mnt/ramdisk/book/example01.dbf  EXAMPLE
    6        13227498083 2016-03-31 08:34:53          1314508       13227286650 ONLINE               929 YES /mnt/ramdisk/book/sugar01.dbf    SUGAR
    7        13227498083 2016-03-31 08:34:53      13227207527       13227286650 ONLINE                22 YES /mnt/ramdisk/book/tea01.dbf      TEA
7 rows selected.

--每一次open resetlog都会改变RESETLOGS_CHANGE#,我这里当前是13227286650。也可以通过这个视图查询v$database_incarnation :
SYS@book> select * from v$database_incarnation ;
INCARNATION# RESETLOGS_CHANGE# RESETLOGS_TIME      PRIOR_RESETLOGS_CHANGE# PRIOR_RESETLOGS_TIM STATUS  RESETLOGS_ID PRIOR_INCARNATION# FLASHBACK_DATABASE_ALLOWED
------------ ----------------- ------------------- ----------------------- ------------------- ------- ------------ ------------------ --------------------------
           1                 1 2013-08-24 11:37:30                       0                     PARENT     824297850                  0 NO
           2            925702 2015-11-24 09:11:12                       1 2013-08-24 11:37:30 PARENT     896605872                  1 NO
           3       13227285139 2016-03-25 16:45:42                  925702 2015-11-24 09:11:12 PARENT     907433142                  2 NO
           4       13227286650 2016-03-25 17:06:01             13227285139 2016-03-25 16:45:42 CURRENT    907434361                  3 NO

--我的测试库经历了过几次1->925702->13227285139->13227286650.感觉这个就是当时切换的scn号。注意后面的RESETLOGS_ID跟
--log_archive_format参数的%r有关.
--还可以通过rman的list incarnation;查看

RMAN> list incarnation;
using target database control file instead of recovery catalog
List of Database Incarnations
DB Key  Inc Key DB Name  DB ID            STATUS  Reset SCN  Reset Time
------- ------- -------- ---------------- --- ---------- ----------
1       1       BOOK     1337401710       PARENT  1          2013-08-24 11:37:30
2       2       BOOK     1337401710       PARENT  925702     2015-11-24 09:11:12
3       3       BOOK     1337401710       PARENT  13227285139 2016-03-25 16:45:42
4       4       BOOK     1337401710       CURRENT 13227286650 2016-03-25 17:06:01

--另外查看参数log_archive_format,里面的%r表示的就是v$database_incarnation的RESETLOGS_ID,这个参数必须包括%r参数。如果你
--open resetlogs的日志的seq又从1开始。
SCOTT@book> show parameter log_archive_format
NAME                TYPE    VALUE
------------------- ------- ---------------
log_archive_format  string  %t_%s_%r.dbf

$ ll -l /u01/app/oracle/archivelog/book/
total 19952
-rw-r----- 1 oracle oinstall 20403712 2016-03-31 08:43:00 1_18_907434361.dbf

--这些信息应该保存在控制文件中,如果新建控制文件list incarnation;就看不到了。
--从这里看出如果open resetlogs 前,最好查询视图v$datafile_header确定是否存在表空间或者数据文件offline.

2.测试:
CREATE TABLESPACE TEA DATAFILE
  '/mnt/ramdisk/book/tea01.dbf' SIZE 1536K AUTOEXTEND OFF
LOGGING
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
BLOCKSIZE 8K
SEGMENT SPACE MANAGEMENT AUTO
FLASHBACK ON;

SCOTT@book> create table t tablespace tea as select rownum id ,lpad('text',4) name from dual connect by level<=4;
Table created.

SCOTT@book> select rowid,t.* from scott.t ;

ROWID                      ID NAME
------------------ ---------- --------------------
AAAWgeAAHAAAACLAAA          1 text
AAAWgeAAHAAAACLAAB          2 text
AAAWgeAAHAAAACLAAC          3 text
AAAWgeAAHAAAACLAAD          4 text

SCOTT@book> @ &r/rowid AAAWgeAAHAAAACLAAA
    OBJECT       FILE      BLOCK        ROW DBA                  TEXT
---------- ---------- ---------- ---------- -------------------- ----------------------------------------
     92190          7        139          0 7,139                alter system dump datafile 7 block 139 ;

--关机做一个冷备份,便于重复测试.
--session 1:
update t set name='aaaa' where id=1;
commit ;

--session 2:
update t set name='bbbb' where id=2;
--不提交。

--session 3:
SYS@book> alter database datafile 7 offline ;
Database altered.

--session 2:
commit ;

alter system archive log current ;
alter system archive log current ;
alter system archive log current ;
alter system archive log current ;

--切换出全部redo到归档。

SYS@book> shutdown abort ;
ORACLE instance shut down.

3.重新打开数据库看看:
SYS@book> startup mount
ORACLE instance started.
Total System Global Area  634732544 bytes
Fixed Size                  2255792 bytes
Variable Size             197133392 bytes
Database Buffers          427819008 bytes
Redo Buffers                7524352 bytes
Database mounted.


SYS@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header;
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS  CHECKPOINT_COUNT FUZ NAME                             TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ------- ---------------- --- -------------------------------- ----------------
    1        13227501337 2016-03-31 09:59:13                7       13227286650 ONLINE              1006 YES /mnt/ramdisk/book/system01.dbf   SYSTEM
    2        13227501337 2016-03-31 09:59:13             1834       13227286650 ONLINE              1002 YES /mnt/ramdisk/book/sysaux01.dbf   SYSAUX
    3        13227501337 2016-03-31 09:59:13           923328       13227286650 ONLINE               922 YES /mnt/ramdisk/book/undotbs01.dbf  UNDOTBS1
    4        13227501337 2016-03-31 09:59:13            16143       13227286650 ONLINE              1006 YES /mnt/ramdisk/book/users01.dbf    USERS
    5        13227501337 2016-03-31 09:59:13           952916       13227286650 ONLINE               919 YES /mnt/ramdisk/book/example01.dbf  EXAMPLE
    6        13227501337 2016-03-31 09:59:13          1314508       13227286650 ONLINE               935 YES /mnt/ramdisk/book/sugar01.dbf    SUGAR
    7        13227500869 2016-03-31 09:53:40      13227207527       13227286650 OFFLINE               26 YES /mnt/ramdisk/book/tea01.dbf      TEA
7 rows selected.

--如果我使用 recover database using backup controlfile until cancel;恢复后,不能在使用open打开,必须加入open resetlogs参数。

SYS@book> recover database using backup controlfile until cancel;
ORA-00279: change 13227501224 generated at 03/31/2016 09:10:29 needed for thread 1
ORA-00289: suggestion : /u01/app/oracle/archivelog/book/1_24_907434361.dbf
ORA-00280: change 13227501224 for thread 1 is in sequence #24
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
ORA-00308: cannot open archived log '/u01/app/oracle/archivelog/book/1_24_907434361.dbf'
ORA-27037: unable to obtain file status
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3

ORA-00308: cannot open archived log '/u01/app/oracle/archivelog/book/1_24_907434361.dbf'
ORA-27037: unable to obtain file status
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3

ORA-01547: warning: RECOVER succeeded but OPEN RESETLOGS would get error below
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: '/mnt/ramdisk/book/system01.dbf'

SYS@book> @ &r/logfile
GROUP# THREAD# SEQUENCE#       BYTES BLOCKSIZE MEMBERS ARC STATUS     FIRST_CHANGE# FIRST_TIME          NEXT_CHANGE# NEXT_TIME           GROUP# STATUS     TYPE       MEMBER                                             IS_
------ ------- --------- ----------- --------- ------- --- ---------- ------------- ------------------- ------------ ------------------- ------ ---------- ---------- -------------------------------------------------- ---
     1       1        22    52428800       512       1 YES INACTIVE     13227501329 2016-03-31 09:59:09  13227501333 2016-03-31 09:59:12      1            ONLINE     /mnt/ramdisk/book/redo01.log                       NO
     2       1        23    52428800       512       1 YES INACTIVE     13227501333 2016-03-31 09:59:12  13227501337 2016-03-31 09:59:13      2            ONLINE     /mnt/ramdisk/book/redo02.log                       NO
     3       1        24    52428800       512       1 NO  CURRENT      13227501337 2016-03-31 09:59:13 2.814750E+14                          3            ONLINE     /mnt/ramdisk/book/redo03.log                       NO

--还需要 /mnt/ramdisk/book/redo03.log.

SYS@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header where file# in (1,7);
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS     CHECKPOINT_COUNT FUZ NAME                                               TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ---------- ---------------- --- -------------------------------------------------- ------------------------------
    1        13227501337 2016-03-31 09:59:13                7       13227286650 ONLINE                 1006 YES /mnt/ramdisk/book/system01.dbf                     SYSTEM
    7        13227500869 2016-03-31 09:53:40      13227207527       13227286650 ONLINE                   26 YES /mnt/ramdisk/book/tea01.dbf                        TEA

--另外可以发现数据文件7在recover时已经设置为online.

SYS@book> recover database using backup controlfile until cancel;
ORA-00279: change 13227500869 generated at 03/31/2016 09:53:40 needed for thread 1
ORA-00289: suggestion : /u01/app/oracle/archivelog/book/1_20_907434361.dbf
ORA-00280: change 13227500869 for thread 1 is in sequence #20

Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
ORA-00279: change 13227501325 generated at 03/31/2016 09:59:08 needed for thread 1
ORA-00289: suggestion : /u01/app/oracle/archivelog/book/1_21_907434361.dbf
ORA-00280: change 13227501325 for thread 1 is in sequence #21
ORA-00278: log file '/u01/app/oracle/archivelog/book/1_20_907434361.dbf' no longer needed for this recovery


ORA-00279: change 13227501329 generated at 03/31/2016 09:59:09 needed for thread 1
ORA-00289: suggestion : /u01/app/oracle/archivelog/book/1_22_907434361.dbf
ORA-00280: change 13227501329 for thread 1 is in sequence #22
ORA-00278: log file '/u01/app/oracle/archivelog/book/1_21_907434361.dbf' no longer needed for this recovery


ORA-00279: change 13227501333 generated at 03/31/2016 09:59:12 needed for thread 1
ORA-00289: suggestion : /u01/app/oracle/archivelog/book/1_23_907434361.dbf
ORA-00280: change 13227501333 for thread 1 is in sequence #23
ORA-00278: log file '/u01/app/oracle/archivelog/book/1_22_907434361.dbf' no longer needed for this recovery


ORA-00279: change 13227501337 generated at 03/31/2016 09:59:13 needed for thread 1
ORA-00289: suggestion : /u01/app/oracle/archivelog/book/1_24_907434361.dbf
ORA-00280: change 13227501337 for thread 1 is in sequence #24
ORA-00278: log file '/u01/app/oracle/archivelog/book/1_23_907434361.dbf' no longer needed for this recovery


ORA-00308: cannot open archived log '/u01/app/oracle/archivelog/book/1_24_907434361.dbf'
ORA-27037: unable to obtain file status
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3


ORA-01547: warning: RECOVER succeeded but OPEN RESETLOGS would get error below
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: '/mnt/ramdisk/book/system01.dbf'

--我觉得这个地方是执行恢复数据文件7.因为前面提示是差seq=24.继续恢复:

SYS@book> recover database using backup controlfile until cancel;
ORA-00279: change 13227501337 generated at 03/31/2016 09:59:13 needed for thread 1
ORA-00289: suggestion : /u01/app/oracle/archivelog/book/1_24_907434361.dbf
ORA-00280: change 13227501337 for thread 1 is in sequence #24
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
/mnt/ramdisk/book/redo03.log
Log applied.
Media recovery complete.

SYS@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header where file# in (1,7);
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS     CHECKPOINT_COUNT FUZ NAME                                               TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ---------- ---------------- --- -------------------------------------------------- ------------------------------
    1        13227501343 2016-03-31 09:59:24                7       13227286650 ONLINE                 1007 NO  /mnt/ramdisk/book/system01.dbf                     SYSTEM
    7        13227501343 2016-03-31 09:59:24      13227207527       13227286650 ONLINE                   27 NO  /mnt/ramdisk/book/tea01.dbf                        TEA

SYS@book> alter database datafile 7 offline ;
Database altered.

SYS@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header where file# in (1,7);
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS     CHECKPOINT_COUNT FUZ NAME                                               TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ---------- ---------------- --- -------------------------------------------------- ------------------------------
    1        13227501343 2016-03-31 09:59:24                7       13227286650 ONLINE                 1007 NO  /mnt/ramdisk/book/system01.dbf                     SYSTEM
    7        13227501343 2016-03-31 09:59:24      13227207527       13227286650 OFFLINE                  27 NO  /mnt/ramdisk/book/tea01.dbf                        TEA

-- 数据文件7已经offline.

SYS@book> alter database read ;
alter database read
                   *
ERROR at line 1:
ORA-02231: missing or invalid option to ALTER DATABASE

SYS@book> alter database open resetlogs;
Database altered.

SYS@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header where file# in (1,7);
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS     CHECKPOINT_COUNT FUZ NAME                                               TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ---------- ---------------- --- -------------------------------------------------- ------------------------------
    1        13227501347 2016-03-31 10:08:00                7       13227501344 ONLINE                 1009 YES /mnt/ramdisk/book/system01.dbf                     SYSTEM
    7        13227501343 2016-03-31 09:59:24      13227207527       13227286650 OFFLINE                  27 NO  /mnt/ramdisk/book/tea01.dbf                        TEA

--数据文件7的RESETLOGS_CHANGE#与数据文件1的RESETLOGS_CHANGE#不一致.

SYS@book> alter database datafile 7 online ;
alter database datafile 7 online
*
ERROR at line 1:
ORA-01190: control file or data file 7 is from before the last RESETLOGS
ORA-01110: data file 7: '/mnt/ramdisk/book/tea01.dbf'

--不要被ORA-01190提示蒙骗,实际上10g以后可以跨resetlogs恢复,只要归档日志全部都在.

RMAN> list incarnation;
using target database control file instead of recovery catalog

List of Database Incarnations
DB Key  Inc Key DB Name  DB ID            STATUS  Reset SCN  Reset Time
------- ------- -------- ---------------- --- ---------- ----------
1       1       BOOK     1337401710       PARENT  1          2013-08-24 11:37:30
2       2       BOOK     1337401710       PARENT  925702     2015-11-24 09:11:12
3       3       BOOK     1337401710       PARENT  13227285139 2016-03-25 16:45:42
4       4       BOOK     1337401710       PARENT  13227286650 2016-03-25 17:06:01
5       5       BOOK     1337401710       CURRENT 13227501344 2016-03-31 10:07:59

SYS@book> recover datafile 7;
Media recovery complete.

SYS@book> alter database datafile 7 online ;
Database altered.

SYS@book> select rowid,t.* from scott.t ;
ROWID                        ID NAME
------------------ ------------ ---------
AAAWgeAAHAAAACLAAA            1 aaaa
AAAWgeAAHAAAACLAAB            2 bbbb
AAAWgeAAHAAAACLAAC            3 text
AAAWgeAAHAAAACLAAD            4 text

--没有任何丢失.

SYS@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header;
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS     CHECKPOINT_COUNT FUZ NAME                                               TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ---------- ---------------- --- -------------------------------------------------- ------------------------------
    1        13227501347 2016-03-31 10:08:00                7       13227501344 ONLINE                 1009 YES /mnt/ramdisk/book/system01.dbf                     SYSTEM
    2        13227501347 2016-03-31 10:08:00             1834       13227501344 ONLINE                 1005 YES /mnt/ramdisk/book/sysaux01.dbf                     SYSAUX
    3        13227501347 2016-03-31 10:08:00           923328       13227501344 ONLINE                  925 YES /mnt/ramdisk/book/undotbs01.dbf                    UNDOTBS1
    4        13227501347 2016-03-31 10:08:00            16143       13227501344 ONLINE                 1009 YES /mnt/ramdisk/book/users01.dbf                      USERS
    5        13227501347 2016-03-31 10:08:00           952916       13227501344 ONLINE                  922 YES /mnt/ramdisk/book/example01.dbf                    EXAMPLE
    6        13227501347 2016-03-31 10:08:00          1314508       13227501344 ONLINE                  938 YES /mnt/ramdisk/book/sugar01.dbf                      SUGAR
    7        13227501818 2016-03-31 10:12:53      13227207527       13227501344 ONLINE                   29 YES /mnt/ramdisk/book/tea01.dbf                        TEA
7 rows selected.

--注意一个细节,我的recover database using backup controlfile until cancel;数据文件7已经恢复到CHECKPOINT_CHANGE#=13227501343,而open resetlogs的scn=13227501344,仅仅相差1.
--如果存在一定差距,能恢复吗? 看一篇帖子.

上一篇:html5 getComputedStyle + resize 实现动态宽高度等比响应式页面设计


下一篇:Swift里的类继承,构造和析构代码