db2 事務日誌和歸檔的管理

dl_lang發表於2011-08-23
參考了,很多同學的文件,主要是:[DB2]歸檔日誌實驗:線上與離線,DB2_體系架構_db2歸檔模式,Prayer 的清理DB2的歸檔日誌。

算是給對自己在DB2歸檔方面學習作一個總結,也給自己留一點資料。

db2 事務日誌和歸檔的管理

 

1DB2的日誌模式

 

1.1迴圈日誌

   當迴圈日誌生效時,事務資料將透過迴圈的方式寫入主要日誌檔案。當儲存於某個日誌檔案中的所有記錄都不再需要用於恢復時,該日誌檔案將被重用,並且可以 在以後再次成為活動日誌檔案。這意味著在迴圈日誌模式中,日誌檔案的內容最終將被新日誌條目重寫。由於日誌檔案的內容被重寫覆蓋了,因此我們只能將資料庫 恢復到最後一次完整的資料庫備份。不能使用迴圈日誌執行時間點(point-in-time)恢復。

1.2歸檔日誌

   在歸檔日誌模式中,redo log 條目將寫入主要日誌檔案。但是,與迴圈日誌不同,這些日誌檔案永遠都不可重用。當儲存於某個日誌檔案中的所有記錄都不再需要用於恢復時,該日誌檔案將被標 記為非活動 而不是可重用。這意味著它的內容永遠都不會被覆蓋。當第一個主要日誌檔案變滿時,系統將分配一個新的日誌檔案,這樣主要日誌檔案的配置數量 (LOGPRIMARY 資料庫引數)將一直可用。

  與單 個事務相關的所有條目必須在活動日誌空間中保持一致。如果長時間執行的事務所需要的日誌空間大於主要日誌檔案可以提供的空間,則可能會分配並使用次要日誌 檔案。在歸檔日誌模式中,透過結合使用資料庫備份映像和日誌檔案,我們可以將資料庫恢復到具體的時間點。有關此流程的詳細描述請參見下文。

   設定了歸檔模式後,資料庫將支援前滾恢復。此時,系統中將會存在三種型別的日誌檔案:

  活動日誌:該日誌包含尚未提交或回滾的事務單元的相關資訊,以及已提交但尚未寫入資料庫檔案的事務的資訊。

  聯機存檔日誌:活動日誌中所有改動對正常處理已不需要,即該日誌中所記錄的事務都已提交併寫入資料庫檔案時,該活動日誌轉換為聯機存檔日誌。稱之為聯機,是由於它們與活動日誌存放在同一個目錄下。

  離線存檔日誌:將聯機存檔日誌從活動日誌目錄下Copy到另外的地方存檔,就稱為離線存檔日誌。這些日誌可能在資料庫前滾恢復的時候仍然需要。

(所謂聯機歸檔日誌,就是歸檔日誌檔案和活動日誌檔案同在一個目錄下;而離線歸檔日誌,則是歸檔日誌與活動日誌分開存放)

 

2. 聯機歸檔

 

預設建立資料庫是迴圈日誌模式

為的是演示日誌能夠迅速寫滿,然後立刻去寫下一個日誌。

 C:\Documents and Settings\Administrator>  update db cfg for arc using LOGFILSIZ 100

 提示需要斷開資料庫的連線:

 C:\Documents and Settings\Administrator>db2  disconnect arc

C:\Documents and Settings\Administrator>db2 connect to arc

   Database Connection Information

 Database server        = DB2/NT 9.7.0

 SQL authorization ID   = TONY

 Local database alias   = ARC

 

2.1、修改主日誌數和輔日誌數:

db2 update db cfg for arc using logprimary 3

db2 update db cfg for arc using logsecond 2

 

2.2、啟用歸檔模式,型別為聯機歸檔

  將資料庫改為歸檔模式下的聯機歸檔日誌模式,只需將資料庫引數,更改之前的系統顯示

