Oracle控制文件操作


控制文件是连接instance和 database的纽带。记录了database的结构信息。
控制文件是1个2进制文件。记录的是当前database的状态。
控制文件可以有多个,在参数文件中通过control_files参数指定位置,在需要向控制文件写入时,会同步的向多个控制文件写入。读取控制文件时,只会读取第一个控制文件。如果任意一个控制文件损坏,则实例会abort。
控制文件只能和1个database相关联。
控制文件是在创建database的时候创建的。也可以在启动到nomount状态时重建。
与control file相关的视图
v$controlfile:当前instance中所有的控制文件的信息。
v$controlfile_record_section: 控制文件中所有的section信息。


查看当前控制文件信息:
 select * from v$controlfile;
 select * from v$parameter where name like '%control%';
 show parameter control;
 select * from v$controlfile_record_section;

 

用命令修改控制文件的路径

alter system set control_files='/u01/app/oracle/oradata/saigon/control01.ctl',
                               '/u01/app/oracle/oradata/saigon/control02.ctl',
                               '/u01/app/oracle/oradata/saigon/control03.ctl' scope=spfile;

 

 

使用spfile增加控制文件个数或修改控制文件路径
(1)利用v$controlfile 来获取现有控制文件名字和位置.
(2)修改spfile,使用
alter system set control_files=
'D:\DISK3\CONTROL01.CTL',
'D:\DISK6\CONTROL02.CTL',
'D:\DISK9\CONTROL03.CTL' SCOPE=SPFIL;
(3)正常关闭数据库,(shutdown,shutdown immediate).
(4)使用操作系统的复制命令将现有控制文件复制到指定位置.
(5)重新启动oracle数据库(startup)
(6)利用数据字段v$controlfile来验证新的控制文件名字是否正确.
(7)如有错误,重复上述操作:如果无误,删除原有的控制文件.

 

使用pfile增加控制文件个数或修改控制文件路径
 1.干净的关闭数据库。
 2.在操作系统上复制1个新的控制文件并改名。
 3.在initSID.ora中的control_files参数中添加之前的参数文件。
 4.启动数据库。

在oracle运行过程中备份控制文件
 1.alter database backup controlfile to 'D:\aaa.bak';
 2.alter database backup controlfile to trace;  把控制文件翻译成创建控制文件的脚本,路径在用户警告文件的目录下(可以通过show parameter user_dump;查看),后缀名为trc。

或根据下面方式找到:

