db2事务日志和归档的管理
1.DB2的日志模式
1.1循环日志
当循环日志生效时,事务数据将通过循环的方式写入主要日志文件。当存储于某个日志文件中的所有记录都不再需要用于恢复时,该日志文件将被重用,并且可以在以后再次成为活动日志文件。这意味着在循环日志模式中,日志文件的内容最终将被新日志条目重写。由于日志文件的内容被重写覆盖了,因此我们只能将数据库恢复到最后一次完整的数据库备份。不能使用循环日志执行时间点(point-in-time)恢复。
1.2归档日志
在归档日志模式中,redo log条目将写入主要日志文件。但是,与循环日志不同,这些日志文件永远都不可重用。当存储于某个日志文件中的所有记录都不再需要用于恢复时,该日志文件将被标记为非活动而不是可重用。这意味着它的内容永远都不会被覆盖。当第一个主要日志文件变满时,系统将分配一个新的日志文件,这样主要日志文件的配置数量(LOGPRIMARY数据库参数)将一直可用。
与单个事务相关的所有条目必须在活动日志空间中保持一致。如果长时间运行的事务所需要的日志空间大于主要日志文件可以提供的空间,则可能会分配并使用次要日志文件。在归档日志模式中,通过结合使用数据库备份映像和日志文件,我们可以将数据库恢复到具体的时间点。有关此流程的详细描述请参见下文。
设置了归档模式后,数据库将支持前滚恢复。此时,系统中将会存在三种类型的日志文件:
活动日志:该日志包含尚未提交或回滚的事务单元的相关信息,以及已提交但尚未写入数据库文件的事务的信息。
联机存档日志:活动日志中所有改动对正常处理已不需要,即该日志中所记录的事务都已提交并写入数据库文件时,该活动日志转换为联机存档日志。称之为联机,是由于它们与活动日志存放在同一个目录下。
脱机存档日志:将联机存档日志从活动日志目录下Copy到另外的地方存档,就称为脱机存档日志。这些日志可能在数据库前滚恢复的时候仍然需要。
(所谓联机归档日志,就是归档日志文件和活动日志文件同在一个目录下;而离线归档日志,则是归档日志与活动日志分开存放)
2.联机归档
缺省创建数据库是循环日志模式
为的是演示日志能够迅速写满,然后立刻去写下一个日志。
C:\Documents and Settings\Administrator>db2update db cfg for arc using LOGFILSIZ 100
提示需要断开数据库的连接:
C:\Documents and Settings\Administrator>db2disconnect arc
C:\Documents and Settings\Administrator>db2 connect to arc
Database Connection Information
Databaseserver= DB2/NT 9.7.0
SQLauthorization 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 duringindexbuild(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>db2disconnect arc
C:\Documents and Settings\Administrator>db2 connect to arc
SQL1116NA 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) =
OptionDescriptionCurrent Setting
-------------------------------------------------------------
-aDisplay SQLCAOFF
-cAuto-CommitON
-dRetrieve and displayXMLdeclarationsOFF
-eDisplay SQLCODE/SQLSTATEOFF
-fRead from input fileOFF
-iDisplay XMLdatawith indentationOFF
-lLog commands in history fileOFF
-mDisplay the number of rows affectedOFF
-nRemove new line characterOFF
-oDisplay outputON
-pDisplay interactive input promptON
-qPreserve whitespaces & linefeedsOFF
-rSave output to report fileOFF
-sStop execution on command errorOFF
-tSet statement termination characterOFF
-vEcho current commandOFF
-wDisplay FETCH/SELECT warning messagesON
-xSuppress printing of column headingsOFF
-zSave all output to output fileOFF
将command的c选项(即auto-commit)关闭即可:
db2 update command options using c off
DB20000IThe UPDATE COMMAND OPTIONS command completed successfully.
插入数据,查看日志文件的变化
Db2 createtabletest( name varchar(200)
Db2 insert into test values(‘aaaaaaa’)
Db2 insert into test select * from test
反复执行,直到出现
SQL0964CThe transaction log for the database is full.SQLSTATE=57011
提示,日志已满。由于我们没有提交事务,而活动日志都已经写满了,无法进行归档,所以错。
我们现在看下,活动日志的情况,发现总数是5个。由于日志是先写3个主日志,再写2个从日志的。
下面减少插入的记录数,查看下活动日志的情况,发现从变为了4~7。于此同时,也发生了日志的归档。
db2 insert into test1 select * from test1fetchfirst 5000 rows only
DB20000IThe SQL command completed successfully.
db2 commit
DB20000IThe 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
DB20000IThe PRUNE command completed successfully.
**
可以使用force选项:
db2 prune history 20110823105033with force optionand 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
DB20000IThe 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 test1fetchfirst 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
DB20000IThe PRUNE command completed successfully.
这个时候,离线的归档文件被删除了。