C:\Documents and Settings\Administrator>db2 get db cfg for arc |grep LOG

 Catalog cache size (4KB)              (CATALOGCACHE_SZ) = 300

 Log buffer size (4KB)                        (LOGBUFSZ) = 256

 Log file size (4KB)                         (LOGFILSIZ) = 100

 Number of primary log files                (LOGPRIMARY) = 3

 Number of secondary log files               (LOGSECOND) = 2

 Changed path to log files                  (NEWLOGPATH) =

 Path to log files                                       = C:\DB2\arc\DB2\NODE0000\SQL00001\SQLOGDIR\

 Overflow log path                     (OVERFLOWLOGPATH) =

 Mirror log path                         (MIRRORLOGPATH) =

 First active log file                                   = S0000039.LOG

 Block log on disk full                (BLK_LOG_DSK_FUL) = NO

 Block non logged operations            (BLOCKNONLOGGED) = NO

 Percent max primary log space by transaction  (MAX_LOG) = 0

 Num. of active log files for 1 active UOW(NUM_LOG_SPAN) = 0

 Log retain for recovery enabled             (LOGRETAIN) = OFF

 First log archive method                 (LOGARCHMETH1) = OFF

 Options for logarchmeth1                  (LOGARCHOPT1) =

 Second log archive method                (LOGARCHMETH2) = OFF

 Options for logarchmeth2                  (LOGARCHOPT2) =

 Log pages during index build            (LOGINDEXBUILD) = OFF

 

LOGARCHMETH1設定為logretain

db2 update db cfg for arc using LOGARCHMETH1 logretain

 

設定LOGARCHMETH1引數,此引數可取值如下

  a.OFF      :表示非歸檔

  b.LOGRETAIN:等價於將 LOGRETAIN 配置引數設定為 RECOVERY,如果指定此值,將自動更新LOGRETAIN引數

  c.USEREXIT :且等價於將 USEREXIT 配置引數設定為 ON,如果指定此值,將自動更新USEREXIT引數I

  d.DISK     :日誌檔案將在其中歸檔,

  e.TSM      :將日誌檔案歸檔在本地 TSM 伺服器上

f.VENDOR   :指定將使用供應商庫來歸檔日誌檔案。此值後必須緊跟冒號(:)和庫的名稱

 

修改之後的引數:

C:\Documents and Settings\Administrator>db2 get db cfg for arc |grep LOG

 Catalog cache size (4KB)              (CATALOGCACHE_SZ) = 300

 Log buffer size (4KB)                        (LOGBUFSZ) = 256

 Log file size (4KB)                         (LOGFILSIZ) = 100

 Number of primary log files                (LOGPRIMARY) = 3

 Number of secondary log files               (LOGSECOND) = 2

 Changed path to log files                  (NEWLOGPATH) =

 Path to log files                                       = C:\DB2\arc\DB2\NODE0000\SQL00001\SQLOGDIR\

 Overflow log path                     (OVERFLOWLOGPATH) =

 Mirror log path                         (MIRRORLOGPATH) =

 First active log file                                   = S0000039.LOG

 Block log on disk full                (BLK_LOG_DSK_FUL) = NO

 Block non logged operations            (BLOCKNONLOGGED) = NO

 Percent max primary log space by transaction  (MAX_LOG) = 0

 Num. of active log files for 1 active UOW(NUM_LOG_SPAN) = 0

 Log retain for recovery enabled             (LOGRETAIN) = RECOVERY

 First log archive method                 (LOGARCHMETH1) = LOGRETAIN

 Options for logarchmeth1                  (LOGARCHOPT1) =

 Second log archive method                (LOGARCHMETH2) = OFF

 Options for logarchmeth2                  (LOGARCHOPT2) =

 Log pages during index build            (LOGINDEXBUILD) = OFF

 

 

C:\Documents and Settings\Administrator>db2  disconnect arc

C:\Documents and Settings\Administrator>db2 connect to arc

SQL1116N A connection to or activation of database "arc" cannot be made

because of BACKUP PENDING. SQLSTATE=57019

C:\Documents and Settings\Administrator>db2 backup db arc to C:\DB2\backup

C:\Documents and Settings\Administrator>db2 connect to arc

 

2.3檢視活動日誌檔案和禁用自動commit

 

2.3.1、檢視活動日誌使用如下命令:

db2pd -db arc -logs

 

2.3.2、禁止自動commit

        db2預設是自動提交的,本例中我們將這個給禁止掉。首先檢視下auto-commit的情況,發現時開啟的。

