控制檔案-control file

super_sky發表於2014-01-13

什麼是控制檔案

Every Oracle database has a control file. A control file is a small binary file that records the physical structure of the database and includes:

  • The database name
  • Names and locations of associated datafiles and online redo log files
  • The timestamp of the database creation
  • The current log sequence number
  • Checkpoint information

The control file must be available for writing by the Oracle database server whenever the database is open. Without the control file, the database cannot be mounted and recovery is difficult.

The control file of an Oracle database is created at the same time as the database. By default, at least one copy of the control file is created during database creation. On some operating systems the default is to create multiple copies. You should create two or more copies of the control file during database creation. You might also need to create control files later, if you lose control files or want to change particular settings in the control files.

管理控制檔案的準則

1)提供控制檔案的路徑和名稱,需要檢視引數檔案中的control_files值

SYS@PROD>show parameter control_files;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_files                        string      /u01/app/oracle/oradata/PROD/d
                                                 isk1/control01.ctl, /u01/app/o
                                                 racle/oradata/PROD/disk1/contr
                                                 ol02.ctl, /u01/app/oracle/orad
                                                 ata/PROD/disk1/control03.ctl

2)控制檔案需要在不同的物理磁碟做映象

3)控制檔案的備份,這個是重中之重。在下面操作後,需要進行控制檔案的備份

  • Adding, dropping, or renaming datafiles
  • Adding or dropping a tablespace, or altering the read-write state of the tablespace
  • Adding or dropping redo log files or groups

 

備份控制檔案

1)備份控制檔案到一個二進位制檔案

SYS@PROD>alter database backup controlfile to '/home/oracle/controlfile_20140113.ora' ;

Database altered.

[oracle@ocm1 ~]$ strings controlfile_20140113.ora | more
}|{z
PROD
TAG20140113T154832
1PROD
1PROD
PROD
PROD
/u01/app/oracle/oradata/PROD/disk1/redo01.log
/u01/app/oracle/oradata/PROD/disk1/redo02.log
/u01/app/oracle/oradata/PROD/disk1/redo03.log
/u01/app/oracle/oradata/PROD/disk1/system01.dbf
/u01/app/oracle/oradata/PROD/disk1/undotbs01.dbf
/u01/app/oracle/oradata/PROD/disk1/sysaux01.dbf
/u01/app/oracle/oradata/PROD/disk1/temp01.dbf
/u01/app/oracle/oradata/PROD/disk1/users01.dbf
/u01/app/oracle/oradata/PROD/disk1/undotbs02.dbf
/u01/app/oracle/oradata/PROD/disk1/bigtbs01.dbf
/u01/app/oracle/oradata/PROD/disk1/redo04.log
/u01/app/oracle/oradata/PROD/disk1/redo05.log
/u01/app/oracle/oradata/PROD/disk1/redo06.log
/u01/app/oracle/oradata/PROD/disk1/redo01.log
/u01/app/oracle/oradata/PROD/disk1/redo02.log
/u01/app/oracle/oradata/PROD/disk1/redo03.log
/u01/app/oracle/oradata/PROD/disk1/system01.dbf
/u01/app/oracle/oradata/PROD/disk1/undotbs01.dbf
/u01/app/oracle/oradata/PROD/disk1/sysaux01.dbf
/u01/app/oracle/oradata/PROD/disk1/temp01.dbf
/u01/app/oracle/oradata/PROD/disk1/users01.dbf
/u01/app/oracle/oradata/PROD/disk1/undotbs02.dbf
/u01/app/oracle/oradata/PROD/disk1/bigtbs01.dbf
/u01/app/oracle/oradata/PROD/disk1/redo04.log
/u01/app/oracle/oradata/PROD/disk1/redo05.log
/u01/app/oracle/oradata/PROD/disk1/redo06.log
SYSTEM
UNDOTBS
SYSAUX
TEMPTS1
USERS
UNDOTBS2
BIGTBS
SYSTEM
UNDOTBS
SYSAUX
TEMPTS1
USERS
UNDOTBS2
BIGTBS
/u01/app/oracle/1_11_835022046.dbf
/u01/app/oracle/1_12_835022046.dbf
/u01/app/oracle/1_11_835022046.dbf
PROD
UNNAMED_INSTANCE_1
[oracle@ocm1 ~]$

2)提供建立控制檔案的SQL語句

SYS@PROD>alter database backup controlfile to trace as '/home/oracle/controlfile_trace.ora';

Database altered.

