运维管理13 分钟阅读
IBM Db2 运维命令 100 条
IBM Db2 是企业级关系型数据库,广泛用于金融、制造和核心业务系统。Db2 LUW 的管理体系由实例、数据库、表空间、缓冲池、日志、实用程序和高可用组件组成,很多操作既可以通过 CLP 命令完成,也可以使用 SQL 管理视图和 db2pd 等诊断工具。
2026年8月19日阅读—点赞—收藏—
dba100db2墨力计划
在知识库中专注阅读,并随时返回相关工具与课程
IBM Db2 是企业级关系型数据库,广泛用于金融、制造和核心业务系统。Db2 LUW 的管理体系由实例、数据库、表空间、缓冲池、日志、实用程序和高可用组件组成,很多操作既可以通过 CLP 命令完成,也可以使用 SQL 管理视图和 db2pd 等诊断工具。
IBM Db2 是企业级关系型数据库,广泛用于金融、制造和核心业务系统。Db2 LUW 的管理体系由实例、数据库、表空间、缓冲池、日志、实用程序和高可用组件组成,很多操作既可以通过 CLP 命令完成,也可以使用 SQL 管理视图和 db2pd 等诊断工具。
下面整理 IBM Db2 DBA 常用的 100 条命令,主要面向 Db2 12.1 for Linux、UNIX and Windows,同时兼顾仍大量运行的 Db2 11.5。Db2 for z/OS、Db2 for i 和 Db2 Warehouse 不在本文范围内。
文中的实例、数据库、用户、目录和表名均为示例。停实例、强制断开应用、修改日志参数、恢复数据库、前滚和 HADR 接管等操作,应先确认影响范围并准备回退方案。
1db2level1db2ls1db2ilist1db2 get instance1export DB2INSTANCE=db2inst11db2 get dbm cfg1db2set -all1db2start1db2stop1db2stop force强制停止会断开应用并中止事务,仅在正常停止无法完成且影响已确认时使用。
1db2 list db directory1db2 list active databases1db2 create database APPDB using codeset UTF-8 territory CN1db2 connect to APPDB user db2inst1 using 'Password'1db2 get connection state1db2 connect reset1db2 get db cfg for APPDB1db2 "call get_dbsize_info(?, ?, ?, 0)"返回数据库容量和容量时间戳等信息。
1db2 "select dbpartitionnum, type, path2 from table(admin_list_db_paths()) as t"1db2 drop database APPDB删除不可逆,必须先确认备份和目标数据库名称。
1db2 "select schemaname, owner, create_time2 from syscat.schemata3 order by schemaname"1db2 "select tabschema, tabname, type, tableorg2 from syscat.tables3 where tabschema='APP'4 order by tabname"1db2 describe table APP.ORDERS1db2 "select colno, colname, typename, length,2 scale, nulls3 from syscat.columns4 where tabschema='APP' and tabname='ORDERS'5 order by colno"1db2 "select indschema, indname, uniquerule,2 indexType, colnames3 from syscat.indexes4 where tabschema='APP' and tabname='ORDERS'5 order by indname"1db2 "create table APP.ORDERS (2 ORDER_ID bigint not null primary key,3 CUSTOMER_ID bigint not null,4 ORDER_TIME timestamp,5 AMOUNT decimal(18,2),6 STATUS varchar(20)7 )"1db2 "alter table APP.ORDERS2 add column REMARK varchar(500)"1db2 "create index APP.IDX_ORDERS_CUSTOMER2 on APP.ORDERS(CUSTOMER_ID)"1db2 "select tabschema, tabname, constname, type,2 enforced3 from syscat.tabconst4 where tabschema='APP'5 order by tabname, constname"1db2look -d APPDB -e -z APP -o app_ddl.sql1db2 list tablespaces show detail1db2 list tablespace containers for 3 show detail1db2 "select tbsp_name, tbsp_type, tbsp_state,2 tbsp_total_size_kb, tbsp_used_size_kb,3 tbsp_free_size_kb4 from table(mon_get_tablespace('', -2)) as t5 order by tbsp_name"1db2 "create large tablespace APP_DATA2 pagesize 32K3 managed by automatic storage4 autoresize yes5 initialsize 1G6 increasesize 512M7 maxsize none"1db2 "alter tablespace APP_DATA2 autoresize yes3 increasesize 1G4 maxsize 500G"1db2 "select tabschema, tabname,2 data_object_p_size,3 index_object_p_size,4 long_object_p_size5 from sysibmadm.admintabinfo6 where tabschema='APP'7 order by data_object_p_size desc"1db2 "select bpname, pagesize, npages,2 numblockpages, blocksize3 from syscat.bufferpools"1db2 "select bp_name,2 pool_data_l_reads,3 pool_data_p_reads,4 pool_index_l_reads,5 pool_index_p_reads6 from table(mon_get_bufferpool('', -2)) as t"1db2 "alter bufferpool IBMDEFAULTBP size 100000"1db2pd -db APPDB -storagepaths1db2 list applications show detail1db2pd -db APPDB -dbcfg1db2 "select application_handle,2 session_auth_id,3 client_hostname,4 application_name,5 connection_start_time6 from table(mon_get_connection(NULL, -2)) as t7 order by connection_start_time"1db2 "select application_handle,2 elapsed_time_sec,3 activity_state,4 stmt_text5 from sysibmadm.mon_current_sql6 order by elapsed_time_sec desc"1db2 "select num_executions,2 total_act_time,3 total_cpu_time,4 rows_read,5 stmt_text6 from table(mon_get_pkg_cache_stmt(NULL, NULL, NULL, -2)) as t7 order by total_act_time desc8 fetch first 20 rows only"1db2pd -db APPDB -locks showlocks1db2 "select hld_application_handle as holder,2 req_application_handle as waiter,3 lock_object_type,4 tabname,5 lock_mode,6 lock_status7 from table(mon_get_appl_lockwait(NULL, -2)) as t"1db2 "call monreport.lockwait()"1db2 "force application (12345)"1db2 force applications all执行前应确认活动事务及业务影响。
1db2expln -d APPDB -q \2"select * from APP.ORDERS where CUSTOMER_ID=2001"1db2 "explain plan for2 select * from APP.ORDERS3 where CUSTOMER_ID=2001"1db2exfmt -d APPDB -1 -o explain.out1db2 "call monreport.dbsummary(60)"该过程会按指定秒数采样数据库活动。
1db2 "call monreport.connections()"1db2 "call monreport.currentapps()"1db2 get snapshot for dynamic sql on APPDB快照接口属于传统监控方式,新版本优先使用 MON_GET 表函数。
1db2 get snapshot for database on APPDB1db2pd -edus1db2fodc -hang full -db APPDBFODC 可能生成大量诊断文件,应确认目录空间。
1db2 "runstats on table APP.ORDERS2 with distribution3 and detailed indexes all"1db2 "runstats on table APP.ORDERS2 with distribution3 and sampled detailed indexes all"1db2 "reorgchk current statistics on table APP.ORDERS"1db2 "reorgchk update statistics2 on schema APP"1db2 "reorg table APP.ORDERS"1db2 "reorg table APP.ORDERS2 inplace allow write access"1db2 "reorg indexes all2 for table APP.ORDERS3 allow write access"1db2pd -db APPDB -reorg1db2rbind APPDB -l db2rbind.log all1db2 "flush package cache dynamic"执行后 SQL 需要重新编译,应避开高峰。
1db2 "export to orders.del of del2 select * from APP.ORDERS"1db2 "import from orders.del of del2 insert into APP.ORDERS"1db2 "load from orders.del of del2 insert into APP.ORDERS"Load 的锁和日志行为与 Import 不同,生产执行前应评估恢复要求。
1db2pd -db APPDB -load1db2 list utilities show detail1db2move APPDB export1db2move APPDB import1db2move APPDB load1db2 "export to orders.ixf of ixf2 select * from APP.ORDERS"1db2 "import from orders.ixf of ixf2 create into APP.ORDERS_COPY"1db2 get db cfg for APPDB | grep -i log1db2pd -db APPDB -logs1db2 update db cfg for APPDB \2using LOGARCHMETH1 DISK:/db2archive/APPDB/该参数通常需要重新激活数据库。
1db2 backup database APPDB online \2to /backup/db2 include logs1db2 backup database APPDB online \2to /backup/db2 compress include logs1db2 list history backup all for APPDB1db2ckbkp /backup/db2/APPDB.0.db2inst1.DBPART000.20260727120000.0011db2 restore database APPDB \2from /backup/db2 \3taken at 202607271200001db2 rollforward database APPDB \2to end of logs and stop1db2pd -db APPDB -recovery1db2pd -db APPDB -hadr1db2 start hadr on database APPDB as primary1db2 start hadr on database APPDB as standby1db2 stop hadr on database APPDB1db2 takeover hadr on database APPDB1db2 takeover hadr on database APPDB by force仅在原主库已确认不可用并防止双主后执行。
1db2 "select grantee, connectauth, dbadmauth,2 securityadmauth, dataaccessauth3 from syscat.dbauth4 order by grantee"1db2diag -H 1h1db2pd -alldbs1db2level2db2 get instance3db2 list active databases4db2pd -db APPDB -dbcfg -logs -hadr5db2 list applications show detail6db2 list utilities show detail7db2diag -H 1h8df -h每日巡检还应检查表空间、缓冲池命中率、锁等待、长 SQL、统计信息、重组状态、归档日志、备份可恢复性和 HADR 延迟。
Db2 运维应从实例、数据库、存储、应用、SQL、日志和高可用七个层面建立检查体系。db2pd 适合低开销实时诊断,MON_GET 管理函数适合结构化监控,传统快照命令则更多用于兼容旧环境。
备份恢复、日志参数和 HADR 接管具有较高风险,必须按照当前 Db2 LUW 版本文档执行,并通过定期恢复演练验证 RPO 和 RTO,而不能只以备份任务成功作为判断依据。