Oracle控制文件详解

一、Oracle控制文件

为二进制文件,初始化大小由CREATEDATABASE指定,可以使用RMAN备份

记录了当前数据库的结构信息,同时也包含数据文件及日志文件的信息以及相关的状态,归档信息等等

在参数文件中描述其位置,个数等等。通常采用分散放开,多路复用的原则。在mount阶段被读取,open阶段一直被使用

维护数据库一致性(数据库启动时会比较控制文件与联机日志文件中的ckpt,即起始scn号,如相等则正常启动,否则需要介质恢复)

一个控制文件只能属于一个数据库

控制文件的任意修改将写入到初始化参数中指定的所有控制文件中,读取时则仅读取第一个控制文件

控制文件只能连接一个数据库,控制文件的大小一般不要超过MB,最多为个,最少一个,互为镜像

控制文件中包含的内容

数据库的名字、ID、创建的时间戳

表空间的名字

联机日志文件、数据文件的位置、个数、名字

联机日志的Sequence号码

检查点的信息

撤销段的开始或结束

归档信息

备份信息

二、查看控制文件的相关信息

1.使用相关视图来查看

V$CONTROLFILE--列出实例中所有控制文件的名字及状态信息

V$PARAMETER--列出所有参数的位置及状态信息

V$CONTROLFILE_RECORD_SECTION--列出控制文件中记录的部分信息

SHOW PARAMETER CONTROL_FILES--列出控制文件的名字、状态、位置等

2.使用STRINGS命令来查看控制文件中的具体内容

3.备份控制文件到平面文件(然后查看控制文件中的具体内容)

SQL>alterdatabasebackupcontrolfiletotraceas'/u01/app/oracle/ctl.txt';

Databasealtered.

4.转储控制文件内容(查看控制文件中的具体内容)

altersessionsetevents'immediate trace name CONTROLF level 12';level表示级别

或--level1块头的内容--level2数据文件内容--levle 10 |12所有内容

oradebug setmypid

oradebugdumpcontrolf 12

SQL>altersystemsetevents'immediate trace name controlf level 10';

System altered.

SQL>show parameter user_dump;

NAMETYPEVALUE

------------------------------------ ----------- ------------------------------

user_dump_deststring/u01/app/oracle/admin/orcl/udu

mp

三、控制文件的管理

规划原则:多路复用,建议存放到不同的磁盘或同一磁盘不同的分区

个数与位置及状态管理:

查看控制文件的状态是否与参数定义中的相吻合,当数据库发生结构修改时,将修改内容同时写入控制文件

备份管理

恢复管理

新建控制文件语句

spfile或pfile都可以实现对控制文件的个数及位置管理

spfile步骤

修改spfile参数中的control_files-- alter system ... scope = spfile | both |memory

一致性关闭数据库

增加或减少控制文件(cpormv)

启动数据库使用spfile

验证结果

pfile步骤

一致性关闭数据库

修改pfile参数(vi或vim)修改*.control_files=......这一段

增加或减少控制文件(cpormv)

启动数据库使用pfile

验证结果

--演示spfile修改控制文件

SQL>show parameter control_file

NAMETYPEVALUE

------------------------------------ ----------- ------------------------------

control_file_record_keep_timeinteger7

control_filesstring/u01/app/oracle/oradata/orcl/c

ontrol01.ctl,/u01/app/oracle/

oradata/orcl/control02.ctl,/u

01/app/oracle/oradata/orcl/con

trol03.ctl

--将控制文件减少到一个

SQL>altersystemsetcontrol_files='/u01/app/oracle/oradata/orcl/control01.ctl'scope=spfile;

System altered.

SQL>shutdownimmediate;

Databaseclosed.

Databasedismounted.

ORACLE instance shut down.

SQL>startup mount

ORACLE instance started.

Total System Global Area251658240 bytes

FixedSize1218796 bytes

VariableSize88082196 bytes

DatabaseBuffers159383552 bytes

Redo Buffers2973696 bytes

Databasemounted.

--再次查看参数文件,已显示为一个

SQL>show parameter control_file

NAMETYPEVALUE

------------------------------------ ----------- ------------------------------

control_file_record_keep_timeinteger7

control_filesstring/u01/app/oracle/oradata/orcl/c

