oracle

关注公众号 jb51net

关闭
首页 > 数据库 > oracle > Oracle 控制文件

Oracle的控制文件管理

作者:multis

Oracle 的控制文件记录了当前数据库的结构信息,包含数据文件及日志文件的信息以及相关的状态、归档信息等,本文主要介绍了Oracle的控制文件管理,感兴趣的可以了解一下

1、控制文件概述

1.1 概念

Oracle 的控制文件记录了当前数据库的结构信息,包含数据文件及日志文件的信息以及相关的状态、归档信息等。控制文件是一个二进制文件,一个控制文件只属于一个数据库。当数据库的物理结构发生改变时,Oracle会自动更新控制文件。当增加、重命名、删除一个数据文件或者一个重做日志文件时,Oracle 服务器进程会立即更新控制文件以反映数据库结构的变化。用户不能手工编辑控制文件,控制文件的修改由 Oracle 自动完成。

数据库的启动和正常运行都离不开控制文件(数据库在 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_files
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_files                        string      /app/oracle/oradata/ORCL/control01.ctl, /app/oracle/oradata/ORCL/control02.ctl

2.2 查看 v$controlfile 视图

SQL>  desc v$controlfile
Name                  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 PERFORMANCE
STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "ORCL" NORESETLOGS  ARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 292
LOGFILE
  GROUP 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 LOGFILE
DATAFILE
  '/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 PERFORMANCE
STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "ORCL" RESETLOGS  ARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 292
LOGFILE
  GROUP 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 LOGFILE
DATAFILE
  '/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.
--

注:第一种日志可用 第二种日志不可用

3.3 使用 strings 命令直接查看控制文件

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
SQL> host cp /app/oracle/oradata/ORCL/control01.ctl /home/oracle/control01.ctl.bak
SQL> host ls -l
总用量 10360
-rw-r----- 1 oracle oinstall 10600448 1月  12 13:40 control01.ctl.bak
-rw-r--r-- 1 oracle oinstall     5849 1月  11 16:09 ctl001.txt
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 公共
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 模板
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 视频
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 图片
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 文档
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 下载
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 音乐
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 桌面
[oracle@node1 ~]$ strings control01.ctl.bak

3.4 查看控制文件包含的记录片段

SQL> select type,record_size,records_total,records_used from v$controlfile_record_section;

4、控制文件的多路复用

数据库的启动和正常运行都离不开控制文件,控制文件损坏将导致整个数据库损坏,数据库正常工作至少需要一个控制文件,由于控制文件极其重要,生产库最少创建控制文件的两个以上副本,可以通过多路复用技术,将控制文件的副本创建到不同的磁盘上。这样,如果一个控制文件损坏了,可以自动使用另一个控制文件。

但控制文件并不是越多越好,因为当 Oracle 更新控制文件时,会将所有的控制文件全部进行更新,对数据库的性能会有一定的影响,读取时则仅读取第一个控制文件。

控制文件的位置和数量由初始化参数(control_files)决定。启动数据库时,Oracle 从初始化参数文件中获取控制文件的名字及位置,并打开控制文件,然后从控制文件中读取数据文件和重做日志文件的信息,最后打开数据库。数据库运行时,会更改控制文件。

增加控制文件的步骤如下:

4.1 查看当前使用的控制文件

SQL> show parameter control_file
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_file_record_keep_time        integer     7
control_files                        string      /app/oracle/oradata/ORCL/control01.ctl, /app/oracle/oradata/ORCL/control02.ctl

4.2 修改参数文件

[oracle@node1 controlfile]$ mkdir /app/oracle/controlfile
SQL> alter system set control_files = '/app/oracle/oradata/ORCL/control01.ctl','/app/oracle/oradata/ORCL/control02.ctl','/app/oracle/controlfile/contrl03.ctl' scope = spfile sid = '*';
System altered

4.3 重启数据库到 nomount 状态,复制所需的控制文件

SQL> startup force nomount;
ORACLE instance started.
Total System Global Area 8002730448 bytes
Fixed Size    8915408 bytes
Variable Size 1308622848 bytes
Database Buffers 6677331968 bytes
Redo Buffers    7860224 bytes
SQL> show parameter control_file;
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_file_record_keep_time        integer     7
control_files                        string      /app/oracle/oradata/ORCL/control01.ctl, /app/oracle/oradata/ORCL/control02.ctl, /app/oracle/controlfile/contrl03.ctl
SQL> host cp /app/oracle/oradata/ORCL/control02.ctl /app/oracle/controlfile/contrl03.ctl
SQL> alter database mount;
Database altered.
SQL> alter database open;
Database altered.

5、控制文件丢失的解决办法

5.1 控制文件部分丢失

一个或多个控制文件丢失,至少有一个控制文件存在。比如,原来有 3 个控制文件,丢失一个导致数据库无法启动。解决方法如下:

(1)将已经存在的控制文件复制到目的路径并更改为正确的控制文件名称;

(2)修改 control_files 参数将丢失的控制文件去掉。

5.2 模拟控制文件丢失

5.2.1 查看控制文件信息

SQL> show parameter control_file;
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_file_record_keep_time        integer     7
control_files                        string      /app/oracle/oradata/ORCL/control01.ctl, /app/oracle/oradata/ORCL/control02.ctl, /app/oracle/controlfile/contrl03.ctl

5.2.2 删除控制文件

SQL> host rm -rf /app/oracle/controlfile/contrl03.ctl

5.2.3 重启数据库

SQL> startup force;
ORACLE instance started.
Total System Global Area 8002730448 bytes
Fixed Size    8915408 bytes
Variable Size 1308622848 bytes
Database Buffers 6677331968 bytes
Redo Buffers    7860224 bytes
ORA-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                                                                                      0
         1 ADR Base                                                         /app/oracle                                                                               0
         1 ADR Home                                                         /app/oracle/diag/rdbms/orcl/orcl                                                          0
         1 Diag Trace                                                       /app/oracle/diag/rdbms/orcl/orcl/trace                                                    0
         1 Diag Alert                                                       /app/oracle/diag/rdbms/orcl/orcl/alert                                                    0
         1 Diag Incident                                                    /app/oracle/diag/rdbms/orcl/orcl/incident                                                 0
         1 Diag Cdump                                                       /app/oracle/diag/rdbms/orcl/orcl/cdump                                                    0
         1 Health Monitor                                                   /app/oracle/diag/rdbms/orcl/orcl/hm                                                       0
         1 Default Trace File                                               /app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_2861.trc                                  0
         1 Active Problem Count                                             4                                                                                         0
         1 Active Incident Count                                            75                                                                                        0
         1 ORACLE_HOME                                                      /app/oracle/product/19.3.0/db                                                             0
12 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:00
Errors 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 directory
Additional information: 7
Checker run found 1 new persistent data failures
2023-01-12T14:21:48.271581+08:00
Using 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.ctl
SQL> 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     7
control_files                        string      /app/oracle/oradata/ORCL/control01.ctl, /app/oracle/oradata/ORCL/control02.ctl, /app/oracle/controlfile/contrl03.ctl
SQL> 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 nomount
ORACLE instance started.
Total System Global Area 8002730448 bytes
Fixed Size    8915408 bytes
Variable Size 1308622848 bytes
Database Buffers 6677331968 bytes
Redo Buffers    7860224 bytes
SQL> alter database mount;
Database altered.
SQL> alter database open;
Database altered.

6、控制文件版本不一致

6.1 模拟控制文件版本不一致问题:

6.1.1 查看数据库控制文件信息

SQL> show parameter control_file
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_file_record_keep_time        integer     7
control_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 bytes
Fixed Size    8915408 bytes
Variable Size 1308622848 bytes
Database Buffers 6677331968 bytes
Redo Buffers    7860224 bytes
Database 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 force
ORACLE instance started.
Total System Global Area 8002730448 bytes
Fixed Size    8915408 bytes
Variable Size 1308622848 bytes
Database Buffers 6677331968 bytes
Redo Buffers    7860224 bytes
ORA-00214: control file '/app/oracle/oradata/ORCL/control01.ctl' version 36188
inconsistent 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.ctl
SQL> startup force
ORACLE instance started.
Total System Global Area 8002730448 bytes
Fixed Size    8915408 bytes
Variable Size 1308622848 bytes
Database Buffers 6677331968 bytes
Redo Buffers    7860224 bytes
Database mounted.
Database opened.

7、控制文件的备份

7.1 使用 alter database backup controlfile 备份

7.1.1 语法

alter database backup controlfile to '<dir>';

7.1.2 备份控制文件

SQL> alter database backup controlfile to '/home/oracle/controlfile.bak';
Database altered.

7.1.3 查看控制文件的备份

SQL> host ls -l /home/oracle
总用量 20712
-rw-r----- 1 oracle oinstall 10600448 1月  12 13:40 control01.ctl.bak
-rw-r----- 1 oracle oinstall 10600448 1月  12 15:13 controlfile.bak
-rw-r--r-- 1 oracle oinstall     5849 1月  11 16:09 ctl001.txt
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 公共
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 模板
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 视频
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 图片
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 文档
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 下载
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 音乐
drwxr-xr-x 2 oracle oinstall        6 12月 19 20:19 桌面
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0

7.1.4 恢复控制文件

使用控制文件覆盖数据库,用redo日志去恢复

一个一个尝试 redo01.log,redo02.log 或redo03.log.....

[oracle@node1 ~]$ rm -rf /app/oracle/oradata/ORCL/control01.ctl
[oracle@node1 ~]$ rm -rf /app/oracle/oradata/ORCL/control02.ctl
[oracle@node1 ~]$ cp /home/oracle/controlfile.bak /app/oracle/oradata/ORCL/control01.ctl
[oracle@node1 ~]$ cp /home/oracle/controlfile.bak /app/oracle/oradata/ORCL/control02.ctl
[oracle@node1 ~]$ sas
SQL> startup
ORACLE instance started.
Total System Global Area 8002730448 bytes
Fixed Size    8915408 bytes
Variable Size 1308622848 bytes
Database Buffers 6677331968 bytes
Redo Buffers    7860224 bytes
Database mounted.
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
SQL> recover database using backup controlfile;
ORA-00279: change 7636324 generated at 01/12/2023 15:37:27 needed for thread 1
ORA-00289: suggestion : /app/archive/orcl_62725153_0000000001_1_1125934647.dbf
ORA-00280: change 7636324 for thread 1 is in sequence #1
Specify log: {<RET>=suggested | filename | AUTO | CANCEL} 
/app/oracle/oradata/ORCL/redo01.log
Log applied.
Media recovery complete.
SQL>  alter database open resetlogs;
Database altered.

7.2 使用 rman 备份(推荐)

7.2.1 查看数据库的归档状态

SQL> archive log list
Database log mode       Archive Mode
Automatic archival       Enabled
Archive destination       /app/archive
Oldest online log sequence     49
Next log sequence to archive   51
Current log sequence       51

7.2.2 备份控制文件

[oracle@node1 ~]$ rman target /
RMAN> backup current controlfile format '/app/rmanbak/control_2023_01_12.bak';

7.2.3 查看控制文件的备份信息

RMAN> list backup of controlfile;

7.2.4 使用 rman 恢复控制文件

由于误操作、磁盘故障等导致控制文件全部丢失时,可以使用备份的控制文件进行恢复操作。下面模拟控制文件全部丢失时,恢复数据库的操作

SQL> show parameter control_file
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_file_record_keep_time        integer     7
control_files                        string      /app/oracle/oradata/ORCL/control01.ctl, /app/oracle/oradata/ORCL/control02.ctl
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
[oracle@node1 ~]$ rm -rf /app/oracle/oradata/ORCL/control01.ctl
[oracle@node1 ~]$ rm -rf /app/oracle/oradata/ORCL/control02.ctl

7.2.5 启动数据库,出现错误

SQL> startup
ORACLE instance started.
Total System Global Area 8002730448 bytes
Fixed Size    8915408 bytes
Variable Size 1308622848 bytes
Database Buffers 6677331968 bytes
Redo Buffers    7860224 bytes
ORA-00205: error in identifying control file, check alert log for more info

7.2.6 数据库启动到 nomount 状态

SQL> select status from v$instance;
STATUS
STARTED

7.2.7 使用 RMAN 还原控制文件

RMAN> restore controlfile from '/app/rmanbak/control_2023_01_12.bak';
Starting restore at 12-JAN-23
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=1709 device type=DISK
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/app/oracle/oradata/ORCL/control01.ctl
output file name=/app/oracle/oradata/ORCL/control02.ctl
Finished restore at 12-JAN-23

7.2.8 启动数据库到 mount 状态

RMAN> alter database mount;
released channel: ORA_DISK_1
Statement processed

7.2.9 恢复数据库

RMAN> recover database;
Starting recover at 12-JAN-23
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=1709 device type=DISK
starting media recovery
archived log for thread 1 with sequence 51 is already on disk as file /app/oracle/oradata/ORCL/redo03.log
archived log file name=/app/oracle/oradata/ORCL/redo03.log thread=1 sequence=51
media recovery complete, elapsed time: 00:00:01
Finished 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 '<dir>' 

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 PERFORMANCE
STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "ORCL" NORESETLOGS  ARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 292
LOGFILE
  GROUP 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 LOGFILE
DATAFILE
  '/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 PERFORMANCE
STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "ORCL" RESETLOGS  ARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 292
LOGFILE
  GROUP 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 LOGFILE
DATAFILE
  '/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.
--

注:大致意思是分为两种创建控制文件方式,第一种日志可用, 第二种日志不可用的状态

7.3.4 日志可用的情况下重建控制文件

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
[oracle@node1 ~]$ rm -rf /app/oracle/oradata/ORCL/control01.ctl
[oracle@node1 ~]$ rm -rf /app/oracle/oradata/ORCL/control02.ctl
SQL> startup nomount
ORACLE instance started.
Total System Global Area 8002730448 bytes
Fixed Size    8915408 bytes
Variable Size 1308622848 bytes
Database Buffers 6677331968 bytes
Redo Buffers    7860224 bytes
SQL> CREATE CONTROLFILE REUSE DATABASE "ORCL" NORESETLOGS  ARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 292
LOGFILE
  GROUP 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 LOGFILE
DATAFILE
  '/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
 20  ;
SQL> ALTER DATABASE OPEN;
Database altered.
SQL> ALTER TABLESPACE TEMP ADD TEMPFILE '/app/oracle/oradata/ORCL/temp01.dbf' 
SIZE 33554432  REUSE AUTOEXTEND ON NEXT 655360  MAXSIZE 32767M;
Tablespace altered.

7.3.5 日志不可用的情况下重建控制文件

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
[oracle@node1 ~]$ rm -rf /app/oracle/oradata/ORCL/control01.ctl
[oracle@node1 ~]$ rm -rf /app/oracle/oradata/ORCL/control02.ctl
SQL> startup nomount
ORACLE instance started.
Total System Global Area 8002730448 bytes
Fixed Size    8915408 bytes
Variable Size 1308622848 bytes
Database Buffers 6677331968 bytes
Redo Buffers    7860224 bytes
SQL> CREATE CONTROLFILE REUSE DATABASE "ORCL" RESETLOGS  ARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 292
LOGFILE
  GROUP 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 LOGFILE
DATAFILE
  '/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
 20  ;
Control file created.
SQL> RECOVER DATABASE USING BACKUP CONTROLFILE
ORA-00279: change 7647399 generated at 01/12/2023 19:00:06 needed for thread 1
ORA-00289: suggestion : /app/archive/orcl_62725153_0000000002_1_1125935795.dbf
ORA-00280: change 7647399 for thread 1 is in sequence #2
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
/app/oracle/oradata/ORCL/redo01.log
ORA-00310: ���������������� 1; �������� 2 ORA-00334: ��������:
'/app/oracle/oradata/ORCL/redo01.log'
SQL> RECOVER DATABASE USING BACKUP CONTROLFILE
ORA-00279: change 7647399 generated at 01/12/2023 19:00:06 needed for thread 1
ORA-00289: suggestion : /app/archive/orcl_62725153_0000000002_1_1125935795.dbf
ORA-00280: change 7647399 for thread 1 is in sequence #2
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
/app/oracle/oradata/ORCL/redo02.log
Log applied.
Media recovery complete.}
SQL> ALTER DATABASE OPEN RESETLOGS;
Database altered.
SQL> ALTER TABLESPACE TEMP ADD TEMPFILE '/app/oracle/oradata/ORCL/temp01.dbf' 
SIZE 33554432  REUSE AUTOEXTEND ON NEXT 655360  MAXSIZE 32767M;
Tablespace altered.

到此这篇关于Oracle的控制文件管理的文章就介绍到这了,更多相关Oracle 控制文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文