首页 > 代码库 > 重建控制文件详解

重建控制文件详解

http://note.youdao.com/share/?id=d3991566cad33723db8b92183dcd9161&type=note
完整图文最新版
 
创造“重建控制文件脚本”需要在mount或open下进行。
alter database backup controlfile to trace as ‘/tmp/ctl..sql‘;

执行“重建控制文件脚本”需要在nomount的时候。
 

-- The following are current System-scope REDO Log Archival related
-- parameters and can be included in the database initialization file.
--
-- LOG_ARCHIVE_DEST=‘‘
-- LOG_ARCHIVE_DUPLEX_DEST=‘‘
--
-- LOG_ARCHIVE_FORMAT=%t_%s_%r.dbf
--
-- DB_UNIQUE_NAME="gs01ehis"
--
-- LOG_ARCHIVE_CONFIG=‘SEND, RECEIVE, NODG_CONFIG‘
-- LOG_ARCHIVE_MAX_PROCESSES=2
-- STANDBY_FILE_MANAGEMENT=MANUAL
-- STANDBY_ARCHIVE_DEST=?/dbs/arch
-- FAL_CLIENT=‘‘
-- FAL_SERVER=‘‘
--
-- LOG_ARCHIVE_DEST_1=‘LOCATION=/paic/stg/oracle/10g/app102056/oracle/product/10.2.0/dbs/arch‘
-- LOG_ARCHIVE_DEST_1=‘MANDATORY NOREOPEN NODELAY‘
-- LOG_ARCHIVE_DEST_1=‘ARCH NOAFFIRM EXPEDITE NOVERIFY SYNC‘
-- LOG_ARCHIVE_DEST_1=‘NOREGISTER NOALTERNATE NODEPENDENCY‘
-- LOG_ARCHIVE_DEST_1=‘NOMAX_FAILURE NOQUOTA_SIZE NOQUOTA_USED NODB_UNIQUE_NAME‘
-- LOG_ARCHIVE_DEST_1=‘VALID_FOR=(PRIMARY_ROLE,ONLINE_LOGFILES)‘
-- LOG_ARCHIVE_DEST_STATE_1=ENABLE
--
-- Below are two sets of SQL statements, each of which creates a new
-- control file and uses it to open the database. The first set opens
-- the database with the NORESETLOGS option and should be used only if
-- the current versions of all online logs are available. The second
-- set opens the database with the RESETLOGS option and should be used
-- if online logs are unavailable.
-- The appropriate set of statements can be copied from the trace into
-- a script file, edited as necessary, and executed when there is a
-- need to re-create the control file.
--
--     Set #1. NORESETLOGS case
--
-- The following commands will create a new control file and use it
-- to open the database.
-- Data used by Recovery Manager will be lost.
-- Additional logs may be required for media recovery of offline
-- Use this only if the current versions of all online logs are
-- available.

-- After mounting the created controlfile, the following SQL
-- statement will place the database in the appropriate
-- protection mode:
--  ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE PERFORMANCE

STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "GS01EHIS" NORESETLOGS FORCE LOGGING NOARCHIVELOG
    MAXLOGFILES 32
    MAXLOGMEMBERS 3
    MAXDATAFILES 800
    MAXINSTANCES 8
    MAXLOGHISTORY 18696
LOGFILE
  GROUP 1 ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/redo01.log‘  SIZE 100M,
  GROUP 2 ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/redo02.log‘  SIZE 100M,
  GROUP 3 ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/redo03.log‘  SIZE 100M
-- STANDBY LOGFILE

DATAFILE
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/system01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/undotbs01.dbf‘,
   ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/system01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/undotbs01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/sysaux01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/users01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/auddata01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/dbadata01.dbf‘
CHARACTER SET ZHS16GBK
;

