数据库的启动和正常运行都离不开控制文件(数据库在 mount 阶段读取控制文件,open 阶段一直使用),一定要备份控制文件,控制文件损坏将导致整个数据库损坏,数据库正常工作至少需要一个控制文件,生产库至少需要两个控制文件(多个控制文件之间是镜像关系),控制文件的位置和数量由初始化参数(control_files)决定。启动数据库时,Oracle 从初始化参数文件中获取控制文件的名字及位置,并打开控制文件,然后从控制文件中读取数据文件和重做日志文件的信息,最后打开数据库。数据库运行时,会更改控制文件。
1.2 控制文件的内容包括
(1)数据库的名称、ID、创建的时间戳;
(2)表空间的名称;
(3)联机日志文件、数据文件的位置、名称;
(4)联机日志的 Sequence 号码;
(5)检查点的信息;
(6)撤销段的开始或结束;
(7)归档信息;
(8)备份信息。
2、查看控制文件的个数、名称和位置
2.1 使用 show parameter 命令
SQL> show parameter control_filesNAME TYPE VALUE------------------------------------ ----------- ------------------------------control_files string /app/oracle/oradata/ORCL/control01.ctl, /app/oracle/oradata/ORCL/control02.ctl
2.2 查看 v$controlfile 视图
SQL> desc v$controlfileName Type Nullable Default Comments --------------------- ------------- -------- ------- -------- STATUS VARCHAR2(7) Y NAME VARCHAR2(513) Y IS_RECOVERY_DEST_FILE VARCHAR2(3) Y BLOCK_SIZE NUMBER Y FILE_SIZE_BLKS NUMBER Y CON_ID NUMBER Y SQL> select * from v$controlfile;STATUS NAME IS_RECOVERY_DEST_FILE BLOCK_SIZE FILE_SIZE_BLKS CON_ID------- -------------------------------------------------------------------------------- --------------------- ---------- -------------- ----------/app/oracle/oradata/ORCL/control01.ctl NO 16384 646 0/app/oracle/oradata/ORCL/control02.ctl NO 16384 646 0
3、查看控制文件的内容
3.1 将控制文件转出为文本文件
SQL> alter database backup controlfile to trace as '/home/oracle/ctl001.txt';Database altered.
3.2 查看文件的内容
[oracle@rac1 ~]$ cat ctl001.txt-- 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=orcl_%d_%S_%t_%r.dbf---- DB_UNIQUE_NAME="orcl"---- LOG_ARCHIVE_CONFIG='SEND, RECEIVE, NODG_CONFIG'-- LOG_ARCHIVE_MAX_PROCESSES=4-- STANDBY_FILE_MANAGEMENT=MANUAL-- FAL_CLIENT=''-- FAL_SERVER=''---- LOG_ARCHIVE_DEST_1='LOCATION=/app/archive'-- LOG_ARCHIVE_DEST_1='OPTIONAL REOPEN=300 NODELAY'-- LOG_ARCHIVE_DEST_1='ARCH NOAFFIRM NOVERIFY SYNC'-- LOG_ARCHIVE_DEST_1='REGISTER'-- LOG_ARCHIVE_DEST_1='NOALTERNATE'-- LOG_ARCHIVE_DEST_1='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 PERFORMANCESTARTUP NOMOUNTCREATE CONTROLFILE REUSE DATABASE "ORCL" NORESETLOGS ARCHIVELOGMAXLOGFILES 16MAXLOGMEMBERS 3 MAXDATAFILES 100MAXINSTANCES 8MAXLOGHISTORY 292LOGFILEGROUP 1 '/app/oracle/oradata/ORCL/redo01.log' SIZE 200M BLOCKSIZE 512,GROUP 2 '/app/oracle/oradata/ORCL/redo02.log' SIZE 200M BLOCKSIZE 512,GROUP 3 '/app/oracle/oradata/ORCL/redo03.log' SIZE 200M BLOCKSIZE 512-- STANDBY LOGFILEDATAFILE'/app/oracle/oradata/ORCL/system01.dbf','/app/oracle/oradata/ORCL/test01.DBF','/app/oracle/oradata/ORCL/sysaux01.dbf','/app/oracle/oradata/ORCL/undotbs01.dbf','/app/oracle/oradata/ORCL/wms01.DBF','/app/oracle/oradata/ORCL/users01.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 '/app/archive/orcl_62725153_0000000001_1_1005785759.dbf';-- ALTER DATABASE REGISTER LOGFILE '/app/archive/orcl_62725153_0000000001_1_1123878365.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 TEMP ADD TEMPFILE '/app/oracle/oradata/ORCL/temp01.dbf'SIZE 33554432 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 PERFORMANCESTARTUP NOMOUNTCREATE CONTROLFILE REUSE DATABASE "ORCL" RESETLOGS ARCHIVELOGMAXLOGFILES 16MAXLOGMEMBERS 3 MAXDATAFILES 100MAXINSTANCES 8MAXLOGHISTORY 292LOGFILEGROUP 1 '/app/oracle/oradata/ORCL/redo01.log' SIZE 200M BLOCKSIZE 512,GROUP 2 '/app/oracle/oradata/ORCL/redo02.log' SIZE 200M BLOCKSIZE 512,GROUP 3 '/app/oracle/oradata/ORCL/redo03.log' SIZE 200M BLOCKSIZE 512-- STANDBY LOGFILEDATAFILE'/app/oracle/oradata/ORCL/system01.dbf','/app/oracle/oradata/ORCL/test01.DBF','/app/oracle/oradata/ORCL/sysaux01.dbf','/app/oracle/oradata/ORCL/undotbs01.dbf','/app/oracle/oradata/ORCL/wms01.DBF','/app/oracle/oradata/ORCL/users01.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 '/app/archive/orcl_62725153_0000000001_1_1005785759.dbf';-- ALTER DATABASE REGISTER LOGFILE '/app/archive/orcl_62725153_0000000001_1_1123878365.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 TEMP ADD TEMPFILE '/app/oracle/oradata/ORCL/temp01.dbf'SIZE 33554432 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;-- End of tempfile additions.--
SQL> startup force;ORACLE instance started.Total System Global Area 8002730448 bytesFixed Size 8915408 bytesVariable Size 1308622848 bytesDatabase Buffers 6677331968 bytesRedo Buffers 7860224 bytesORA-00205: error in identifying control file, check alert log for more info
5.2.4 查看 alert 文件信息
SQL> SELECT * FROM v$diag_info; INST_ID NAME VALUE CON_ID---------- ---------------------------------------------------------------- -------------------------------------------------------------------------------- ----------1 Diag Enabled TRUE 01 ADR Base /app/oracle 01 ADR Home /app/oracle/diag/rdbms/orcl/orcl 01 Diag Trace /app/oracle/diag/rdbms/orcl/orcl/trace 01 Diag Alert /app/oracle/diag/rdbms/orcl/orcl/alert 01 Diag Incident /app/oracle/diag/rdbms/orcl/orcl/incident 01 Diag Cdump /app/oracle/diag/rdbms/orcl/orcl/cdump 01 Health Monitor /app/oracle/diag/rdbms/orcl/orcl/hm 01 Default Trace File /app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_2861.trc 01 Active Problem Count 4 01 Active Incident Count 75 01 ORACLE_HOME /app/oracle/product/19.3.0/db 012 rows selected[oracle@node1 trace]$ cd /app/oracle/diag/rdbms/orcl/orcl/trace[oracle@node1 trace]$ tail 300 alert_orcl.log tail: 无法打开"300" 读取数据: 没有那个文件或目录==> alert_orcl.log ORA-205 signalled during: ALTER DATABASE MOUNT...2023-01-12T14:21:46.156054+08:00Errors in file /app/oracle/diag/rdbms/orcl/orcl/trace/orcl_mz00_2581.trc:ORA-00202: ????: ''/app/oracle/controlfile/contrl03.ctl''ORA-27037: ????????Linux-x86_64 Error: 2: No such file or directoryAdditional information: 7Checker run found 1 new persistent data failures2023-01-12T14:21:48.271581+08:00Using default pga_aggregate_limit of 5088 MB
5.3 控制文件丢失处理
5.3.1 将已经存在的控制文件复制到目的路径并更改为正确的控制文件名称
SQL> host cp /app/oracle/oradata/ORCL/control02.ctl /app/oracle/controlfile/contrl03.ctlSQL> alter database mount;Database altered.SQL> alter database open;Database altered.
5.3.2 修改 control_files参数,将丢失的控制文件去掉
SQL> show parameter control_file;NAME TYPE VALUE------------------------------------ ----------- ------------------------------control_file_record_keep_time integer 7control_files string /app/oracle/oradata/ORCL/control01.ctl, /app/oracle/oradata/ORCL/control02.ctl, /app/oracle/controlfile/contrl03.ctlSQL> alter system set control_files = '/app/oracle/oradata/ORCL/control01.ctl','/app/oracle/oradata/ORCL/control02.ctl' scope = spfile sid = '*';System altered.SQL> startup force nomountORACLE instance started.Total System Global Area 8002730448 bytesFixed Size 8915408 bytesVariable Size 1308622848 bytesDatabase Buffers 6677331968 bytesRedo Buffers 7860224 bytesSQL> alter database mount;Database altered.SQL> alter database open;Database altered.
6、控制文件版本不一致
6.1 模拟控制文件版本不一致问题:
6.1.1 查看数据库控制文件信息
SQL> show parameter control_fileNAME TYPE VALUE------------------------------------ ----------- ------------------------------control_file_record_keep_time integer 7control_files string /app/oracle/oradata/ORCL/control01.ctl, /app/oracle/oradata/ORCL/control02.ctl
6.1.2 修改参数文件,只保留第一个控制文件
SQL> alter system set control_files = '/app/oracle/oradata/ORCL/control01.ctl','/app/oracle/oradata/ORCL/control02.ctl' scope = spfile sid = '*';System altered.SQL> alter system set control_files = '/app/oracle/oradata/ORCL/control01.ctl' scope=spfile sid = '*';System altered.SQL> startup force ORACLE instance started.Total System Global Area 8002730448 bytesFixed Size 8915408 bytesVariable Size 1308622848 bytesDatabase Buffers 6677331968 bytesRedo Buffers 7860224 bytesDatabase mounted.Database opened.
6.1.3 创建一张表
SQL> create table hr.STUDENT_INFO_01 as select * from hr.STUDENT_INFO;Table created.
6.2 修改参数文件,添加两个控制文件
SQL> alter system set control_files = '/app/oracle/oradata/ORCL/control01.ctl','/app/oracle/oradata/ORCL/control02.ctl' scope=spfile sid = '*';System altered.
6.3 重启数据库,提示版本错误
SQL> startup forceORACLE instance started.Total System Global Area 8002730448 bytesFixed Size 8915408 bytesVariable Size 1308622848 bytesDatabase Buffers 6677331968 bytesRedo Buffers 7860224 bytesORA-00214: control file '/app/oracle/oradata/ORCL/control01.ctl' version 36188inconsistent with file '/app/oracle/oradata/ORCL/control02.ctl' version 36173
6.4 解决方法:利用最新版本的控制文件替换旧的控制文件
SQL> host cp /app/oracle/oradata/ORCL/control01.ctl /app/oracle/oradata/ORCL/control02.ctlSQL> startup forceORACLE instance started.Total System Global Area 8002730448 bytesFixed Size 8915408 bytesVariable Size 1308622848 bytesDatabase Buffers 6677331968 bytesRedo Buffers 7860224 bytesDatabase mounted.Database opened.
7、控制文件的备份
7.1 使用 alter database backup controlfile 备份
7.1.1 语法
alter database backup controlfile to '';
7.1.2 备份控制文件
SQL> alter database backup controlfile to '/home/oracle/controlfile.bak';Database altered.
SQL> startupORACLE instance started.Total System Global Area 8002730448 bytesFixed Size 8915408 bytesVariable Size 1308622848 bytesDatabase Buffers 6677331968 bytesRedo Buffers 7860224 bytesORA-00205: error in identifying control file, check alert log for more info
7.2.6 数据库启动到 nomount 状态
SQL> select status from v$instance;STATUSSTARTED
7.2.7 使用 RMAN 还原控制文件
RMAN> restore controlfile from '/app/rmanbak/control_2023_01_12.bak';Starting restore at 12-JAN-23using target database control file instead of recovery catalogallocated channel: ORA_DISK_1channel ORA_DISK_1: SID=1709 device type=DISKchannel ORA_DISK_1: restoring control filechannel ORA_DISK_1: restore complete, elapsed time: 00:00:01output file name=/app/oracle/oradata/ORCL/control01.ctloutput file name=/app/oracle/oradata/ORCL/control02.ctlFinished restore at 12-JAN-23
7.2.8 启动数据库到 mount 状态
RMAN> alter database mount;released channel: ORA_DISK_1Statement processed
7.2.9 恢复数据库
RMAN> recover database;Starting recover at 12-JAN-23allocated channel: ORA_DISK_1channel ORA_DISK_1: SID=1709 device type=DISKstarting media recoveryarchived log for thread 1 with sequence 51 is already on disk as file /app/oracle/oradata/ORCL/redo03.logarchived log file name=/app/oracle/oradata/ORCL/redo03.log thread=1 sequence=51media recovery complete, elapsed time: 00:00:01Finished recover at 12-JAN-23
7.2.10 打开数据库
以 resetlogs 模式才能打开数据库。
RMAN> alter database open resetlogs;Statement processed
7.3 生成创建控制文件的脚本
7.3.1 语法
alter database backup controlfile to trace as ''
7.3.2 生成创建控制文件的脚本
SQL> alter database backup controlfile to trace as '/home/oracle/ctl002.txt' ;Database altered.
7.3.3 查看文件 /home/oracle/ctl002.txt 的内容:
创建控制文件主要包含三部分内容:
(1)设置日志文件的大小及位置;
(2)设置数据文件的位置;
(3)设置正确的字符集。
[oracle@node1 ~]$ cat ctl002.txt -- 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=orcl_%d_%S_%t_%r.dbf---- DB_UNIQUE_NAME="orcl"---- LOG_ARCHIVE_CONFIG='SEND, RECEIVE, NODG_CONFIG'-- LOG_ARCHIVE_MAX_PROCESSES=4-- STANDBY_FILE_MANAGEMENT=MANUAL-- FAL_CLIENT=''-- FAL_SERVER=''---- LOG_ARCHIVE_DEST_1='LOCATION=/app/archive'-- LOG_ARCHIVE_DEST_1='OPTIONAL REOPEN=300 NODELAY'-- LOG_ARCHIVE_DEST_1='ARCH NOAFFIRM NOVERIFY SYNC'-- LOG_ARCHIVE_DEST_1='REGISTER'-- LOG_ARCHIVE_DEST_1='NOALTERNATE'-- LOG_ARCHIVE_DEST_1='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 PERFORMANCESTARTUP NOMOUNTCREATE CONTROLFILE REUSE DATABASE "ORCL" NORESETLOGS ARCHIVELOGMAXLOGFILES 16MAXLOGMEMBERS 3 MAXDATAFILES 100MAXINSTANCES 8MAXLOGHISTORY 292LOGFILEGROUP 1 '/app/oracle/oradata/ORCL/redo01.log' SIZE 200M BLOCKSIZE 512,GROUP 2 '/app/oracle/oradata/ORCL/redo02.log' SIZE 200M BLOCKSIZE 512,GROUP 3 '/app/oracle/oradata/ORCL/redo03.log' SIZE 200M BLOCKSIZE 512-- STANDBY LOGFILEDATAFILE'/app/oracle/oradata/ORCL/system01.dbf','/app/oracle/oradata/ORCL/test01.DBF','/app/oracle/oradata/ORCL/sysaux01.dbf','/app/oracle/oradata/ORCL/undotbs01.dbf','/app/oracle/oradata/ORCL/wms01.DBF','/app/oracle/oradata/ORCL/users01.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 '/app/archive/orcl_62725153_0000000001_1_1005785759.dbf';-- ALTER DATABASE REGISTER LOGFILE '/app/archive/orcl_62725153_0000000001_1_1123878365.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 TEMP ADD TEMPFILE '/app/oracle/oradata/ORCL/temp01.dbf'SIZE 33554432 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 PERFORMANCESTARTUP NOMOUNTCREATE CONTROLFILE REUSE DATABASE "ORCL" RESETLOGS ARCHIVELOGMAXLOGFILES 16MAXLOGMEMBERS 3 MAXDATAFILES 100MAXINSTANCES 8MAXLOGHISTORY 292LOGFILEGROUP 1 '/app/oracle/oradata/ORCL/redo01.log' SIZE 200M BLOCKSIZE 512,GROUP 2 '/app/oracle/oradata/ORCL/redo02.log' SIZE 200M BLOCKSIZE 512,GROUP 3 '/app/oracle/oradata/ORCL/redo03.log' SIZE 200M BLOCKSIZE 512-- STANDBY LOGFILEDATAFILE'/app/oracle/oradata/ORCL/system01.dbf','/app/oracle/oradata/ORCL/test01.DBF','/app/oracle/oradata/ORCL/sysaux01.dbf','/app/oracle/oradata/ORCL/undotbs01.dbf','/app/oracle/oradata/ORCL/wms01.DBF','/app/oracle/oradata/ORCL/users01.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 '/app/archive/orcl_62725153_0000000001_1_1005785759.dbf';-- ALTER DATABASE REGISTER LOGFILE '/app/archive/orcl_62725153_0000000001_1_1123878365.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 TEMP ADD TEMPFILE '/app/oracle/oradata/ORCL/temp01.dbf'SIZE 33554432 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;-- End of tempfile additions.--