Oracle11g redo log 建立、新增、刪除(重做日誌組,重做日誌檔案)

君子不怨天發表於2020-12-18

Oracle11g redo log建立、新增、刪除(重做日誌組,重做日誌檔案)

一、獲取redo log資訊

1、v$log檢視常用資訊查詢:
select group#, sequence#, members, bytes, status, archived from v$log;

select * from v$log;

查詢輸出:

SQL> select group#, sequence#, members, bytes, status, archived
from v$log;  2  

    GROUP#  SEQUENCE#	 MEMBERS      BYTES STATUS	 ARC
---------- ---------- ---------- ---------- -------- ---
	 1	  140	       2  104857600 CURRENT	   	     NO
	 2	  133	       2  104857600 INACTIVE	     NO
	 3	  137	       2  104857600 INACTIVE	     NO
	 4	  134	       2  104857600 INACTIVE	     NO
	 5	  135	       2  104857600 INACTIVE	     NO
	 6	  136	       2  104857600 INACTIVE	     NO
	 7	  138	       2  104857600 INACTIVE	     NO
	 8	  139	       2  104857600 INACTIVE	     NO

8 rows selected.

SQL>

其中各列含義
group:redo log組號
sequence:資料庫產生的,每個redo log都有唯一的序列編號,以供將來進行資料庫恢復使用
members:redo log中redo logfile數量
bytes:檔案大小
status:redo log狀態有以下幾種:

  1. inactive:表示例項恢復已不再需要這組redo log了;(redo log在這個狀態下才可以刪除)
  2. active: 表示這組聯機redo log是活動的但不是當前組,在例項恢復時需要這組聯機redo log,如這組redo log正在歸檔;
  3. current: 表示這組聯機redo log是當前組,並也隱含該聯機redo log是活動的;
  4. unused: 表示Oracle伺服器從來沒有寫過該組聯機redo log,這是redo log剛被新增到資料庫中狀態。
2、v$logfile檢視查詢
col member for a45
select group#, status, type, member
from v$logfile;

該檢視獲取資料庫中每一個redo log的每個成員、所在的目錄、檔名以及他們的狀態資訊等
查詢輸出:

SQL> col member for a45
select group#, status, type, member
from v$logfile;SQL>   2  

    GROUP# STATUS  TYPE    MEMBER
---------- ------- ------- ---------------------------------------------
	 2	 		   ONLINE  /u01/app/oracle/oradata/orcl/redo02a.log
	 1	  		   ONLINE  /u01/app/oracle/oradata/orcl/redo01a.log
	 3	  		   ONLINE  /u01/app/oracle/oradata/orcl/redo03a.log
	 2	   		   ONLINE  /u01/app/oracle/oradata/orcl/redo02b.log
	 1	   		   ONLINE  /u01/app/oracle/oradata/orcl/redo01b.log
	 4	  		   ONLINE  /u01/app/oracle/oradata/orcl/redo04a.log
	 4	   		   ONLINE  /u01/app/oracle/oradata/orcl/redo04b.log
	 5	   		   ONLINE  /u01/app/oracle/oradata/orcl/redo05a.log
	 5	   		   ONLINE  /u01/app/oracle/oradata/orcl/redo05b.log
	 6	   		   ONLINE  /u01/app/oracle/oradata/orcl/redo06a.log
	 6	   		   ONLINE  /u01/app/oracle/oradata/orcl/redo06b.log

    GROUP# STATUS  TYPE    MEMBER
---------- ------- ------- ---------------------------------------------
	 7	   		   ONLINE  /u01/app/oracle/oradata/orcl/redo07a.log
	 7	   		   ONLINE  /u01/app/oracle/oradata/orcl/redo07b.log
	 8	  		   ONLINE  /u01/app/oracle/oradata/orcl/redo08a.log
	 8	  		   ONLINE  /u01/app/oracle/oradata/orcl/redo08b.log
	 3	  	       ONLINE  /u01/app/oracle/oradata/orcl/redo03b.log

16 rows selected.

SQL> 

在v$logfile檢視中的redo log的status有以下幾種:
空白:表示該檔案正在使用。
stale:表示該檔案中的內容是不完全的。
invalid:表示該檔案不可以被訪問。
deleted:表示該檔案已不再有用了。

二、建立redo log