ontrol01.ctl

--增加控制文件(在nomount状态下即可修改)

SQL>altersystemsetcontrol_files='/u01/app/oracle/oradata/orcl/control01.ctl',

2'/u01/app/oracle/oradata/orcl/control02.ctl',

3'/u01/app/oracle/oradata/orcl/control03.ctl'

4scope=spfile;

System altered.

--启动时可以看到在实例阶段出现了版本号不一致的问题

SQL>startup force

ORACLE instance started.

Total System Global Area251658240 bytes

FixedSize1218796 bytes

VariableSize88082196 bytes

DatabaseBuffers159383552 bytes

Redo Buffers2973696 bytes

ORA-00214:controlfile'/u01/app/oracle/oradata/orcl/control01.ctl'version

1051 inconsistentwithfile'/u01/app/oracle/oradata/orcl/control02.ctl'

version 1049

--处理办法,用版本号高的控制文件覆盖版本号低的控制文件

SQL>host cp/u01/app/oracle/oradata/orcl/control01.ctl/u01/app/oracle/oradata/orcl/control02.ctl;

SQL>host cp/u01/app/oracle/oradata/orcl/control01.ctl/u01/app/oracle/oradata/orcl/control03.ctl;

SQL>alterdatabasemount;

Databasealtered.

SQL>alterdatabaseopen;

Databasealtered.

对于控制文件丢失的情况下,通过查看参数文件中设置,使用操作系统命令逐个查看这些文件是否存在

SQL>host ls/u01/app/oracle/oradata/orcl/control01.ctl

/u01/app/oracle/oradata/orcl/control01.ctl

SQL>host ls/u01/app/oracle/oradata/orcl/control02.ctl

/u01/app/oracle/oradata/orcl/control02.ctl

查看控制文件所在的目录可用空间及控制文件的大小,建议不要超过100MB

SQL>host ls/u01/app/oracle/oradata/orcl/c*.ctl-lh

-rw-r----- 1 oracle oinstall 6.8M May 23 10:55 /u01/app/oracle/oradata/orcl/control01.ctl

-rw-r----- 1 oracle oinstall 6.8M May 23 10:55 /u01/app/oracle/oradata/orcl/control02.ctl

-rw-r----- 1 oracle oinstall 6.8M May 23 10:55 /u01/app/oracle/oradata/orcl/control03.ctl

SQL>!df-h

FilesystemSizeUsed AvailUse%Mountedon

/dev/sda26.4G3.9G2.2G64%/

/dev/sdd16.8G3.7G2.8G58%/u01

/dev/sdc21.2G34M1.1G3%/home

/dev/sdc1760M17M704M3%/tmp

/dev/sda1456M18M415M5%/boot

tmpfs450M0450M0%/dev/shm

none450M104K450M1%/var/lib/xenstored

控制文件的备份

热备:

alterdatabasebackupcontrolfileto'<dir>';--热备份控制文件

alterdatabasebackupcontrolfiletotraceas'<dir>';--得到建立控制文件的脚本

RMAN:

backupcurrentcontrolfile;

backupdatabaseincludecurrentcontrolfile;

--或者设置RMAN为自动备份

RMAN>configure controlfile autobackupon;

--演示备份

--用于归档模式下的恢复,直接覆盖到控制文件

SQL>alterdatabasebackupcontrolfileto'/u01/app/oracle/control.bak';

Databasealtered.

--用于重建控制文件

SQL>alterdatabasebackupcontrolfiletotraceas'/u01/app/oracle/recreate_controlfile.txt';

Databasealtered.

--使用RMAN备份

RMAN>connect target/;

connectedtotargetdatabase:ORCL(DBID=1242732291)

RMAN>backupcurrentcontrolfile;--handle为备份文件的路径

Startingbackupat 23-MAY-10

using targetdatabasecontrolfileinsteadofrecoverycatalog

allocated channel:ORA_DISK_1

channel ORA_DISK_1:sid=148 devtype=DISK

channel ORA_DISK_1:startingfulldatafile backupset

channel ORA_DISK_1:specifying datafile(s)inbackupset

includingcurrentcontrolfileinbackupset

channel ORA_DISK_1:starting piece 1 at 23-MAY-10