SELECT d.VALUE
|| '/'
|| LOWER (RTRIM (i.INSTANCE, CHR (0)))
|| '_ora_'
|| p.spid
|| '.trc' trace_file_name
FROM (SELECT p.spid
FROM v$mystat m, v$session s, v$process p
WHERE m.statistic# = 1 AND s.SID = m.SID AND p.addr = s.paddr) p,
(SELECT t.INSTANCE
FROM v$thread t, v$parameter v
WHERE v.NAME = 'thread'
AND (v.VALUE = 0 OR t.thread# = TO_NUMBER (v.VALUE))) i,
(SELECT VALUE
FROM v$parameter
WHERE NAME = 'user_dump_dest') d
/

3.

run{
backup current controlfile format'/backup1/controlfile_%d_%s.ctl';
}

 

控制文件的的恢复

resetlog方式打开数据只要拥有当前的日志文件是可以实现完全恢复的。 
是否需要使用resetlogs方式打开,则取决于是否使用的是备份的控制文件。
如果使用的是备份的控制文件则需要使用resetlogs方式打开数据库;
如果拥有当前的控制文件或者通过重建控制文件来恢复,就不需要通过resetlogs方式打开。

RMAN>restore controlfile to '/tmp/control01.ctl' from 'c-3152029224-20051221-00'

-------恢复控制文件用户resetlogs方式打开

run{
startup force nomount;

set dbid=
restore controlfile from autobackup;
alter database mount;
recover database;
alter database open resetlogs;
}

-------恢复控制文件用正常方式打开
1.startup nomount;
2.RMAN>restore controlfile from autobackup;
3.alter database mount;
4.SQL>alter database backup controlfile to trace;
5.找到trace文件
SELECT d.VALUE
|| '/'
|| LOWER (RTRIM (i.INSTANCE, CHR (0)))
|| '_ora_'
|| p.spid
|| '.trc' trace_file_name
FROM (SELECT p.spid
FROM v$mystat m, v$session s, v$process p
WHERE m.statistic# = 1 AND s.SID = m.SID AND p.addr = s.paddr) p,
(SELECT t.INSTANCE
FROM v$thread t, v$parameter v
WHERE v.NAME = 'thread'
AND (v.VALUE = 0 OR t.thread# = TO_NUMBER (v.VALUE))) i,
(SELECT VALUE
FROM v$parameter
WHERE NAME = 'user_dump_dest') d
/
6.重建控制文件打开数据库

 

重建控制文件方法如下:

A,确定控制文件,重做日志文件,数据文件位置(A某些情况下需要);

select * from v$controlfile;

select * from v$logfile;

select file#,name from v$datafile;

B,生成可以重建控制文件的脚本

alter database backup controlfile to trace;

C,获取trc文件位置的脚本

SELECT    d.VALUE
       || '/'
       || LOWER (RTRIM (i.INSTANCE, CHR (0)))
       || '_ora_'
       || p.spid
       || '.trc' trace_file_name
  FROM (SELECT p.spid
          FROM v$mystat m, v$session s, v$process p
         WHERE m.statistic# = 1 AND s.SID = m.SID AND p.addr = s.paddr) p,
       (SELECT t.INSTANCE
          FROM v$thread t, v$parameter v
         WHERE v.NAME = 'thread'
           AND (v.VALUE = 0 OR t.thread# = TO_NUMBER (v.VALUE))) i,
       (SELECT VALUE
          FROM v$parameter
         WHERE NAME = 'user_dump_dest') d
/

D,查看trc文件内容

 

[oracle@orcl admin]$ cat /home/oracle/admin/orcl/udump/orcl_ora_10202.trc

/home/oracle/admin/orcl/udump/orcl_ora_10202.trc

Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production

With the Partitioning, OLAP and Data Mining options

ORACLE_HOME = /home/oracle/oracle

System name:   Linux

Node name:     orcl

Release:       2.6.18-164.el5

Version:       #1 SMP Tue Aug 18 15:51:54 EDT 2009

Machine:       i686

Instance name: orcl

Redo thread mounted by this instance: 1

Oracle process number: 15

Unix process pid: 10202, image: oracle@orcl (TNS V1-V3)

 

*** SERVICE NAME:(SYS$USERS) 2012-05-27 12:14:16.466

*** SESSION ID:(159.3) 2012-05-27 12:14:16.466

*** 2012-05-27 12:14:16.466

-- 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="orcl"

--

-- 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_10='LOCATION=USE_DB_RECOVERY_FILE_DEST'

-- LOG_ARCHIVE_DEST_10='OPTIONAL REOPEN=300 NODELAY'

-- LOG_ARCHIVE_DEST_10='ARCH NOAFFIRM NOEXPEDITE NOVERIFY SYNC'

-- LOG_ARCHIVE_DEST_10='REGISTER NOALTERNATE NODEPENDENCY'

-- LOG_ARCHIVE_DEST_10='NOMAX_FAILURE NOQUOTA_SIZE NOQUOTA_USED NODB_UNIQUE_NAME'

-- LOG_ARCHIVE_DEST_10='VALID_FOR=(PRIMARY_ROLE,ONLINE_LOGFILES)'

-- LOG_ARCHIVE_DEST_STATE_10=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 "ORCL" NORESETLOGS NOARCHIVELOG

   MAXLOGFILES 16

   MAXLOGMEMBERS 3

   MAXDATAFILES 100

   MAXINSTANCES 8

   MAXLOGHISTORY 292

LOGFILE

 GROUP 1 '/home/oracle/oradata/orcl/redo01.log' SIZE 50M,

 GROUP 2 '/home/oracle/oradata/orcl/redo02.log' SIZE 50M,

 GROUP 3 '/home/oracle/oradata/orcl/redo03.log' SIZE 50M

-- STANDBY LOGFILE

DATAFILE

 '/home/oracle/oradata/orcl/system01.dbf',

 '/home/oracle/oradata/orcl/undotbs01.dbf',

 '/home/oracle/oradata/orcl/sysaux01.dbf',

 '/home/oracle/oradata/orcl/users01.dbf',

 '/home/oracle/oradata/orcl/example01.dbf'

CHARACTER SET WE8ISO8859P1

;

-- 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 '/home/oracle/flash_recovery_area/ORCL/archivelog/2012_05_27/o1_mf_1_1_%u_.arc';

-- ALTER DATABASE REGISTER LOGFILE '/home/oracle/flash_recovery_area/ORCL/archivelog/2012_05_27/o1_mf_1_1_%u_.arc';

-- Recovery is required if any of the datafiles are restored backups,

-- or if the last shutdown was not normal or immediate.

RECOVER DATABASE

-- 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 TEMP ADD TEMPFILE '/home/oracle/oradata/orcl/temp01.dbf'

    SIZE 22020096 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;

-- 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 "ORCL" RESETLOGS NOARCHIVELOG

   MAXLOGFILES 16

   MAXLOGMEMBERS 3

   MAXDATAFILES 100

   MAXINSTANCES 8

   MAXLOGHISTORY 292

LOGFILE

 GROUP 1 '/home/oracle/oradata/orcl/redo01.log' SIZE 50M,

 GROUP 2 '/home/oracle/oradata/orcl/redo02.log' SIZE 50M,

 GROUP 3 '/home/oracle/oradata/orcl/redo03.log' SIZE 50M

-- STANDBY LOGFILE

DATAFILE

 '/home/oracle/oradata/orcl/system01.dbf',

 '/home/oracle/oradata/orcl/undotbs01.dbf',

 '/home/oracle/oradata/orcl/sysaux01.dbf',

 '/home/oracle/oradata/orcl/users01.dbf',

 '/home/oracle/oradata/orcl/example01.dbf'

CHARACTER SET WE8ISO8859P1

;

-- 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 '/home/oracle/flash_recovery_area/ORCL/archivelog/2012_05_27/o1_mf_1_1_%u_.arc';

-- ALTER DATABASE REGISTER LOGFILE '/home/oracle/flash_recovery_area/ORCL/archivelog/2012_05_27/o1_mf_1_1_%u_.arc';

-- 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 TEMP ADD TEMPFILE '/home/oracle/oradata/orcl/temp01.dbf'

    SIZE 22020096 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;

-- End of tempfile additions.

E,获取创建控制文件的脚本,根据数据库不同状况,可以选择是使用RESETLOGS(没有重做日志的情况)/NORESETLOGS(有重做日志的情况)来重建控制文件,下面为NORESETLOGS.

STARTUP NOMOUNT

CREATE CONTROLFILE REUSE DATABASE "ORCL" NORESETLOGS NOARCHIVELOG

   MAXLOGFILES 16

   MAXLOGMEMBERS 3

   MAXDATAFILES 100

   MAXINSTANCES 8

   MAXLOGHISTORY 292

LOGFILE

 GROUP 1 '/home/oracle/oradata/orcl/redo01.log' SIZE 50M,

 GROUP 2 '/home/oracle/oradata/orcl/redo02.log' SIZE 50M,

 GROUP 3 '/home/oracle/oradata/orcl/redo03.log' SIZE 50M

-- STANDBY LOGFILE

DATAFILE

 '/home/oracle/oradata/orcl/system01.dbf',

 '/home/oracle/oradata/orcl/undotbs01.dbf',

 '/home/oracle/oradata/orcl/sysaux01.dbf',

 '/home/oracle/oradata/orcl/users01.dbf',

 '/home/oracle/oradata/orcl/example01.dbf'

CHARACTER SET WE8ISO8859P1;

RECOVER DATABASE

ALTER DATABASE OPEN;

ALTER TABLESPACE TEMP ADD TEMPFILE '/home/oracle/oradata/orcl/temp01.dbf'

    SIZE 22020096 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;

F,运行脚本重建控制文件

SQL> set echo on

SQL> STARTUP NOMOUNT

CREATE CONTROLFILE REUSE DATABASE "ORCL" NORESETLOGS NOARCHIVELOG

   MAXLOGFILES 16

   MAXLOGMEMBERS 3

   MAXDATAFILES 100

   MAXINSTANCES 8

   MAXLOGHISTORY 292

LOGFILE

 GROUP 1 '/home/oracle/oradata/orcl/redo01.log' SIZE 50M,

 GROUP 2 '/home/oracle/oradata/orcl/redo02.log' SIZE 50M,

 GROUP 3 '/home/oracle/oradata/orcl/redo03.log' SIZE 50M

-- STANDBY LOGFILE

DATAFILE

 '/home/oracle/oradata/orcl/system01.dbf',

 '/home/oracle/oradata/orcl/undotbs01.dbf',

 '/home/oracle/oradata/orcl/sysaux01.dbf',

 '/home/oracle/oradata/orcl/users01.dbf',

 '/home/oracle/oradata/orcl/example01.dbf'

CHARACTER SET WE8ISO8859P1;

RECOVER DATABASE

ALTER DATABASE OPEN;

ALTER TABLESPACE TEMP ADD TEMPFILE '/home/oracle/oradata/orcl/temp01.dbf'

    SIZE 22020096 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;

ORACLE instance started.

Total System Global Area 167772160 bytes

Fixed Size                 1218316 bytes

Variable Size             71305460 bytes

Database Buffers          92274688 bytes

Redo Buffers               2973696 bytes

SQL>  2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18

Control file created.

SQL> Media recovery complete.

SQL>

Database altered.

SQL>  2

Tablespace altered.

 

 

你可能感兴趣的:(oracle)