注意:在建立redo logfile時,會自動按照順序建立新的redo log,如果之前的組號有缺失(比如沒有第二組,本次建立的組號即為2組)

ALTER DATABASE ADD LOGFILE ('新建立redo logfile路徑') SIZE 數值K/M/G;

例如:新增一個redo log,按照組號順序排列為9組,redo log大小為100M,
其中只有一個redo logfile。

這裡首先要明確一個概念,redo log與redo logfile的大小關係
一個redo log由可以由多個redo logfile組成,redo log的大小決定了其中每個redo logfile的大小,不是相加的關係,更像是並列的關係,
因為lgwt要在一個redo log中同時寫其中的redo logfile,所以每一個redo logfile必須是一樣大的,相當於給redo logfile做一個備份

alter database add logfile
('/u01/app/oracle/oradata/orcl/redo09a.log')
size 100M;

查詢輸出:

SQL> select group#, sequence#, members, bytes, status, archived
from v$log;  2  

    GROUP#  SEQUENCE#	 MEMBERS   BYTES STATUS	    	 ARC
---------- ------------ --------- ------ ---------- -------
	 1	  140	   		    2  104857600 CURRENT	     NO
	 2	  133	      		2  104857600 INACTIVE	     NO
	 3	  137	   		    2  104857600 INACTIVE	     NO
	 4	  134	     	    2  104857600 INACTIVE	     NO
	 5	  135	     	    2  104857600 INACTIVE	     NO
	 6	  136	      	    2  104857600 INACTIVE	     NO
	 7	  138	       		2  104857600 INACTIVE	     NO
	 8	  139	      	    2  104857600 INACTIVE	     NO
	 9	    0	            1  104857600 UNUSED	     	YES

9 rows selected.

SQL> 

剛剛新增的磁碟組狀態為UNUSED,切換redo log即可使用
注意:在“CURRENT”狀態的重組日誌組不可修改,只有將CURRENT切換到其他組時才可以新增、刪除redo log。

切換redo log命令:

alter system switch logfile;
三、新增redo logfile

在剛剛建好的第9組redo log中,再加入一個redo logfile(成員)

ALTER DATABASE ADD LOGFILE MEMBER '新建立redo logfile名路徑' TO GROUP 組號;

示例:

alter database add logfile member '/u01/app/oracle/oradata/orcl/redo09b.log' to group 9;

查詢輸出:

SQL> select group#, sequence#, members, bytes, status, archived
from v$log;  2  

    GROUP#  SEQUENCE#	 MEMBERS      BYTES STATUS	     ARC
---------- ---------- ---------- ---------- ---------------- ---
	 1	  140	       2  104857600 CURRENT	     NO
	 2	  133	       2  104857600 INACTIVE	     NO
	 3	  137	       2  104857600 INACTIVE	     NO
	 4	  134	       2  104857600 INACTIVE	     NO
	 5	  135	       2  104857600 INACTIVE	     NO
	 6	  136	       2  104857600 INACTIVE	     NO
	 7	  138	       2  104857600 INACTIVE	     NO
	 8	  139	       2  104857600 INACTIVE	     NO
	 9	    0	       2  104857600 UNUSED	     YES

9 rows selected.

SQL> 

查詢結果可以看到MEMBERS已經變成了2,這說明該組中有兩個redo logfile(成員),使用v$logfile檢視更直觀一些
查詢輸出:

SQL> select * from v$logfile;

    GROUP# STATUS  TYPE    MEMBER					                     IS_
---------- ------- ------- --------------------------------------------- ---
	 2	  			ONLINE  /u01/app/oracle/oradata/orcl/redo02a.log	 NO
	 1	   			ONLINE  /u01/app/oracle/oradata/orcl/redo01a.log	 NO
	 3	  			ONLINE  /u01/app/oracle/oradata/orcl/redo03a.log	 NO
	 2	   			ONLINE  /u01/app/oracle/oradata/orcl/redo02b.log	 NO
	 1	   			ONLINE  /u01/app/oracle/oradata/orcl/redo01b.log	 NO
	 4	   			ONLINE  /u01/app/oracle/oradata/orcl/redo04a.log	 NO
	 4	  			ONLINE  /u01/app/oracle/oradata/orcl/redo04b.log	 NO
	 5	   			ONLINE  /u01/app/oracle/oradata/orcl/redo05a.log	 NO
	 5	   			ONLINE  /u01/app/oracle/oradata/orcl/redo05b.log	 NO
	 6	  			ONLINE  /u01/app/oracle/oradata/orcl/redo06a.log	 NO
	 6	   			ONLINE  /u01/app/oracle/oradata/orcl/redo06b.log	 NO

    GROUP# STATUS  TYPE    MEMBER					                     IS_