channel ORA_DISK_1:finished piece 1 at 23-MAY-10

piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/

2010_05_23/o1_mf_ncnnf_TAG20100523T131841_5zkgon2l_.bkp tag=TAG20100523T131841 comment=NONE

channel ORA_DISK_1:backupsetcomplete,elapsedtime:00:00:05

Finishedbackupat 23-MAY-10

RMAN>backupdatabaseincludecurrentcontrolfile;

Startingbackupat 23-MAY-10

using targetdatabasecontrolfileinsteadofrecoverycatalog

allocated channel:ORA_DISK_1

channel ORA_DISK_1:sid=141 devtype=DISK

channel ORA_DISK_1:startingfulldatafile backupset

channel ORA_DISK_1:specifying datafile(s)inbackupset

input datafile fno=00001name=/u01/app/oracle/oradata/orcl/system01.dbf

input datafile fno=00003name=/u01/app/oracle/oradata/orcl/sysaux01.dbf

input datafile fno=00005name=/u01/app/oracle/oradata/orcl/example01.dbf

input datafile fno=00006name=/u01/app/oracle/oradata/orcl/tbs1_1.dbf

input datafile fno=00007name=/u01/app/oracle/oradata/orcl/tbs1_2.dbf

input datafile fno=00002name=/u01/app/oracle/oradata/orcl/undotbs01.dbf

input datafile fno=00004name=/u01/app/oracle/oradata/orcl/users01.dbf

channel ORA_DISK_1:starting piece 1 at 23-MAY-10

channel ORA_DISK_1:finished piece 1 at 23-MAY-10

piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/

2010_05_23/o1_mf_nnndf_TAG20100523T132647_5zkh4sk2_.bkp tag=TAG20100523T132647 comment=NONE

channel ORA_DISK_1:backupsetcomplete,elapsedtime:00:05:25

channel ORA_DISK_1:startingfulldatafile backupset

channel ORA_DISK_1:specifying datafile(s)inbackupset

includingcurrentcontrolfileinbackupset

includingcurrentSPFILEinbackupset

channel ORA_DISK_1:starting piece 1 at 23-MAY-10

channel ORA_DISK_1:finished piece 1 at 23-MAY-10

piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/

2010_05_23/o1_mf_ncsnf_TAG20100523T132647_5zkhh5st_.bkp tag=TAG20100523T132647 comment=NONE

channel ORA_DISK_1:backupsetcomplete,elapsedtime:00:00:14

Finishedbackupat 23-MAY-10

RMAN>showall;

RMAN configuration parameters are:

CONFIGURE RETENTION POLICYTOREDUNDANCY 1;#default

CONFIGUREBACKUPOPTIMIZATIONOFF;#default

CONFIGUREDEFAULTDEVICETYPETODISK;#default

CONFIGURE CONTROLFILE AUTOBACKUPOFF;#default

CONFIGURE CONTROLFILE AUTOBACKUP FORMATFORDEVICETYPEDISKTO'%F';#default

CONFIGURE DEVICETYPEDISKPARALLELISM 1BACKUPTYPETOBACKUPSET;#default

CONFIGURE DATAFILEBACKUPCOPIESFORDEVICETYPEDISKTO1;#default

CONFIGURE ARCHIVELOGBACKUPCOPIESFORDEVICETYPEDISKTO1;#default

CONFIGURE MAXSETSIZETOUNLIMITED;#default

CONFIGURE ENCRYPTIONFORDATABASEOFF;#default

CONFIGURE ENCRYPTION ALGORITHM'AES128';#default

CONFIGURE ARCHIVELOG DELETION POLICYTONONE;#default

CONFIGURE SNAPSHOT CONTROLFILENAMETO'/u01/app/oracle/10g/dbs/snapcf_orcl.f';#default

RMAN>configure controlfile autobackupon;--将控制文件自动备份功能置为on;

new RMAN configuration parameters:

CONFIGURE CONTROLFILE AUTOBACKUPON;

new RMAN configuration parameters are successfully stored

控制文件的恢复管理

控制文件版本不一致的问题

用较新版本的控制文件覆盖旧版本的控制文件

直接修改参数control_file

丢失问题

归档模式下

当归档日志全的时候,先做全备,然后使用备份的控制文件恢复即可

