代码改变世界

体系结构之联机重做日志

2020-12-11 11:28  EniNiemand  阅读(26)  评论(0编辑  收藏  举报
  1. 又称在线联机日志/在线归档重做日志
  2. 特点
    1. 记录数据库的变化
    2. 用于数据块的恢复
    3. 以日志组的方式进行管理,至少有两组且每组至少有两个成员
    4. 一般情况下和数据文件放在不同的磁盘中
    5. 在线重做日志是循环覆盖写的
  3. 数据字典
    1. v$log;
    2. v$logfile
    3. v$archived_log
  4. 日志组
    1. 日志组切换
      1. 在归档模式下:执行日志切换命令,LGWR进程将打开下一个日志组进行记录,并通知ARCn进程将前一个日志组的日志进行归档保存。
      2. 在非归档模式下:执行日志切换命令,LGWR进程将打开下一个日志组进行记录,对于历史日志组的记录将覆盖。
    2. 日志组成员 member---每组的日志成员,内容都是完全一致的
    3. 查看日志组及成员
SQL> select a.group#,a.members,a.status,a.bytes/1024/1024 M,b.member from v$log a, v$logfile b where a.group#=b.group# order by 1;
    1. 添加日志组
SQL> alter database add logfile group 4('/u01/app/oracle/oradata/PROD1/redo04.log','/u01/app/oracle/oradata/PROD1/redo04a.log') size 200M reuse;
    1. 添加日志组成员
SQL> alter database add logfile member '/u01/app/oracle/oradata/PROD1/redo04b.log' to group 4; ----单独添加日志组成员时,状态为INVALID,手工切换日志进行同步组成员 SQL> alter system switch logfile;
    1. 日志组状态
      1. UNUSED:新创建的日志组状态,一旦被使用,就不存在这个状态了
      2. CURRENT:当前数据库正在使用的日志组,不允许被覆盖
      3. ACTIVE:数据缓冲区里面的数据还没有刷入到磁盘,不允许被覆盖-----当发出alter system checkpoint;命令后,会从ACTIVE变成INACTIVE
      4. INACTIVE:非活动状态,可以被覆盖
    2. 删除日志组
SQL> alter database drop logfile group 1(2,3,4,5); ------删除的是控制文件里的元数据,但是物理文件仍然是存在的
    1. 清除日志(不是删除日志,而是重建日志)
SQL> alter database clear logfile group 1;------隐含的条件是日志已归档 SQL> alter database clear unarchived logfile group 1;-------未归档的日志清除时,使用此命令
  1. 备份、还原、恢复的概念
    1. 备份:backup
    2. 还原:restore 把原来备份的东西重新拿过来,这个过程(动作)叫还原。
    3. 恢复:recover 利用重做日志把数据库恢复到指定的时间点(完全恢复、不完全恢复)
  2. 在线日志组恢复  (重点)
0
  1. CURRENT日志组丢失
    1. 非归档模式
      1. 如果Current日志组丢失,只能借助备份文件进行完全还原,无法进行实例恢复,即将丢失一部分数据。 非归档模式下,不管实例有没有崩溃,都只能进行完全还原。
      2. 步骤
        1. 关闭数据库;
        2. 将备份文件覆盖现有文件;
        3. 开启数据库;
    2. 归档模式

      1. 实例没有崩溃

             1. 执行切换日志组  SQL> alter system switch logfile;

                2. 执行完全检查点  SQL> alter system checkpoint;

                3. 强制清除日志组(清除日志组不是删除,而是重建)

SQL> alter database clear unarchived logfile group 1;----在归档模式下,清除日志组命令隐含着要将当前的日志组归档,但当前日志组所对应的物理文件已被删除,无法进行归档,所以报错。因此需用UNARCHIVED进行强制清除。
    1. 实例崩溃
      1. 实例崩溃必须通过还原和基于cancel-based时间点恢复来处理
        1. 情况一:还原了所有的数据文件、控制文件和日志文件。因为是一致性的,所以可以直接startup启动。
        2. 情况二:还原了所有的数据文件、控制文件
SQL> startup mount;
SQL> recover database until cancel using backup controlfile;
    1. 情况三:仅还原所有的数据文件。SQL> startup mount; SQL> recover database until cancel;

 

实验:在线日志组容量扩容

实验需求:需要3个日志组,每个日志组有两个成员,大小为200M

实验步骤:

  1. 查看现有的日志组成员及其大小

  2. 添加两个过渡日志组,每组两个成员

  3. 执行日志组切换及完全检查点,使现有的日志组都处于INACTIVE状态

  4. 删除现有日志组及其物理文件

  5. 按照需要重建3个日志组,每组两个成员,大小为200M

  6. 执行日志组切换及完全检查点,使过渡组都处于INACTIVE状态

  7. 删除过渡组及物理文件

  8. 查看此时的日志组成员及大小

实验代码:

第一步:
SQL> select a.GROUP#,a.SEQUENCE#,a.BYTES/1024/1024 M,a.MEMBERS,a.status,b.member,b.status from v$log a,v$logfile b where a.group#=b.group# orderby 1;

    GROUP#  SEQUENCE#           M    MEMBERS STATUS     MEMBER                         STATUS
---------- ---------- ---------- ---------- ---------- --------------------------------------------- ----------
     1       57         200      1 ACTIVE     /u01/app/oracle/oradata/OCP12C/redo01.log
     2       54         200      1 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo02.log
     3       55         200      1 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo03.log
     4       56         200      2 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo04a.log
     4       56         200      2 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo04b.log
     5       58         200      1 CURRENT    /u01/app/oracle/oradata/OCP12C/redo05a.log

6 rows selected.
第二步:
SQL> alter database add logfile group 6('/u01/app/oracle/oradata/OCP12C/redo06a.log','/u01/app/oracle/oradata/OCP12C/redo06b.log') size 200m;

Database altered.

SQL> alter database add logfile group 7('/u01/app/oracle/oradata/OCP12C/redo07a.log','/u01/app/oracle/oradata/OCP12C/redo07b.log') size 200m;

Database altered.
SQL> select a.group#,a.sequence#,a.bytes/1024/1024 m,a.members,a.status,b.member,b.status from v$log a,v$logfile b where a.group#=b.group#;

    GROUP#  SEQUENCE#           M    MEMBERS STATUS     MEMBER                         STATUS
---------- ---------- ---------- ---------- ---------- --------------------------------------------- ----------
     1       57         200      1 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo01.log
     3       55         200      1 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo03.log
     2       54         200      1 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo02.log
     4       56         200      2 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo04a.log
     4       56         200      2 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo04b.log
     5       58         200      1 CURRENT    /u01/app/oracle/oradata/OCP12C/redo05a.log
     6        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo06a.log
     6        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo06b.log
     7        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo07a.log
     7        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo07b.log

10 rows selected.
第三步:
SQL> alter system switch logfile;

System altered.
SQL> alter system checkpoint;

System altered.

SQL> select a.group#,a.sequence#,a.bytes/1024/1024 m,a.members,a.status,b.member,b.status from v$log a,v$logfile b where a.group#=b.group#;

    GROUP#  SEQUENCE#           M    MEMBERS STATUS     MEMBER                         STATUS
---------- ---------- ---------- ---------- ---------- --------------------------------------------- ----------
     1       57         200      1 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo01.log
     3       55         200      1 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo03.log
     2       54         200      1 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo02.log
     4       56         200      2 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo04a.log
     4       56         200      2 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo04b.log
     5       58         200      1 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo05a.log
     6       59         200      2 CURRENT    /u01/app/oracle/oradata/OCP12C/redo06a.log
     6       59         200      2 CURRENT    /u01/app/oracle/oradata/OCP12C/redo06b.log
     7        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo07a.log
     7        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo07b.log

10 rows selected.
第四步:
SQL> alter database drop logfile group 1;

Database altered.

SQL> alter database drop logfile group 2;

Database altered.

SQL> alter database drop logfile group 3;

Database altered.

SQL> alter database drop logfile group 4;

Database altered.

SQL> alter database drop logfile group 5;

Database altered.
[oracle@oracle12c OCP12C]$ rm -rf redo01.log
[oracle@oracle12c OCP12C]$ rm -rf redo02.log
[oracle@oracle12c OCP12C]$ rm -rf redo03.log
[oracle@oracle12c OCP12C]$ rm -rf redo04*
[oracle@oracle12c OCP12C]$ rm -rf redo05a.log

SQL> select a.group#,a.sequence#,a.bytes/1024/1024 m,a.members,a.status,b.member,b.status from v$log a,v$logfile b where a.group#=b.group#;

    GROUP#  SEQUENCE#           M    MEMBERS STATUS     MEMBER                         STATUS
---------- ---------- ---------- ---------- ---------- --------------------------------------------- ----------
     6       59         200      2 CURRENT    /u01/app/oracle/oradata/OCP12C/redo06a.log
     6       59         200      2 CURRENT    /u01/app/oracle/oradata/OCP12C/redo06b.log
     7        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo07a.log
     7        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo07b.log
第五步:
SQL> alter database add logfile group 1('/u01/app/oracle/oradata/OCP12C/redo01a.log','/u01/app/oracle/oradata/OCP12C/redo01b.log') size 200m;

Database altered.

SQL> alter database add logfile group 2('/u01/app/oracle/oradata/OCP12C/redo02a.log','/u01/app/oracle/oradata/OCP12C/redo02b.log') size 200m;

Database altered.

SQL> alter database add logfile group 3('/u01/app/oracle/oradata/OCP12C/redo03a.log','/u01/app/oracle/oradata/OCP12C/redo03b.log') size 200m;

Database altered.
第六步:
SQL> alter system switch logfile;

System altered.
SQL> alter system checkpoint;

System altered.

SQL> select a.group#,a.sequence#,a.bytes/1024/1024 m,a.members,a.status,b.member,b.status from v$log a,v$logfile b where a.group#=b.group# orderby 1;

    GROUP#  SEQUENCE#           M    MEMBERS STATUS     MEMBER                         STATUS
---------- ---------- ---------- ---------- ---------- --------------------------------------------- ----------
     1       60         200      2 CURRENT    /u01/app/oracle/oradata/OCP12C/redo01a.log
     1       60         200      2 CURRENT    /u01/app/oracle/oradata/OCP12C/redo01b.log
     2        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo02a.log
     2        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo02b.log
     3        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo03a.log
     3        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo03b.log
     6       59         200      2 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo06a.log
     6       59         200      2 INACTIVE   /u01/app/oracle/oradata/OCP12C/redo06b.log
     7        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo07a.log
     7        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo07b.log

10 rows selected.
第七步:
SQL> alter database drop logfile group 6;

Database altered.

SQL> alter database drop logfile group 7;

Database altered.
[oracle@oracle12c OCP12C]$ rm -rf redo06*
[oracle@oracle12c OCP12C]$ rm -rf redo07*
第八步:
SQL> select a.group#,a.sequence#,a.bytes/1024/1024 m,a.members,a.status,b.member,b.status from v$log a,v$logfile b where a.group#=b.group# orderby 1;

    GROUP#  SEQUENCE#           M    MEMBERS STATUS     MEMBER                         STATUS
---------- ---------- ---------- ---------- ---------- --------------------------------------------- ----------
     1       60         200      2 CURRENT    /u01/app/oracle/oradata/OCP12C/redo01a.log
     1       60         200      2 CURRENT    /u01/app/oracle/oradata/OCP12C/redo01b.log
     2        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo02a.log
     2        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo02b.log
     3        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo03a.log
     3        0         200      2 UNUSED     /u01/app/oracle/oradata/OCP12C/redo03b.log
View Code