- Commands to re-create incarnation table
-- Below log names MUST be changed to existing filenames on
-- disk. Any one log file from each branch can be used to
-- re-create incarnation records.
-- ALTER DATABASE REGISTER LOGFILE ‘/paic/stg/oracle/10g/app102056/oracle/product/10.2.0/dbs/arch1_1_807985002.dbf‘;
-- ALTER DATABASE REGISTER LOGFILE ‘/paic/stg/oracle/10g/app102056/oracle/product/10.2.0/dbs/arch1_1_807987054.dbf‘;
-- Recovery is required if any of the datafiles are restored backups,
-- or if the last shutdown was not normal or immediate.
RECOVER DATABASE

-- Set Database Guard and/or Supplemental Logging
ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
-- Database can now be opened normally.
ALTER DATABASE OPEN;

-- Files in read-only tablespaces are now named.
ALTER DATABASE RENAME FILE ‘MISSING00030‘
  TO ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/fwy01.dbf‘;

-- Online the files in read-only tablespaces.
ALTER TABLESPACE "FWY01" ONLINE;
-- Commands to add tempfiles to temporary tablespaces.
-- Online tempfiles have complete space information.
-- Other tempfiles may require adjustment.
ALTER TABLESPACE TEMP ADD TEMPFILE ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/temp01.dbf‘ REUSE;
-- End of tempfile additions.
--
--     Set #2. RESETLOGS case
--
-- The following commands will create a new control file and use it
-- to open the database.
-- Data used by Recovery Manager will be lost.
-- The contents of online logs will be lost and all backups will
-- be invalidated. Use this only if online logs are damaged.

-- After mounting the created controlfile, the following SQL
-- statement will place the database in the appropriate
-- protection mode:
--  ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE PERFORMANCE

STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "GS01EHIS" RESETLOGS FORCE LOGGING NOARCHIVELOG
    MAXLOGFILES 32
    MAXLOGMEMBERS 3
    MAXDATAFILES 800
    MAXINSTANCES 8
    MAXLOGHISTORY 18696
LOGFILE
  GROUP 1 ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/redo01.log‘  SIZE 100M,
  GROUP 2 ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/redo02.log‘  SIZE 100M,
  GROUP 3 ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/redo03.log‘  SIZE 100M
-- STANDBY LOGFILE

DATAFILE
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/system01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/undotbs01.dbf‘,
     ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/system01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/undotbs01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/sysaux01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/users01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/auddata01.dbf‘,
  ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/dbadata01.dbf‘
CHARACTER SET ZHS16GBK
;
-- Commands to re-create incarnation table
-- Below log names MUST be changed to existing filenames on
-- disk. Any one log file from each branch can be used to
-- re-create incarnation records.
-- re-create incarnation records.
-- ALTER DATABASE REGISTER LOGFILE ‘/paic/stg/oracle/10g/app102056/oracle/product/10.2.0/dbs/arch1_1_807985002.dbf‘;
-- ALTER DATABASE REGISTER LOGFILE ‘/paic/stg/oracle/10g/app102056/oracle/product/10.2.0/dbs/arch1_1_807987054.dbf‘;
-- Recovery is required if any of the datafiles are restored backups,
-- or if the last shutdown was not normal or immediate.
RECOVER DATABASE USING BACKUP CONTROLFILE

-- Set Database Guard and/or Supplemental Logging
ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
-- Database can now be opened zeroing the online logs.
ALTER DATABASE OPEN RESETLOGS;

-- Files in read-only tablespaces are now named.
ALTER DATABASE RENAME FILE ‘MISSING00030‘
  TO ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/fwy01.dbf‘;

-- Online the files in read-only tablespaces.
ALTER TABLESPACE "FWY01" ONLINE;
-- Commands to add tempfiles to temporary tablespaces.
-- Online tempfiles have complete space information.
-- Other tempfiles may require adjustment.
ALTER TABLESPACE TEMP ADD TEMPFILE ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/temp01.dbf‘ REUSE;
-- End of tempfile additions.

 
主要分为两部分,resetlogs也有noresetlogs的重建控制文件脚本。如果想单独要某部分,分别执行以下语句即可。
alter database backup controlfile to trace as ‘/tmp/ctl_1.sql‘ noresetlogs;
alter database backup controlfile to trace as ‘/tmp/ctl_1.sql‘ resetlogs;