[oracle@ocm1 ~]$ more controlfile_trace.ora
-- 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="PROD"
--
-- 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=/u01/app/oracle/archlog'
-- LOG_ARCHIVE_DEST_1='OPTIONAL REOPEN=300 NODELAY'
-- LOG_ARCHIVE_DEST_1='ARCH NOAFFIRM NOEXPEDITE NOVERIFY SYNC'
-- LOG_ARCHIVE_DEST_1='REGISTER 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 "PROD" NORESETLOGS  ARCHIVELOG
    MAXLOGFILES 5
    MAXLOGMEMBERS 5
    MAXDATAFILES 100
    MAXINSTANCES 1
    MAXLOGHISTORY 292
LOGFILE
  GROUP 1 (
    '/u01/app/oracle/oradata/PROD/disk1/redo01.log',
    '/u01/app/oracle/oradata/PROD/disk1/redo04.log'
  ) SIZE 100M,
  GROUP 2 (
    '/u01/app/oracle/oradata/PROD/disk1/redo02.log',
    '/u01/app/oracle/oradata/PROD/disk1/redo05.log'
  ) SIZE 100M,
  GROUP 3 (
    '/u01/app/oracle/oradata/PROD/disk1/redo03.log',
    '/u01/app/oracle/oradata/PROD/disk1/redo06.log'
  ) SIZE 100M
-- STANDBY LOGFILE

DATAFILE
  '/u01/app/oracle/oradata/PROD/disk1/system01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/undotbs01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/sysaux01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/users01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/undotbs02.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/bigtbs01.dbf'
CHARACTER SET US7ASCII
;

-- 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 '/u01/app/oracle/archlog/1_1_835022046.dbf';
-- Recovery is required if any of the datafiles are restored backups,
-- or if the last shutdown was not normal or immediate.
RECOVER DATABASE

-- All logs need archiving and a log switch is needed.
ALTER SYSTEM ARCHIVE LOG ALL;

-- Database can now be opened normally.
ALTER DATABASE OPEN;

-- Commands to add tempfiles to temporary tablespaces.
-- Online tempfiles have complete space information.
-- Other tempfiles may require adjustment.
ALTER TABLESPACE TEMPTS1 ADD TEMPFILE '/u01/app/oracle/oradata/PROD/disk1/temp01.dbf'
     SIZE 20971520  REUSE AUTOEXTEND OFF;
-- 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 "PROD" RESETLOGS  ARCHIVELOG
    MAXLOGFILES 5
    MAXLOGMEMBERS 5
    MAXDATAFILES 100
    MAXINSTANCES 1
    MAXLOGHISTORY 292
LOGFILE
  GROUP 1 (
    '/u01/app/oracle/oradata/PROD/disk1/redo01.log',
    '/u01/app/oracle/oradata/PROD/disk1/redo04.log'
  ) SIZE 100M,
  GROUP 2 (
    '/u01/app/oracle/oradata/PROD/disk1/redo02.log',
    '/u01/app/oracle/oradata/PROD/disk1/redo05.log'
  ) SIZE 100M,
  GROUP 3 (
    '/u01/app/oracle/oradata/PROD/disk1/redo03.log',
    '/u01/app/oracle/oradata/PROD/disk1/redo06.log'
  ) SIZE 100M
-- STANDBY LOGFILE

DATAFILE
  '/u01/app/oracle/oradata/PROD/disk1/system01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/undotbs01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/sysaux01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/users01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/undotbs02.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/bigtbs01.dbf'
CHARACTER SET US7ASCII
;

-- 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 '/u01/app/oracle/archlog/1_1_835022046.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

-- Database can now be opened zeroing the online logs.
ALTER DATABASE OPEN RESETLOGS;

-- Commands to add tempfiles to temporary tablespaces.
-- Online tempfiles have complete space information.
-- Other tempfiles may require adjustment.
ALTER TABLESPACE TEMPTS1 ADD TEMPFILE '/u01/app/oracle/oradata/PROD/disk1/temp01.dbf'
     SIZE 20971520  REUSE AUTOEXTEND OFF;
-- End of tempfile additions.
--

3)使用rman 備份控制檔案

RMAN> backup current controlfile format '/u01/app/oracle/backup/controlfile_%U';

Starting backup at 13-JAN-14
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=309 devtype=DISK
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current control file in backupset
channel ORA_DISK_1: starting piece 1 at 13-JAN-14
channel ORA_DISK_1: finished piece 1 at 13-JAN-14
piece handle=/u01/app/oracle/backup/controlfile_01otvndf_1_1 tag=TAG20140113T155423 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 13-JAN-14

 

控制檔案的日常操作

