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 | 实例恢复不在需要联机重做文件日志组,可能归档也可能未归档 |
查看日志文件当前活动状态
SQL> select group#,status,members,archived from v$log;
GROUP# STATUS MEMBERS ARC
---------- ---------------- ---------- ---
1 INACTIVE 1 YES
2 INACTIVE 1 YES
3 CURRENT 1 NO
V$LOGFILE中STATUS的状态值
INVALID | 表明该文件不可访问 |
---|---|
STALE | 表明文件内容不完全 |
DELETED | 表明该文件不再使用 |
NULL | 表明文件正在使用 |
SQL> select group#,status,member from v$logfile;
GROUP# STATUS
---------- -------
MEMBER
--------------------------------------------------------------------------------
1
/opt/oracle/oradata/ORCLCDB/redo01.log
3
/opt/oracle/oradata/ORCLCDB/redo03.log
2
/opt/oracle/oradata/ORCLCDB/redo02.log
切换日志组
SQL> select group#,status,members,archived from v$log;
GROUP# STATUS MEMBERS ARC
---------- ---------------- ---------- ---
1 ACTIVE 1 YES
2 CURRENT 1 NO
3 INACTIVE 1 YES
SQL> alter system switch logfile;
System altered.
SQL> select group#,status,members,archived from v$log;
GROUP# STATUS MEMBERS ARC
---------- ---------------- ---------- ---
1 ACTIVE 1 YES
2 ACTIVE 1 YES
3 CURRENT 1 NO
添加日志组文件
SQL> alter database add logfile group 4 ('/opt/oracle/oradata/ORCLCDB/redo04a.log','/opt/oracle/oradata/ORCLCDB/redo04b.log') size 15M;
Database altered.
SQL> select group#,status,member from v$logfile;
GROUP# STATUS
---------- -------
MEMBER
--------------------------------------------------------------------------------
1
/opt/oracle/oradata/ORCLCDB/redo01.log
3
/opt/oracle/oradata/ORCLCDB/redo03.log
2
/opt/oracle/oradata/ORCLCDB/redo02.log
GROUP# STATUS
---------- -------
MEMBER
--------------------------------------------------------------------------------
4
/opt/oracle/oradata/ORCLCDB/redo04a.log
4
/opt/oracle/oradata/ORCLCDB/redo04b.log
删除日志组,在删除之前查看日志组状态是否为inactive或者unused,之后清除操作系统文件。
SQL> select group#,status,members,archived from v$log;
GROUP# STATUS MEMBERS ARC
---------- ---------------- ---------- ---
1 INACTIVE 1 YES
2 INACTIVE 1 YES
3 CURRENT 1 NO
4 UNUSED 2 YES
SQL> alter database drop logfile group 1;
Database altered.
SQL> select group#,status,members,archived from v$log;
GROUP# STATUS MEMBERS ARC
---------- ---------------- ---------- ---
2 INACTIVE 1 YES
3 CURRENT 1 NO
4 UNUSED 2 YES
添加成员文件
SQL> alter database add logfile member '/opt/oracle/oradata/ORCLCDB/redo02a.log' to group 2 ;
Database altered.
SQL> select group#,member from v$logfile;
GROUP#
----------
MEMBER
--------------------------------------------------------------------------------
3
/opt/oracle/oradata/ORCLCDB/redo03a.log
3
/opt/oracle/oradata/ORCLCDB/redo03.log
2
/opt/oracle/oradata/ORCLCDB/redo02.log
GROUP#
----------
MEMBER
--------------------------------------------------------------------------------
4
/opt/oracle/oradata/ORCLCDB/redo04a.log
4
/opt/oracle/oradata/ORCLCDB/redo04b.log
重命名日志文件
1.查询日志文件状态在inaction或者unused状态下直接拷贝重命名。
select * from v$logfile;
3 ONLINE /opt/oracle/oradata/ORCLCDB/redo03a.log NO 0
3 ONLINE /opt/oracle/oradata/ORCLCDB/redo03.log NO 0
2 ONLINE /opt/oracle/oradata/ORCLCDB/redo02.log NO 0
4 ONLINE /opt/oracle/oradata/ORCLCDB/redo4a.log NO 0
4 ONLINE /opt/oracle/oradata/ORCLCDB/redo4b.log NO 0
2 ONLINE /opt/oracle/oradata/ORCLCDB/redo02a.log NO 0
[oracle@instance-ej1hspdt ~]$ cp /opt/oracle/oradata/ORCLCDB/redo04a.log /opt/oracle/oradata/ORCLCDB/redo4
a.log
[oracle@instance-ej1hspdt ~]$ cp /opt/oracle/oradata/ORCLCDB/redo04b.log /opt/oracle/oradata/ORCLCDB/redo4
b.log
alter database rename file '/opt/oracle/oradata/ORCLCDB/redo04b.log'
to '/opt/oracle/oradata/ORCLCDB/redo4b.log';
在数据库一致性关闭的情况下,日志文件缺失,可以直接清理数据库文件,会自动重建日志文件,重建之后日志文件为unused状态。
SQL> alter database clear logfile group 2;
alter database clear logfile group 2
*
ERROR at line 1:
ORA-00350: log 2 of instance ORCLCDB (thread 1) needs to be archived
ORA-00312: online log 2 thread 1: '/opt/oracle/oradata/ORCLCDB/redo02.log'
ORA-00312: online log 2 thread 1: '/opt/oracle/oradata/ORCLCDB/redo02a.log'
SQL> alter database clear unarchived logfile group 2;
Database altered.
在日志文件不一致的情况下,可通过清理日志文件,恢复到unused状态
SQL> alter database clear logfile group 4;
Database altered.
SQL> select * from v$log;
GROUP# THREAD# SEQUENCE# BYTES BLOCKSIZE MEMBERS ARC
---------- ---------- ---------- ---------- ---------- ---------- ---
STATUS FIRST_CHANGE# FIRST_TIM NEXT_CHANGE# NEXT_TIME CON_ID
---------------- ------------- --------- ------------ --------- ----------
2 1 0 209715200 512 2 YES
UNUSED 7152652 26-JUL-19 7183949 26-JUL-19 0
3 1 72 209715200 512 2 NO
CURRENT 7183949 26-JUL-19 1.8447E+19 0
4 1 0 15728640 512 2 YES
UNUSED 7152153 26-JUL-19 7152652 26-JUL-19 0