时间:2021-07-01 10:21:17 帮助过:41人阅读
首先我们应该先了解一下控制文件的重要性 控制文件的概念: 控制文件是一个很小的二进制文件,用于记录数据库的物理结构。一个控制文件只属于一个数据库。创建数据库时,创建控制文件。当数据库的物理结构改变的时候,Oracle会更新控制文件。用户不能编辑控
首先我们应该先了解一下控制文件的重要性
控制文件的概念:控制文件是一个很小的二进制文件,用于记录数据库的物理结构。一个控制文件只属于一个数据库。创建数据库时,创建控制文件。当数据库的物理结构改变的时候,Oracle会更新控制文件。用户不能编辑控制文件,控制文件的修改由Oracle完成。
数据库的启动和正常运行都离不开控制文件。启动数据库时,Oracle从初始化参数文件中获得控制文件的名字及位置,打开控制文件,然后从控制文件中读取数据文件和联机日志文件的信息,最后打开数据库。数据库运行时,Oracle会修改控制文件,所以,一旦控制文件损坏,数据库将不能正常运行。如果没有数据库的备份和归档日志文件,数据库将无法恢复。因此,我们应该多路镜像控制文件(Multiplex Control Files),并把每个镜像的控制文件分布在不同的物理磁盘。根据经验,控制文件多路镜像以后,几个控制文件同时坏掉的可能性几乎为零。控制文件管理的重心是重在预防,而不是亡羊补牢!
控制文件的内容:
数据库名称(Database Name)
创建数据库的时间戳
数据文件的名字及位置
联机日志文件的名字及位置
表空间信息
日志历史记录(Log History)
归档日志的信息
备份信息
当前的日志序列号(Log Sequence Number)
检查点信息(Checkpoint)
控制文件的大小由下面的几个参数决定:
MAXLOGFILES
MAXLOGMEMBERS
MAXLOGHISTORY
MAXDATAFILES
MAXINSTANCES
当增加、重命名、删除一个数据文件或者一个联机日志文件时,Oracle服务器进程(Server Process)会立即更新控制文件以反映数据库结构的这种变化。所以,Oracle总是告诫我们,在数据库的结构发生变化后,要备份控制文件。日志写进程LGWR负责把当前日志序列号记录到控制文件中。校验点进程CKPT负责把校验点的信息记录到控制文件中。归档进程负责把归档日志的信息记录到控制文件中。
初始化参数CONTROL_FILES的值记录控制文件的位置。通常,DBA应该镜像控制文件,把每个控制文件分布到不同的物理磁盘,发生灾难时,即使其中一个控制文件损坏,数据不会丢失,也不会使整个数据库陷于瘫痪。
我们了解了控制文件的重要性后,如果我们真的将控制文件丢失,我们该如何做呢?
1、 从备份中拷贝控制文件
2、 重建控制文件
一、重建控制文件:
Oracle提供如下命令:
alter databasebackup controlfile to trace;
操作步骤如下:
1、 连接数据库并查看版本
[oracle@edbjr2p1~]$ sqlplus / as sysdba
SQL*Plus:Release 10.2.0.1.0 - Production on Sat Mar 15 18:45:48 2014
Copyright (c)1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database10g Enterprise Edition Release 10.2.0.1.0 - Production
With thePartitioning, OLAP and Data Mining options
SYS@PROD>selectversion from v$instance;
VERSION
-----------------
10.2.0.1.0
SYS@PROD>archivelog list
Database logmode Archive Mode
Automaticarchival Enabled
Archivedestination /u01/app/oracle/product/10.2.0/db_1/dbs/arch
Oldest onlinelog sequence 2
Next logsequence to archive 4
Current logsequence 4
SYS@PROD>
2、得到创建控制文件的语句与操作
SYS@PROD>alterdatabase backup controlfile to trace;
Databasealtered.
3、查看跟踪文件的内容
[oracle@edbjr2p1udump]$ more prod_ora_18466.trc
Dump file/u01/app/oracle/admin/PROD/udump/prod_ora_17286.trc
Oracle Database10g Enterprise Edition Release 10.2.0.1.0 - Production
With thePartitioning, OLAP and Data Mining options
ORACLE_HOME =/u01/app/oracle/product/10.2.0/db_1
Systemname: Linux
Node name: edbjr2p1
Release: 2.6.18-238.el5
Version: #1 SMP Tue Jan 4 15:24:05 EST 2011
Machine: i686
Instance name:PROD
Redo threadmounted by this instance: 1
Oracle processnumber: 26
Unix processpid: 17286, image: oracle@edbjr2p1 (TNS V1-V3)
*** 2014-03-1520:15:21.711
*** SERVICENAME:(SYS$USERS) 2014-03-15 20:15:21.711
*** SESSIONID:(289.3) 2014-03-15 20:15:21.711
tkcrrsarc:(WARN) Failed to find ARCH for message (message:0x1)
tkcrrpa: (WARN)Failed initial attempt to send ARCH message (message:0x1)
*** 2014-03-1520:15:34.425
-- The followingare current System-scope REDO Log Archival related
-- parametersand 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/product/10.2.0/db_1/dbs/arch'
--LOG_ARCHIVE_DEST_1='MANDATORY NOREOPEN NODELAY'
--LOG_ARCHIVE_DEST_1='ARCH NOAFFIRM EXPEDITE NOVERIFY SYNC'
-- LOG_ARCHIVE_DEST_1='NOREGISTERNOALTERNATE 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 twosets of SQL statements, each of which creates a new
-- control fileand uses it to open the database. The first set opens
-- the databasewith the NORESETLOGS option and should be used only if
-- the currentversions of all online logs are available. The second
-- set opens thedatabase with the RESETLOGS option and should be used
-- if onlinelogs are unavailable.
-- Theappropriate set of statements can be copied from the trace into
-- a scriptfile, edited as necessary, and executed when there is a
-- need tore-create the control file.
--
-- Set #1. NORESETLOGS case
--
-- The followingcommands will create a new control file and use it
-- to open thedatabase.
-- Data used byRecovery Manager will be lost.
-- Additionallogs may be required for media recovery of offline
-- Use this onlyif the current versions of all online logs are
-- available.
-- Aftermounting the created controlfile, the following SQL
-- statementwill place the database in the appropriate
-- protectionmode:
-- ALTER DATABASE SET STANDBY DATABASE TOMAXIMIZE PERFORMANCE
STARTUP NOMOUNT
CREATECONTROLFILE REUSE DATABASE "PROD" NORESETLOGS ARCHIVELOG
MAXLOGFILES 30
MAXLOGMEMBERS 5
MAXDATAFILES 100
MAXINSTANCES 1
MAXLOGHISTORY 292
LOGFILE
GROUP 1 (
'/u01/app/oracle/oradata/PROD/disk1/redo01.log',
'/u01/app/oracle/oradata/PROD/disk2/redo01_b.log'
) SIZE 100M,
GROUP 2 (
'/u01/app/oracle/oradata/PROD/disk1/redo02.log',
'/u01/app/oracle/oradata/PROD/disk2/redo02_b.log'
) SIZE 100M,
GROUP 3 (
'/u01/app/oracle/oradata/PROD/disk1/redo03.log',
'/u01/app/oracle/oradata/PROD/disk2/redo03_b.log'
) SIZE 100M,
GROUP 4 (
'/u01/app/oracle/oradata/PROD/disk1/redo04_a.log',
'/u01/app/oracle/oradata/PROD/disk2/redo04_b.log'
) SIZE100M
-- STANDBYLOGFILE
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/EXAMPLE_01.dbf',
'/u01/app/oracle/oradata/PROD/disk3/USERS_01.dbf',
'/u01/app/oracle/oradata/PROD/disk5/dds_01.dbf'
CHARACTER SETUS7ASCII
;
-- Commands tore-create incarnation table
-- Below lognames MUST be changed to existing filenames on
-- disk. Any onelog file from each branch can be used to
-- re-createincarnation records.
-- ALTERDATABASE REGISTER LOGFILE'/u01/app/oracle/product/10.2.0/db_1/dbs/arch1_1_842277968.dbf';
-- Recovery isrequired if any of the datafiles are restored backups,
-- or if thelast shutdown was not normal or immediate.
RECOVER DATABASE
-- All logs needarchiving and a log switch is needed.
ALTER SYSTEMARCHIVE LOG ALL;
-- Database cannow be opened normally.
ALTER DATABASEOPEN;
-- Commands toadd tempfiles to temporary tablespaces.
-- Onlinetempfiles have complete space information.
-- Othertempfiles may require adjustment.
ALTER TABLESPACETEMP01 ADD TEMPFILE '/u01/app/oracle/oradata/PROD/disk1/temp01.dbf'
SIZE 20971520 REUSE AUTOEXTEND ON NEXT 8192 MAXSIZE 32767M;
ALTER TABLESPACETEMP1 ADD TEMPFILE '/u01/app/oracle/oradata/PROD/disk1/temp1_01_grp.dbf'
SIZE 20971520 REUSE AUTOEXTEND ON NEXT 8192 MAXSIZE 32767M;
ALTER TABLESPACETEMP2 ADD TEMPFILE '/u01/app/oracle/oradata/PROD/disk1/temp2_01_grp.dbf'
SIZE 20971520 REUSE AUTOEXTEND ON NEXT 8192 MAXSIZE 32767M;
-- End oftempfile additions.
--
*** 2014-03-1520:18:32.065
*** 2014-03-1520:18:32.065 60679 kcrr.c
ARCH: Archivaldisabled due to shutdown: 1089
*** 2014-03-1520:18:32.082 60679 kcrr.c
ARCH: Archivaldisabled due to shutdown: 1089
4、编辑这个trace文件,我们就可以获得创建控制文件的脚本.
根据数据库不同状况,你可以选择是使用RESETLOGS/NORESETLOGS来重建控制文件
我们获得以下脚本:
[oracle@standby tools]$ cat createctlf.sql
STARTUP NOMOUNT
CREATECONTROLFILE REUSE DATABASE "PROD" NORESETLOGS ARCHIVELOG
MAXLOGFILES 30
MAXLOGMEMBERS 4
MAXDATAFILES 100
MAXINSTANCES 1
MAXLOGHISTORY 292
LOGFILE
GROUP 1 (
'/u01/app/oracle/oradata/PROD/disk1/redo01.log',
'/u01/app/oracle/oradata/PROD/disk2/redo01_b.log'
) SIZE 100M,
GROUP 2 (
'/u01/app/oracle/oradata/PROD/disk1/redo02.log',
'/u01/app/oracle/oradata/PROD/disk2/redo02_b.log'
) SIZE 100M,
GROUP 3 (
'/u01/app/oracle/oradata/PROD/disk1/redo03.log',
'/u01/app/oracle/oradata/PROD/disk2/redo03_b.log'
) SIZE 100M,
GROUP 4 (
'/u01/app/oracle/oradata/PROD/disk1/redo04_a.log',
'/u01/app/oracle/oradata/PROD/disk2/redo04_b.log'
) SIZE 100M
-- STANDBYLOGFILE
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/EXAMPLE_01.dbf',
'/u01/app/oracle/oradata/PROD/disk3/USERS_01.dbf',
'/u01/app/oracle/oradata/PROD/disk5/dds_01.dbf'
CHARACTER SET US7ASCII;
RECOVERDATABASE;
ALTER SYSTEMARCHIVE LOG ALL;
ALTER DATABASEOPEN;
ALTER TABLESPACETEMP01 ADD TEMPFILE '/u01/app/oracle/oradata/PROD/disk1/temp01.dbf'
SIZE 20971520 REUSE AUTOEXTEND ON NEXT 8192 MAXSIZE 32767M;
ALTER TABLESPACETEMP1 ADD TEMPFILE '/u01/app/oracle/oradata/PROD/disk1/temp1_01_grp.dbf'
SIZE 20971520 REUSE AUTOEXTEND ON NEXT 8192 MAXSIZE 32767M;
ALTER TABLESPACETEMP2 ADD TEMPFILE '/u01/app/oracle/oradata/PROD/disk1/temp2_01_grp.dbf'
SIZE 20971520 REUSE AUTOEXTEND ON NEXT 8192 MAXSIZE 32767M;
5、运行此脚本即可重建控制文件:
SYS@PROD>startupnomount;
ORACLE instancestarted.
Total SystemGlobal Area 419430400 bytes
Fixed Size 1219784 bytes
VariableSize 121635640 bytes
DatabaseBuffers 293601280 bytes
RedoBuffers 2973696 bytes
SYS@PROD>CREATECONTROLFILE REUSE DATABASE "PROD" NORESETLOGS ARCHIVELOG
MAXLOGFILES 30
MAXLOGMEMBERS 4
MAXDATAFILES 100
MAXINSTANCES 1
MAXLOGHISTORY 292
LOGFILE
GROUP 1 (
'/u01/app/oracle/oradata/PROD/disk1/redo01.log',
'/u01/app/oracle/oradata/PROD/disk2/redo01_b.log'
) SIZE 100M,
GROUP 2 (
'/u01/app/oracle/oradata/PROD/disk1/redo02.log',
'/u01/app/oracle/oradata/PROD/disk2/redo02_b.log'
) SIZE 100M,
GROUP 3 (
'/u01/app/oracle/oradata/PROD/disk1/redo03.log',
'/u01/app/oracle/oradata/PROD/disk2/redo03_b.log'
) SIZE 100M,
GROUP 4 (
'/u01/app/oracle/oradata/PROD/disk1/redo04_a.log',
'/u01/app/oracle/oradata/PROD/disk2/redo04_b.log'
) SIZE 100M
-- STANDBYLOGFILE
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/EXAMPLE_01.dbf',
'/u01/app/oracle/oradata/PROD/disk3/USERS_01.dbf',
'/u01/app/oracle/oradata/PROD/disk5/dds_01.dbf'
CHARACTER SETUS7ASCII;
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Control filecreated.
SYS@PROD>RECOVERDATABASE;
ORA-00283:recovery session canceled due to errors
ORA-00264: norecovery required
SYS@PROD>ALTERSYSTEM ARCHIVE LOG ALL;
System altered.
SYS@PROD>ALTERDATABASE OPEN;
Databasealtered.
SYS@PROD>ALTERTABLESPACE TEMP01 ADD TEMPFILE '/u01/app/oracle/oradata/PROD/disk1/temp01.dbf'
SIZE 20971520 REUSE AUTOEXTEND ON NEXT 8192 MAXSIZE 32767M;
ALTER TABLESPACETEMP1 ADD TEMPFILE '/u01/app/oracle/oradata/PROD/disk1/temp1_01_grp.dbf'
SIZE 20971520 REUSE AUTOEXTEND ON NEXT 8192 MAXSIZE 32767M;
ALTER TABLESPACETEMP2 ADD TEMPFILE '/u01/app/oracle/oradata/PROD/disk1/temp2_01_grp.dbf'
SIZE 20971520 REUSE AUTOEXTEND ON NEXT 8192 MAXSIZE 32767M;
2
Tablespacealtered.
SYS@PROD> 2
Tablespacealtered.
SYS@PROD> 2
Tablespacealtered.
SYS@PROD>
二、备份控制文件
以上给出生成创建控制文件脚本并重建控制文件的方法,但是具体恢复中遇到的问题可能需要具体对待,当得不到trace文件,可根据这个格式,查看数据文件,更改相关内容再重建.这种方法通常是在没有控制文件(二进制文件)备份的情况下所采用的,如果存在备份应该使用备份的控制文件尝试恢复.