下面分别来讲解,红框的几种选项。
重建控制文件的时候会去数据文件头读db_name,reuse是会参考数据文件头的db_name,而set会将数据文件头的db_name都改成脚本中指定的database "ZXRRIN"

SET还是REUSE
假如我选reuse database "ZXRRIN2",而数据文件头是ZXRRIN2,那么会报如下错误。
ORA-01503: CREATE CONTROLFILE failed
ORA-01161: database name ZXRRIN in file header does not match given name of
ZXRRIN2
ORA-01110: data file 1: ‘/oradata/zxrrin/zxrrin/system01.dbf‘
所以,只有数据文件头的database name与参数文件的db_name一致,才能在重建控制文件时用reuse。当然,用set也没关系。只是将数据文件头的db_name重写成相同的。就如update t set i=3 where i=3一样。如果想改变数据文件头的db_name使他们与起库参数的db_name一致,就当然得用set咯。

database "ZXRRIN"
database "ZXRRIN"指定的db_name,要跟起库参数文件的db_name一致,重建控制文件才能成功。
这是必须的,而跟REUSE还是SET等没关系。所以不能用A1起库,然后重建控制文件成A2的db_name哦。

noresetlogs还是resetlogs
重建conrolfile时,假如用的是noresetlogs的选项。那么controlfile里面的系统scn号,以及controlfile里面记录的数据文件scn号,来自Online logs中的Current log头。 所以这个操作的前提是,在线日志完好没有损坏。并且,不会擦除redo log file中的内容与重置redo头的scn号。起库的时候,只能alter database open,而不能alter database open resetlogs。否则会报这个错。
SQL> alter database open noresetlogs;
alter database open noresetlogs
*
ERROR at line 1:
ORA-01139: RESETLOGS option only valid after an incomplete database recovery
所以,在重建控制文件时选择noresetlogs,oracle就会认为你想告诉他,“我很可能是不一致关闭的”,既然是不一致关闭,那怎么能alter database open resetlogs呢,所以会报上面的错。
在执行”重建控制文件脚本“的时候如果不需要在线重做日志的话可以用resetlogs选项。选择resetlogs,会将redo log file中的内容清空,sequence#由1算起。重建过程中,控制文件中datafile Checkpoint来自各数据文件头。而controlfile中记录的系统scn,会被设为0.可以通过select checkpoint_change#  from v$database;看出来。

NOARCHIVELOG还是ARCHIVELOG
重建控制文件脚本中,NOARCHIVELOG与ARCHIVELOG字段,选择重建的控制文件是使库不归档或者归档的。
一个数据库是否开启归档,是在控制文件里面辨析的。

几者之间的关系
如果要用set,那么一定要跟resetlogs那一part。因为set会改数据文件头的db_name,那么redo log file中的日志已经没用了,因为应用日志时必须匹配db_name的。
假如用set配noresetlogs会出现报错:
ORA-01223: RESETLOGS must be specified to set a new database name

关于tempfile
重建的控制文件不包含temp文件,所以重建控制文件脚本中有如下语句,
ALTER TABLESPACE TEMP ADD TEMPFILE ‘/paic/drsh101/stg/ehis/data/oradata/gs01ehis/temp01.dbf‘ REUSE;
我们也可以拷贝出来,等open库后再执行。
重建控制文件的时候,相应的redo与datafile文件必须已经存在,datafile文件必须是一致的。
而tempfile是不必要存在的,起库后,要为temp表空间添加tempfile,否则temp表空间里面空空如也。
ALTER TABLESPACE TEMP ADD TEMPFILE ‘/oradata/zxrrin/zxrrin/temp01.dbf‘ size 100m reuse autoextend on;
加上reuse是很好的,如果已经存在就覆盖掉,如果不存在就新建上。


这个命令可以备份控制文件成二进制文件。
ALTER DATABASE BACKUP CONTROLFILE TO ‘/tmp/fwy.ctl‘;