1、重做日志的目的
    保存对DML操作的日志,在DWRN进程尚未将已提交的事务写入dbf数据文件时,先将事务写入重做日志文件,防止数据库异常关闭数据丢失。在数据库重新启动时用日志文件恢复尚未写入数据文件的数据。
    2、什么是重做日志组
    重做日志组是逻辑上的概念,由重做日志文件组成的。要保证oracle数据库正常工作,至少需要2个重做日志组,多数oracle版本默认3个重做日志组
    3、什么是重做日志成员
    重做日志组中的每个重做日志文件叫做重做日志成员。oracle默认每个重做日志组只有一个成员,但是oracle建议每个重做日志组至少有2个成员,且最好放在不同物理磁盘上,以防止重做日志成员文件的物理错误,许多数据库的每个重做日志做有3个成员。
    4、重做日志怎样工作
    重做日志写进程(LGWR)在任意时刻只能写一组重做日志组,LGWR后台进程正在写的重做日志组叫做当前重做日志组。
    LGWR将完全相同的信息从重做日志缓冲区复制到改组的每个重做日志文件中。LGWR是以循环的方式写重做日志组的,当写满一个重做日志组时,就会开始写下一组重做日志,这称为日志切换,当写满最后一组时,LGWR又开始写第一组重做日志。
    如果数据库运行在归档模式下(oracle默认为非归档模式),当LGWR的写操作从一个重做日志组切换到下一个重做日志组后,归档写进程(ARCH/ARCH0)就会将原来的重做日志文件中的信息复制到归档日志文件中。oracle服务器保证在归档写进程没有将重做日志文件中的信息复制到归档日志文件中之前,LGWR进程不能再写这组重做日志。
    ——————————————————————————————————————————————————————————

    V$LOG中STATUS的状态值

    UNUSED 从未对该联机日志写入任何内容,一般为新增加联机日志文件或是使用resetlog后的状态
    CURRENT 当前重做日志文件,表示该重做日志文件为活动状态,能够被打开和关闭
    ACTIVE 处于活动状态,不属于当前日志,崩溃恢复需要该状态,可用于块恢复,可能归档,也可能未归档
    CLEARING 表示在执行alter database clear logfile命令后正将该日志重建为一个空日志,重建后状态变为unused
    CLEARING_CURRENT 当前日志处于关闭线程的清除状态。如日志某些故障或写入新日志标头时发生I/O错误
    INACTIVE 实例恢复不在需要联机重做文件日志组,可能归档也可能未归档

    查看日志文件当前活动状态

    1. SQL> select group#,status,members,archived from v$log;
    2. GROUP# STATUS MEMBERS ARC
    3. ---------- ---------------- ---------- ---
    4. 1 INACTIVE 1 YES
    5. 2 INACTIVE 1 YES
    6. 3 CURRENT 1 NO

    V$LOGFILE中STATUS的状态值

    INVALID 表明该文件不可访问
    STALE 表明文件内容不完全
    DELETED 表明该文件不再使用
    NULL 表明文件正在使用
    1. SQL> select group#,status,member from v$logfile;
    2. GROUP# STATUS
    3. ---------- -------
    4. MEMBER
    5. --------------------------------------------------------------------------------
    6. 1
    7. /opt/oracle/oradata/ORCLCDB/redo01.log
    8. 3
    9. /opt/oracle/oradata/ORCLCDB/redo03.log
    10. 2
    11. /opt/oracle/oradata/ORCLCDB/redo02.log

    切换日志组

    1. SQL> select group#,status,members,archived from v$log;
    2. GROUP# STATUS MEMBERS ARC
    3. ---------- ---------------- ---------- ---
    4. 1 ACTIVE 1 YES
    5. 2 CURRENT 1 NO
    6. 3 INACTIVE 1 YES
    7. SQL> alter system switch logfile;
    8. System altered.
    9. SQL> select group#,status,members,archived from v$log;
    10. GROUP# STATUS MEMBERS ARC
    11. ---------- ---------------- ---------- ---
    12. 1 ACTIVE 1 YES
    13. 2 ACTIVE 1 YES
    14. 3 CURRENT 1 NO

    添加日志组文件

    1. SQL> alter database add logfile group 4 ('/opt/oracle/oradata/ORCLCDB/redo04a.log','/opt/oracle/oradata/ORCLCDB/redo04b.log') size 15M;
    2. Database altered.
    3. SQL> select group#,status,member from v$logfile;
    4. GROUP# STATUS
    5. ---------- -------
    6. MEMBER
    7. --------------------------------------------------------------------------------
    8. 1
    9. /opt/oracle/oradata/ORCLCDB/redo01.log
    10. 3
    11. /opt/oracle/oradata/ORCLCDB/redo03.log
    12. 2
    13. /opt/oracle/oradata/ORCLCDB/redo02.log
    14. GROUP# STATUS
    15. ---------- -------
    16. MEMBER
    17. --------------------------------------------------------------------------------
    18. 4
    19. /opt/oracle/oradata/ORCLCDB/redo04a.log
    20. 4
    21. /opt/oracle/oradata/ORCLCDB/redo04b.log

    删除日志组,在删除之前查看日志组状态是否为inactive或者unused,之后清除操作系统文件。

    1. SQL> select group#,status,members,archived from v$log;
    2. GROUP# STATUS MEMBERS ARC
    3. ---------- ---------------- ---------- ---
    4. 1 INACTIVE 1 YES
    5. 2 INACTIVE 1 YES
    6. 3 CURRENT 1 NO
    7. 4 UNUSED 2 YES
    8. SQL> alter database drop logfile group 1;
    9. Database altered.
    10. SQL> select group#,status,members,archived from v$log;
    11. GROUP# STATUS MEMBERS ARC
    12. ---------- ---------------- ---------- ---
    13. 2 INACTIVE 1 YES
    14. 3 CURRENT 1 NO
    15. 4 UNUSED 2 YES

    添加成员文件

    1. SQL> alter database add logfile member '/opt/oracle/oradata/ORCLCDB/redo02a.log' to group 2 ;
    2. Database altered.
    3. SQL> select group#,member from v$logfile;
    4. GROUP#
    5. ----------
    6. MEMBER
    7. --------------------------------------------------------------------------------
    8. 3
    9. /opt/oracle/oradata/ORCLCDB/redo03a.log
    10. 3
    11. /opt/oracle/oradata/ORCLCDB/redo03.log
    12. 2
    13. /opt/oracle/oradata/ORCLCDB/redo02.log
    14. GROUP#
    15. ----------
    16. MEMBER
    17. --------------------------------------------------------------------------------
    18. 4
    19. /opt/oracle/oradata/ORCLCDB/redo04a.log
    20. 4
    21. /opt/oracle/oradata/ORCLCDB/redo04b.log

    重命名日志文件
    1.查询日志文件状态在inaction或者unused状态下直接拷贝重命名。

    1. select * from v$logfile;
    2. 3 ONLINE /opt/oracle/oradata/ORCLCDB/redo03a.log NO 0
    3. 3 ONLINE /opt/oracle/oradata/ORCLCDB/redo03.log NO 0
    4. 2 ONLINE /opt/oracle/oradata/ORCLCDB/redo02.log NO 0
    5. 4 ONLINE /opt/oracle/oradata/ORCLCDB/redo4a.log NO 0
    6. 4 ONLINE /opt/oracle/oradata/ORCLCDB/redo4b.log NO 0
    7. 2 ONLINE /opt/oracle/oradata/ORCLCDB/redo02a.log NO 0
    8. [oracle@instance-ej1hspdt ~]$ cp /opt/oracle/oradata/ORCLCDB/redo04a.log /opt/oracle/oradata/ORCLCDB/redo4
    9. a.log
    10. [oracle@instance-ej1hspdt ~]$ cp /opt/oracle/oradata/ORCLCDB/redo04b.log /opt/oracle/oradata/ORCLCDB/redo4
    11. b.log
    12. alter database rename file '/opt/oracle/oradata/ORCLCDB/redo04b.log'
    13. to '/opt/oracle/oradata/ORCLCDB/redo4b.log';

    在数据库一致性关闭的情况下,日志文件缺失,可以直接清理数据库文件,会自动重建日志文件,重建之后日志文件为unused状态。

    1. SQL> alter database clear logfile group 2;
    2. alter database clear logfile group 2
    3. *
    4. ERROR at line 1:
    5. ORA-00350: log 2 of instance ORCLCDB (thread 1) needs to be archived
    6. ORA-00312: online log 2 thread 1: '/opt/oracle/oradata/ORCLCDB/redo02.log'
    7. ORA-00312: online log 2 thread 1: '/opt/oracle/oradata/ORCLCDB/redo02a.log'
    8. SQL> alter database clear unarchived logfile group 2;
    9. Database altered.

    在日志文件不一致的情况下,可通过清理日志文件,恢复到unused状态

    1. SQL> alter database clear logfile group 4;
    2. Database altered.
    3. SQL> select * from v$log;
    4. GROUP# THREAD# SEQUENCE# BYTES BLOCKSIZE MEMBERS ARC
    5. ---------- ---------- ---------- ---------- ---------- ---------- ---
    6. STATUS FIRST_CHANGE# FIRST_TIM NEXT_CHANGE# NEXT_TIME CON_ID
    7. ---------------- ------------- --------- ------------ --------- ----------
    8. 2 1 0 209715200 512 2 YES
    9. UNUSED 7152652 26-JUL-19 7183949 26-JUL-19 0
    10. 3 1 72 209715200 512 2 NO
    11. CURRENT 7183949 26-JUL-19 1.8447E+19 0
    12. 4 1 0 15728640 512 2 YES
    13. UNUSED 7152153 26-JUL-19 7152652 26-JUL-19 0