1)控制檔案的新增、刪除、修改路徑、重新命名。說明一下,這些操作同一個控制檔案損壞,有控制檔案是好的情況恢復,操作步驟是一致的。如果單獨一個控制檔案損壞,我們可以把映象的控制檔案拷貝過來,啟動資料庫,這樣資料庫不需要恢復,這就是建議將控制檔案存放在不同的物理磁碟的好處。

下面以一個控制檔案損壞為例,演示其操作過程。

關閉資料庫,刪除control03.ctl控制檔案

SYS@PROD>shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

[oracle@ocm1 oracle]$ cd oradata/PROD/disk1
[oracle@ocm1 disk1]$ ls
bigtbs01.dbf   control02.ctl  redo01.log  redo03.log  redo05.log  sysaux01.dbf  temp01.dbf     undotbs02.dbf
control01.ctl  control03.ctl  redo02.log  redo04.log  redo06.log  system01.dbf  undotbs01.dbf  users01.dbf
[oracle@ocm1 disk1]$
[oracle@ocm1 disk1]$ rm -f control03.ctl

啟動資料庫

SYS@PROD>startup
ORACLE instance started.

Total System Global Area  524288000 bytes
Fixed Size                  1220384 bytes
Variable Size             159383776 bytes
Database Buffers          360710144 bytes
Redo Buffers                2973696 bytes
ORA-00205: error in identifying control file, check alert log for more info

在alert log 日誌中,我們可以看到那個日誌丟失了。

Mon Jan 13 16:09:55 2014
ALTER DATABASE   MOUNT
Mon Jan 13 16:09:56 2014
ORA-00202: control file: '/u01/app/oracle/oradata/PROD/disk1/control03.ctl'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
Mon Jan 13 16:09:56 2014
ORA-205 signalled during: ALTER DATABASE   MOUNT...

關閉資料庫

SYS@PROD>shutdown abort
ORACLE instance shut down.

拷貝其他控制檔案並重新命名為/u01/app/oracle/oradata/PROD/disk1/control03.ctl
[oracle@ocm1 oracle]$ cp /u01/app/oracle/oradata/PROD/disk1/control02.ctl /u01/app/oracle/oradata/PROD/disk1/control03.ctl

啟動資料庫

SYS@PROD>startup
ORACLE instance started.

Total System Global Area  524288000 bytes
Fixed Size                  1220384 bytes
Variable Size             159383776 bytes
Database Buffers          360710144 bytes
Redo Buffers                2973696 bytes
Database mounted.
Database opened.

 

2)使用alter database backup controlfile to '/home/oracle/controlfile_20140113.ora' ; 備份的控制檔案恢復,只要使用作業系統級別的拷貝即可

例如:cp /home/oracle/controlfile_20140113.ora /u01/app/oracle/oradata/PROD/disk1/control03.ctl

 

3)控制檔案全部損壞,但是有rman備份

備份控制檔案

RMAN> backup current controlfile format '/u01/app/oracle/backup/controlfile_%U'; 

Starting backup at 13-JAN-14
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=308 devtype=DISK
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current control file in backupset
channel ORA_DISK_1: starting piece 1 at 13-JAN-14
channel ORA_DISK_1: finished piece 1 at 13-JAN-14
piece handle=/u01/app/oracle/backup/controlfile_02otvomu_1_1 tag=TAG20140113T161630 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 13-JAN-14

關閉資料庫並刪除全部控制檔案

SYS@PROD>shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

[oracle@ocm1 disk1]$ rm -f control0*

啟動資料庫到mount

SYS@PROD>startup nomount
ORACLE instance started.

Total System Global Area  524288000 bytes
Fixed Size                  1220384 bytes
Variable Size             159383776 bytes
Database Buffers          360710144 bytes
Redo Buffers                2973696 bytes
SYS@PROD>
SYS@PROD>alter database mount;
alter database mount
*
ERROR at line 1:
ORA-00205: error in identifying control file, check alert log for more info

在alert log日誌中可以看到如下資訊

Mon Jan 13 16:19:35 2014
alter database mount
Mon Jan 13 16:19:35 2014
ORA-00202: control file: '/u01/app/oracle/oradata/PROD/disk1/control01.ctl'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
Mon Jan 13 16:19:35 2014
ORA-205 signalled during: alter database mount...

使用rman 進行恢復

RMAN> restore controlfile from '/u01/app/oracle/backup/controlfile_02otvomu_1_1';

Starting restore at 13-JAN-14
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=321 devtype=DISK

channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:03
output filename=/u01/app/oracle/oradata/PROD/disk1/control01.ctl
output filename=/u01/app/oracle/oradata/PROD/disk1/control02.ctl
output filename=/u01/app/oracle/oradata/PROD/disk1/control03.ctl
Finished restore at 13-JAN-14

啟動資料庫

SYS@PROD>alter database mount;

Database altered.

SYS@PROD>alter database open;
alter database open
*
ERROR at line 1:
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open

SYS@PROD>alter database open resetlogs;
alter database open resetlogs
*
ERROR at line 1:
ORA-01152: file 1 was not restored from a sufficiently old backup
ORA-01110: data file 1: '/u01/app/oracle/oradata/PROD/disk1/system01.dbf'

檢查一下

SYS@PROD>select checkpoint_change# from v$database;

CHECKPOINT_CHANGE#
------------------
            789731

SYS@PROD>
SYS@PROD>select checkpoint_change# from v$datafile;

CHECKPOINT_CHANGE#
------------------
            789731
            789731
            789731
            789731
            789731
            789731

6 rows selected.

SYS@PROD>select checkpoint_change# from v$datafile_header;

CHECKPOINT_CHANGE#
------------------
            790006
            790006
            790006
            790006
            790006
            790006

6 rows selected.

SYS@PROD>alter system set "_allow_resetlogs_corruption"=true scope=spfile;

System altered.

SYS@PROD>

SYS@PROD>shutdown abort
ORACLE instance shut down.

啟動資料庫到mount狀態

SYS@PROD>recover database using backup controlfile until cancel;
ORA-00279: change 789867 generated at 01/13/2014 16:39:09 needed for thread 1
ORA-00289: suggestion : /u01/app/oracle/archlog/1_1_836757538.dbf
ORA-00280: change 789867 for thread 1 is in sequence #1

Specify log: {=suggested | filename | AUTO | CANCEL}

ORA-00308: cannot open archived log '/u01/app/oracle/archlog/1_1_836757538.dbf'
ORA-27037: unable to obtain file status
Linux 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: '/u01/app/oracle/oradata/PROD/disk1/system01.dbf'

SYS@PROD>alter database open resetlogs;
alter database open resetlogs
*
ERROR at line 1:
ORA-01092: ORACLE instance terminated. Disconnection forced

SYS@PROD>startup mount;
ORACLE instance started.

Total System Global Area  524288000 bytes
Fixed Size                  1220384 bytes
Variable Size             159383776 bytes
Database Buffers          360710144 bytes
Redo Buffers                2973696 bytes
Database mounted.
SYS@PROD>
SYS@PROD>alter database open;

Database altered.

SYS@PROD>

 

4)使用controlfile 建立語句,這個主要就是指令碼,如果沒有controlfile指令碼的備份,那就需要自己根據已知的資訊來寫了。恢復過程同步驟3

STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "PROD" NORESETLOGS  ARCHIVELOG
    MAXLOGFILES 5
    MAXLOGMEMBERS 5
    MAXDATAFILES 100
    MAXINSTANCES 1
    MAXLOGHISTORY 292
LOGFILE
  GROUP 1 (
    '/u01/app/oracle/oradata/PROD/disk1/redo01.log',
    '/u01/app/oracle/oradata/PROD/disk1/redo04.log'
  ) SIZE 100M,
  GROUP 2 (
    '/u01/app/oracle/oradata/PROD/disk1/redo02.log',
    '/u01/app/oracle/oradata/PROD/disk1/redo05.log'
  ) SIZE 100M,
  GROUP 3 (
    '/u01/app/oracle/oradata/PROD/disk1/redo03.log',
    '/u01/app/oracle/oradata/PROD/disk1/redo06.log'
  ) SIZE 100M
-- STANDBY LOGFILE

DATAFILE
  '/u01/app/oracle/oradata/PROD/disk1/system01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/undotbs01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/sysaux01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/users01.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/undotbs02.dbf',
  '/u01/app/oracle/oradata/PROD/disk1/bigtbs01.dbf'
CHARACTER SET US7ASCII
;

需要說明的是,步驟3和4都開啟了隱含引數。如果可能需要重建庫。

 

控制檔案相關檢視

View
Description

V$DATABASE

Displays database information from the control file

V$CONTROLFILE

Lists the names of control files

V$CONTROLFILE_RECORD_SECTION

Displays information about control file record sections

V$PARAMETER

Can be used to display the names of control files as specified in the CONTROL_FILES initialization parameter

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11590946/viewspace-1070096/,如需轉載,請註明出處,否則將追究法律責任。

相關文章