Db2 日常实用操作积累-部分

来源:百度文库 编辑:神马文学网 时间:2024/05/01 14:58:08
DB2数据库HNEitug
1、Load 方法装入数据:HNEitug
export to tempfile of del select * from TABLENAME where not 清理条件;HNEitug
load from tempfile of del modified by delprioritychar replace into TABLENAME nonrecoverable;HNEitug
说明: HNEitug
在不相关的数据表export数据时,可以采取并发的形式,以提高效率;HNEitug
TABLENAME指待清理table的名称;HNEitug
modified by delprioritychar防止数据库记录中存在换行符,导致数据无法装入的情况;HNEitug
replace into对现数据库中的内容进行替换,即将现行的数据记录清理,替换为数据文件内容;HNEitug
nonrecoverable无日志方式装入;HNEitug
HNEitug
2、查找当前的应用:HNEitug
db2 list application |grep BTPDBS;HNEitug
HNEitug
3、删除当前正在使用的application:HNEitug
db2 "force application (Id1,Id2,Id3)"HNEitug
Id1,Id2,Id3 是List显示的应用号;HNEitug
HNEitug
4、查看当前应用号的执行状态:HNEitug
db2 get snapshot for application agentid 299 |grep RowHNEitug
HNEitug
5、查看数据库参数:HNEitug
db2 get db cfg for   //当前数据库可以省略HNEitug
6、修改数据库的Log数据:HNEitug
db2 update db cfg using <参数名> <参数值>HNEitug
HNEitug
7、Db2Stop Force的用法:HNEitug
在进行Bind的时候出现如下错误:HNEitug
SQL0082C  An error has occurred which has terminated processing.HNEitug
SQL0092N  No package was created because of previous errors.HNEitug
SQL0091N  Binding was ended with "3" errors and "0" warnings.HNEitug
主要是表文件被加锁,不能继续使用;HNEitug
在进行stop的时候报错:db2stopHNEitug
8/03/2005 21:46:53  0 0   SQL1025N  The database manager was not stopped because databases are still active.HNEitug
SQL1025N  The database manager was not stopped because databases are still active.HNEitug
需要使用如下命令可以解决这个问题: db2stop forceHNEitug
08/03/2005 21:47:49     0   0   SQL1064N  DB2STOP processing was successful.HNEitug
SQL1064N  DB2STOP processing was successful.HNEitug
然后启动数据库db2start,连接数据库db2s后,重新进行bind即可。HNEitug
HNEitug
8、缓冲池参数修改:HNEitug
db2 alter bufferpool ibmdefaultbp size 10240HNEitug
查看本表的数据内容如下:HNEitug
db2 "select * from syscat.bufferpools";  HNEitug
HNEitug
9、DB2 日志处理:HNEitug
DB2日志是以文件的形式存放在文件系统中,分为两种模式:循环日志和归档日志。当创建新数据库时,日志的缺省模式是循环日志。在这种模式下,只能实现数据库的脱机备份和恢复。如果要实现联机备份和恢复,必须设为归档日志模式。 目前在综合业务系统中,设置的均是归档日志模式;其它系统(如事后监督、经营决策、中间业务等)一般都设置为循环日志模式。至于采用何种模式,可以通过修改数据库配置参数(LOGRETAIN)来实现: 归档日志模式:db2 update db cfg for using logretain on 注:改为on后,查看数据库配置参数logretain的值时,实际显示的是recovery。改变此参数后,再次连接数据库会显示数据库处于备份暂挂(BACKUP PENDING)状态。这时,需要做一次对数据库的脱机备份(db2 backup db ),才能使数据库状态变为正常。 循环日志模式:db2 update db cfg for using logretain offHNEitug
HNEitug
10、Db2 日志处理:HNEitug
必须按照以下正确的步骤进行操作: 􀁺 要求必须使用DB2命令PRUNE进行清理,不建议使用rm命令删除。 􀁺 删除前应保证应用已停止(即联机已下来)。 􀁺 查看当前使用的日志文件目录及第一活动日志文件 􀂾 用 “db2 get db cfg for ”命令查看日志文件目录(Path to log files)参数,确定数据库当前使用的日志文件目录。 例如:Path to log files = /db2log/,说明DB2日志存放目录是/db2log 􀂾 用 “db2 get db cfg for ”命令查看第一活动日志文件(First active log file)参数,该参数对应的日志文件之前的日志文件均为归档日志文件,如果确认没有用,可以删除。 例如:First active log file = S0015913.LOG,说明当前第一活动日志文件是S0015913.LOG。 􀁺 备份好要删除的归档日志 􀁺 删除归档日志 以应用用户(如BTP)登录,执行:HNEitug
$ db2 connect to HNEitug
$ db2 prune logfile prior to S???????.LOGHNEitug
注:S???????.LOG为查看到的第一活动日志文件。此命令可以将当前第一活动日志文件之前的归档日志文件全部删除。HNEitug
HNEitug
11、如何清理db2diag.log文件 HNEitug
db2diag.log,是用来记录DB2数据库运行中的信息的文件。可以通过此文件,查看记录的有关DB2数据库详细的错误信息。此文件也是不断增大的,需要定期进行清理。 可以通过查看实例的配置参数DIAGPATH,来确定db2diag.log文件是放在哪个目录下:db2 get dbm cfg 如果Diagnostic data directory path(DIAGPATH) = /home/db2inst1/sqllib/db2dump,则此文件是放在/home/db2inst1/sqllib/db2dump目录下。当文件系统/home的使用率达到80%-90%左右时,应及时删除db2diag.log文件。 请按以下正确步骤操作: 􀁺 确认应用(如BTP)、DB2已经停止。 􀁺 将原db2diag.log文件备份到其它文件系统下。 􀁺 删除db2diag.log文件。删除后,DB2会自动创建一个新的文件。HNEitug
HNEitug
12、Load 操作HNEitug
在进行load的时候HNEitug
db2 "load from acmmst.txt of del modified by coldel| replace into acmmst nonrecoverable ”HNEitug
由于数据不规范出现错误,强行中断以后,进行操作的时候出现如下错误:HNEitug
SQL0668N  Operation not allowed for reason code "3" on table "BTP.ACMMST".HNEitug
SQLSTATE=57016HNEitug
此时,进行反方向操作即可:HNEitug
db2 "load from /dev/null of del terminate into acmmst nonrecoverable"。HNEitug
HNEitug
如果没有使用参数nonrecoverable,则会出现数据库状态不正确的情况,使用:HNEitug
db2 list tablesapces show detail 查看状态,如果不是正常状态,则脱机状态进行备份即可。HNEitug
HNEitug
两个表文件之间UPDATE的方法:HNEitug
db2 "update cdmcrd set offset = (select cdmlsl.offset from cdmlsl where cdmlsl.crdno=cdmcrd.crdno) where cdmcrd.crdno in (select cdmlsl.crdno from cdmlsl)HNEitug
HNEitug
HNEitug
13、多字段条件查询和修改:HNEitug
表A中的字段有actno, cnlno,bal,pwd;表B中的字段为Actno,Cnlno,TxnAmt;目的是将A表中的bal修改为B表中的TxnAmt,命令:HNEitug
db2 "update A set bal=(select txnamt from B where actno=A.actno and cnlno=A.Cnlno) where A.actno||A.cnlno in (select Actno||cnlno from B );HNEitug
HNEitug
14、多条件匹配查询HNEitug
查询某个表中条件是B?AAA的记录:HNEitug
db2 "select * from A where actno like 'B_AAA%'".HNEitug
查询数据中存在某些字符的记录:HNEitug
db2 "select * from A where actno like '%-AAA%".HNEitug
HNEitug
15/数据库恢复的处理HNEitug
进行数据库恢复的时候使用以下的命令:HNEitug
restore db db1 to /tstdb2/catalog  into db newlogpath /tstdb2/db2log buffer 2048HNEitug
replace existing redirect parallelism  16;HNEitug
set tablespace containers for 1  using  (path '/tstdb2/db2tmp');HNEitug
set tablespace containers for 2  usingHNEitug
(device '/dev/rtstcontlv00' 2621440, device '/dev/rtstcontlv01' 2621440,HNEitug
device '/dev/rtstcontlv02' 2621440, device '/dev/rtstcontlv03' 2621440 ) ;HNEitug
restore db db1 continue;HNEitug
HNEitug
恢复完成以后执行命令db2s时报如下的错误:HNEitug
P570:>db2sHNEitug
SQL1117N  A connection to or activation of database "DB" cannot be madeHNEitug
because of ROLL-FORWARD PENDING.  SQLSTATE=57019HNEitug
DB21034E  The command was processed as an SQL statement because it was not aHNEitug
valid Command Line Processor command.  During SQL processing it returned:HNEitug
SQL1024N  A database connection does not exist.  SQLSTATE=08003HNEitug
解决办法如下:HNEitug
P570:>db2 rollforward db db to end of logs and completeHNEitug
HNEitug
Rollforward StatusHNEitug
HNEitug
Input database alias                   = dbHNEitug
Number of nodes have returned status   = 1HNEitug
HNEitug
Node number                            = 0HNEitug
Rollforward status                     = not pendingHNEitug
Next log file to be read               =HNEitug
Log files processed                    =  -HNEitug
Last committed transaction             = 2005-11-20-10.59.23.000000HNEitug
HNEitug
DB20000I  The ROLLFORWARD command completed successfully.