---------- ------- ------- --------------------------------------------- ---
	 7	  			ONLINE  /u01/app/oracle/oradata/orcl/redo07a.log	 NO
	 7	   			ONLINE  /u01/app/oracle/oradata/orcl/redo07b.log	 NO
	 8	   			ONLINE  /u01/app/oracle/oradata/orcl/redo08a.log	 NO
	 8	   			ONLINE  /u01/app/oracle/oradata/orcl/redo08b.log	 NO
	 3	   			ONLINE  /u01/app/oracle/oradata/orcl/redo03b.log	 NO
	 9	   			ONLINE  /u01/app/oracle/oradata/orcl/redo09a.log	 NO
	 9 	   INVALID  ONLINE  /u01/app/oracle/oradata/orcl/redo09b.log	 NO

18 rows selected.

SQL> 

剛剛新增的redo09b.log檔案的狀態INVALID,日誌檔案組為UNUSED狀態,切換redo log即可

四、刪除redo logfile

在redo log 處於不活動的狀態時,使用以下命令刪除redo logfile

ALTER DATABASE DROP LOGFILE MEMBER'redo logfile路徑名';

注意:這只是在資料庫中刪除了redo logfile,但redo logfile依然存在於儲存中,需手動將儲存中redo logfile刪除。即徹底刪除。

示例:刪除第9組redo log 中的redo logfile

alter database drop logfile member'/u01/app/oracle/oradata/orcl/redo09b.log';

查詢輸出:

SQL> select group#, sequence#, members, bytes, status, archived
from v$log;
  2  
    GROUP#  SEQUENCE#	 MEMBERS      BYTES STATUS	     ARC
---------- ---------- ---------- ---------- ---------------- ---
	 1	  140	       2  104857600 CURRENT	    	 NO
	 2	  133	       2  104857600 INACTIVE	     NO
	 3	  137	       2  104857600 INACTIVE	     NO
	 4	  134	       2  104857600 INACTIVE	     NO
	 5	  135	       2  104857600 INACTIVE	     NO
	 6	  136	       2  104857600 INACTIVE	     NO
	 7	  138	       2  104857600 INACTIVE	     NO
	 8	  139	       2  104857600 INACTIVE	     NO
	 9	    0	       1  104857600 UNUSED	     	YES

9 rows selected.

SQL> 

可以看到第9組的redo logfile只剩下了一個

-rw-r-----  1 oracle oinstall 104858112 Dec 18 16:13 redo09a.log
-rw-r-----  1 oracle oinstall 104858112 Dec 18 16:23 redo09b.log

在儲存中依然可以看到redo09b.log,使用“rm -rf redo09b.log” 徹底刪除

五、刪除redo log

在redo log處於不活躍的狀態時使用下面命令刪除

ALTER DATABASE DROP LOGFILE GROUP 組號;

示例輸出:

SQL> alter database drop logfile group 9;

Database altered.

SQL> select group#, sequence#, members, bytes, status, archived
from v$log;  2  

    GROUP#  SEQUENCE# MEMBERS  BYTES    STATUS	     ARC
---------- ---------- ------- -------- ---------- ------
	 1	  140	       2  	 104857600 CURRENT	  	 NO
	 2	  133	       2  	 104857600 INACTIVE	     NO
	 3	  137	       2  	 104857600 INACTIVE	     NO
	 4	  134	       2  	 104857600 INACTIVE	     NO
	 5	  135	       2 	 104857600 INACTIVE	     NO
	 6	  136	       2 	 104857600 INACTIVE	     NO
	 7	  138	       2 	 104857600 INACTIVE	     NO
	 8	  139	       2  	 104857600 INACTIVE	     NO

8 rows selected.

SQL>

若刪除報錯,可能是redo log處於活躍狀態,
使用 alter system switch logfile; 切換redo log組,或只能等待歸檔完畢才可刪除。

至此結束,謝謝。

相關文章