当归档日志不全的时候,先做全备,然后建立新的控制文件即可

非归档模式下

先做全备,然后建立新的控制文件即可

新建控制文件语句

数据库处于mount及open状态

执行alterdatabasebackupcontrolfiletotraceas'<dir>';得到建立语句

注意[no]archievelog [no]resetlogs两个参数的区别

版本不一致演示

SQL>startup

ORACLE instance started.

Total System Global Area251658240 bytes

FixedSize1218796 bytes

VariableSize88082196 bytes

DatabaseBuffers159383552 bytes

Redo Buffers2973696 bytes

ORA-00214:controlfile'/u01/app/oracle/oradata/orcl/control01.ctl'version

1051 inconsistentwithfile'/u01/app/oracle/oradata/orcl/control02.ctl'

version 1049

--处理办法,用版本号高的控制文件覆盖版本号低的控制文件

SQL>host cp/u01/app/oracle/oradata/orcl/control01.ctl/u01/app/oracle/oradata/orcl/control02.ctl;

SQL>host cp/u01/app/oracle/oradata/orcl/control01.ctl/u01/app/oracle/oradata/orcl/control03.ctl;

SQL>alterdatabasemount;

Databasealtered.

SQL>alterdatabaseopen;

Databasealtered.

--控制文件部分丢失的演示,原本有两个控制文件,丢失一个

--处理办法:

1.将存在的控制文件复制到目的路径并更改控制文件名字为正确的控制文件名称

2.修改控控文件参数将丢失的控制文件去掉(一般不建议使用)

SQL>altersystemsetcontrol_files=' /u01/app/oracle/oradata/orcl/c ontrol01.ctl',

2'/u01/app/oracle/oradata/orcl/control02.ctl'scope=spfile;

System altered.

SQL>shutdownimmediate;

Databaseclosed.

Databasedismounted.

ORACLE instance shut down.

SQL>startup

ORACLE instance started.

Total System Global Area251658240 bytes

FixedSize1218796 bytes

VariableSize75499284 bytes

DatabaseBuffers171966464 bytes

Redo Buffers2973696 bytes

ORA-00205:errorinidentifying controlfile,checkalertlogformore info

[oracle@robinson ~]$ tail-n 100/u01/app/oracle/admin/orcl/bdump/alert_orcl.log

..........................

Tue Jun8 19:03:42 2010

starting up 1 sharedserver(s)...

MMON startedwithpid=11,OS id=4557

CJQ0 startedwithpid=10,OS id=4555

Tue Jun8 19:03:44 2010

ALTERDATABASEMOUNT

Tue Jun8 19:03:44 2010

ORA-00202:controlfile:'/u01/app/oracle/10g/dbs/ /u01/app/oracle/oradata/orcl/c ontrol01.ctl'

ORA-27037:unabletoobtainfilestatus

Linux Error:2:Nosuchfileordirectory

Additional information:3

Tue Jun8 19:03:47 2010

ORA-205 signalled during:ALTERDATABASEMOUNT...

--从警告日志中得知,文件名为c ontrol01.ctl的文件不存在,故将其改为正确的文件名

SQL>altersystemsetcontrol_files='/u01/app/oracle/oradata/orcl/control01.ctl',

2'/u01/app/oracle/oradata/orcl/control02.ctl'scope=spfile;

System altered.

SQL>startup force

ORACLE instance started.

Total System Global Area251658240 bytes

FixedSize1218796 bytes

VariableSize83887892 bytes

DatabaseBuffers163577856 bytes

Redo Buffers2973696 bytes

Databasemounted.

Databaseopened.

非归档模式下,当所有的控制文件都丢失,只能重建控制文件来解决

Total System Global Area251658240 bytes

FixedSize1218796 bytes

VariableSize79693588 bytes

DatabaseBuffers167772160 bytes

Redo Buffers2973696 bytes

ORA-00205:errorinidentifying controlfile,checkalertlogformore info

[oracle@robinson ~]$tail-n 100/u01/app/oracle/admin/orcl/bdump/alert_orcl.log

ALTERDATABASEMOUNT

Thu Jul 15 12:13:15 2010

ORA-00202:controlfile:'/u01/app/oracle/oradata/orcl/control01.ctl'