db2 list command options

    Command Line Processor Option Settings

 

 Backend process wait time (seconds)       (DB2BQTIME) = 1

 No. of retries to connect to backend       (DB2BQTRY) = 60

 Request queue wait time (seconds)         (DB2RQTIME) = 5

 Input queue wait time (seconds)           (DB2IQTIME) = 5

 Command options                          (DB2OPTIONS) =

 

 Option Description                              Current Setting

 ------ ---------------------------------------- ---------------

  -a   Display SQLCA                            OFF

  -c   Auto-Commit                              ON

  -d   Retrieve and display declarations    OFF

  -e   Display SQLCODE/SQLSTATE                 OFF

  -f   Read from input file                     OFF

  -i   Display XML with indentation        OFF

  -l   Log commands in history file             OFF

  -m   Display the number of rows affected      OFF

  -n   Remove new line character                OFF

  -o   Display output                           ON

  -p   Display interactive input prompt         ON

  -q   Preserve whitespaces & linefeeds         OFF

  -r   Save output to report file               OFF

  -s   Stop execution on command error          OFF

  -t   Set statement termination character      OFF

  -v   Echo current command                     OFF

  -w   Display FETCH/SELECT warning messages    ON

  -x   Suppress printing of column headings     OFF

  -z   Save all output to output file           OFF

        commandc選項(即auto-commit)關閉即可:

db2 update command options using c off

DB20000I The UPDATE COMMAND OPTIONS command completed successfully.

 

插入資料,檢視日誌檔案的變化

Db2 create table test( name varchar(200)

Db2 insert into test values(‘aaaaaaa’)

Db2 insert into test select * from test

反覆執行,直到出現

SQL0964C The transaction log for the database is full. SQLSTATE=57011

 提示,日誌已滿。由於我們沒有提交事務,而活動日誌都已經寫滿了,無法進行歸檔,所以錯。

我們現在看下,活動日誌的情況,發現總數是5個。由於日誌是先寫3個主日誌,再寫2個從日誌的。

 

 

下面減少插入的記錄數,檢視下活動日誌的情況,發現從變為了4~7。於此同時,也發生了日誌的歸檔。

db2 insert into test1 select * from test1 fetch first 5000 rows only

DB20000I The SQL command completed successfully.

db2 commit

DB20000I The SQL command completed successfully.

 

C:\Documents and Settings\Administrator>db2 list history archive log all for arc

C:\Documents and Settings\Administrator>db2 list history backup all for arc

C:\Documents and Settings\Administrator>db2 terminate

C:\Documents and Settings\Administrator>db2 backup db arc to C:\DB2\backup

C:\Documents and Settings\Administrator>db2 connect to arc

C:\Documents and Settings\Administrator>db2pd -db arc -logs

C:\Documents and Settings\Administrator>db2 list history archive log all for arc

C:\Documents and Settings\Administrator>db2 list backup all for arc

 

C:\Documents and Settings\Administrator>db2 prune history 20110823105033 and delete

DB20000I  The PRUNE command completed successfully.

 

**

可以使用force選項:

db2 prune history 20110823105033 with force option and delete

**

 

雖然提示成功,但是歸檔檔案還是存在,對於聯機歸檔情況,delete不能刪除檔案。

C:\Documents and Settings\Administrator>db2 get db cfg for arc | grep First

 First active log file                                   = S0000007.LOG

 First log archive method                 (LOGARCHMETH1) = LOGRETAIN

C:\Documents and Settings\Administrator>db2 prune logfile prior to S0000007.LOG

DB20000I  The PRUNE command completed successfully.

 

現在07之前的歸檔檔案,全部刪除。

 

3         離線歸檔實驗

 

 

C:\Documents and Settings\Administrator>db2 update db cfg for arc using logarchmeth1 disk:C:\DB2\arc\archive

 

 

db2 insert into test1 select * from test1 fetch first 5000 rows only

可以在目錄C:\DB2\arc\archive\DB2\ARC\NODE0000\C0000000下看到,產生的離線歸檔檔案。

 

C:\Documents and Settings\Administrator>db2 list history archive log all for arc

可以看到產生的歸檔檔案資訊

C:\Documents and Settings\Administrator>db2 list history archive log all for arc

C:\Documents and Settings\Administrator>db2 connect to arc

C:\Documents and Settings\Administrator>db2 prune history 20110823132450 and delete

DB20000I  The PRUNE command completed successfully.

 

這個時候,離線的歸檔檔案被刪除了。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6126/viewspace-705625/,如需轉載,請註明出處,否則將追究法律責任。

相關文章