ORA-27037:unabletoobtainfilestatus

Linux Error:2:Nosuchfileordirectory

--重建控制文件主要有三个需要考虑的是

--搞清各个日志文件的大小及位置

--搞清各个数据文件的位置

--设置正确的字符集

SQL>CREATECONTROLFILE REUSEDATABASE"orcl" NOARCHIVELOG NORESTLOGS

2MAXLOGFILES 16

3MAXLOGMEMBER 3

4MAXDATAFILES 20

5MAXINSTANCES 8

6MAXLOGHISTORY 3

7LOGFILE

8GROUP1(

9'u01/app/oracle/oradata/orcl/redo1.log',

10'/u01/app/oracle/oradata/orcl/redo01.log'

11)SIZE50M,

12GROUP2(

13'/u01/app/oracle/oradata/orcl/redo2.log',

14'/u01/app/oracle/oradata/orcl/redo02.log'

15)SIZE50M,

16GROUP3(

17'/u01/app/oracle/oradata/orcl/redo3.log',

18'/u01/app/oracle/oradata/orcl/redo03.log'

19)SIZE100M

20DATAFILE

21'/u01/app/oracle/oradata/orcl/tbs1_2.dbf',

22'/u01/app/oracle/oradata/orcl/tbs1_1.dbf',

23'/u01/app/oracle/oradata/orcl/example01.dbf',

24'/u01/app/oracle/oradata/orcl/users01.dbf',

25'/u01/app/oracle/oradata/orcl/sysaux01.dbf',

26'/u01/app/oracle/oradata/orcl/system01.dbf',

27'/u01/app/oracle/oradata/orcl/undotbs01.dbf'

28CHARACTERSETWE8ISO8859P1;

CREATECONTROLFILE REUSEDATABASE"orcl" NOARCHIVELOG NORESTLOGS

*

ERROR at line 1:

ORA-01967:invalidoptionforCREATECONTROLFILE

SQL>save/u01/app/oracle/oradata/rectl.sql;

Createdfile/u01/app/oracle/oradata/rectl.sql

SQL>ho vim/u01/app/oracle/oradata/rectl.sql

SQL>@/u01/app/oracle/oradata/rectl.sql

Controlfilecreated.

--下面给出正确执行后的语句内容

SQL>host cat/u01/app/oracle/oradata/rectl.sql

CREATECONTROLFILE REUSEDATABASE"orcl" NOARCHIVELOG NORESETLOGS

MAXLOGFILES 16

MAXLOGMEMBERS 3

MAXDATAFILES 20

MAXINSTANCES 8

MAXLOGHISTORY 3

LOGFILE

GROUP1(

'/u01/app/oracle/oradata/orcl/redo01.log'

)SIZE50M,

GROUP2(

'/u01/app/oracle/oradata/orcl/redo2.log',

'/u01/app/oracle/oradata/orcl/redo02.log'

)SIZE50M,

GROUP3(

'/u01/app/oracle/oradata/orcl/redo3.log',

'/u01/app/oracle/oradata/orcl/redo03.log'

)SIZE100M

DATAFILE

'/u01/app/oracle/oradata/orcl/tbs1_2.dbf',

'/u01/app/oracle/oradata/orcl/tbs1_1.dbf',

'/u01/app/oracle/oradata/orcl/example01.dbf',

'/u01/app/oracle/oradata/orcl/users01.dbf',

'/u01/app/oracle/oradata/orcl/sysaux01.dbf',

'/u01/app/oracle/oradata/orcl/system01.dbf',

'/u01/app/oracle/oradata/orcl/undotbs01.dbf'

CHARACTERSETWE8ISO8859P1

/

--将数据库切换到open状态

SQL>alterdatabaseopen;

Databasealtered.

SQL>select*fromdual;

D

-

X

--查看已经新产生了控制文件

SQL>host ls $ORACLE_BASE/oradata/orcl/control*-l--可以看到新增了控制文件control01.ctl和control02.ctl

-rw-r----- 1 oracle oinstall 6012928 Jul 15 12:58 /u01/app/oracle/oradata/orcl/control01.ctl

-rw------- 1 oracle oinstall 7389184 Jul 15 12:04 /u01/app/oracle/oradata/orcl/control01.ctl.bak

-rw-r----- 1 oracle oinstall 6012928 Jul 15 12:58 /u01/app/oracle/oradata/orcl/control02.ctl

归档模式下控制文件全部丢失的处理

--首先将数据库切换到归档模式

SQL>shutdownimmediate;

Databaseclosed.

Databasedismounted.

ORACLE instance shut down.

SQL>startup mount;

ORACLE instance started.

Total System Global Area251658240 bytes

FixedSize1218796 bytes

VariableSize79693588 bytes

DatabaseBuffers167772160 bytes

Redo Buffers2973696 bytes

Databasemounted.

SQL>alterdatabasearchivelog;

Databasealtered.

SQL>alterdatabaseopen;

Databasealtered.

--查看归档的状态

SQL>archiveloglist;

DatabaselogmodeArchive Mode

Automatic archivalEnabled

Archive destinationUSE_DB_RECOVERY_FILE_DEST

Oldest onlinelogsequence1

Nextlogsequencetoarchive3

Currentlogsequence3

--备份控制文件

SQL>alterdatabasebackupcontrolfileto'/u01/app/oracle/oradata/orcl/rectl.bak';

Databasealtered.

SQL>createtabletb_temp(idint,col1 varchar2(20));

Tablecreated.

SQL>insertintotb_tempselect1,'Robinson'fromdual;

1 row created.

SQL>commit;

Commitcomplete.

SQL>altersystem switch logfile;--切换日志

System altered.

SQL>archiveloglist;--日志切换后sequence由变成

DatabaselogmodeArchive Mode

Automatic archivalEnabled

Archive destinationUSE_DB_RECOVERY_FILE_DEST

Oldest onlinelogsequence2

Nextlogsequencetoarchive4

Currentlogsequence4

--模拟控制文件全部丢失

SQL>startup

ORACLE instance started.

Total System Global Area251658240 bytes

FixedSize1218796 bytes

VariableSize79693588 bytes

DatabaseBuffers167772160 bytes

Redo Buffers2973696 bytes

ORA-00205:errorinidentifying controlfile,checkalertlogformore info

SQL>show parameter control

NAMETYPEVALUE

------------------------------------ ----------- ------------------------------

control_file_record_keep_timeinteger7

control_filesstring/u01/app/oracle/oradata/orcl/c

ontrol01.ctl,/u01/app/oracle/

oradata/orcl/control02.ctl

--查看物理控制文件是否存在

SQL>ho ls/u01/app/oracle/oradata/orcl/contr*

ls:/u01/app/oracle/oradata/orcl/contr*:Nosuchfileordirectory

--建议先对数据库作备份再将备份的控制文件复制到参数文件中指定的位置

SQL>ho cp/u01/app/oracle/oradata/orcl/rectl.bak/u01/app/oracle/oradata/orcl/control01.ctl

SQL>ho cp/u01/app/oracle/oradata/orcl/rectl.bak/u01/app/oracle/oradata/orcl/control02.ctl

SQL>alterdatabasemount;

Databasealtered.

SQL>alterdatabaseopen;

alterdatabaseopen

*

ERROR at line 1:

ORA-01589:mustuseRESETLOGSorNORESETLOGSoptionfordatabaseopen

SQL>shutdownimmediate;

ORA-01109:databasenotopen

Databasedismounted.

ORACLE instance shut down.

SQL>startup mount;

ORACLE instance started.

Total System Global Area251658240 bytes

FixedSize1218796 bytes

VariableSize79693588 bytes

DatabaseBuffers167772160 bytes

Redo Buffers2973696 bytes

Databasemounted.

SQL>alterdatabaseopenresetlogs;

Databasealtered.

SQL>select*fromdual;

D

-

X

SQL>select*fromtb_temp;--表成功被恢复

ID COL1

---------- --------------------

1 Robinson

SQL>droptabletb_temp purge;

Tabledropped.

SQL>archiveloglist;--日志的sequence号被置为

DatabaselogmodeArchive Mode

Automatic archivalEnabled

Archive destinationUSE_DB_RECOVERY_FILE_DEST

Oldest onlinelogsequence1

Nextlogsequencetoarchive1

Currentlogsequence1


你可能感兴趣的:(oracle)