首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> DB2 資料庫 >> DB2 教學

DB2 資料庫實戰操作指令大全:匯入、鎖管理、日誌、備份恢復

     編輯:DB2 教學

一、高效載入資料 (LOAD)

-- 匯出乾淨資料
export to tempfile of del select * from TABLENAME where not 清理條件;

-- 非恢復模式高速載入,替換現有資料,忽略換行符問題
load from tempfile of del modified by delprioritychar replace into TABLENAME nonrecoverable;
  • delprioritychar:防止資料含換行符導致匯入失敗。
  • replace into:截斷表並重新載入。
  • nonrecoverable:不記錄日誌,速度最快,事後需完整備份。
  • 多表並行 export 可提升整體效率。

二、應用程式與連線管理

-- 列出所有應用
db2 list applications | grep DBNAME

-- 強制斷開指定應用 (應用代理 ID 為 123, 456)
db2 "force application (123, 456)"

-- 查看特定應用代理執行狀態
db2 get snapshot for application agentid 299 | grep Row

三、資料庫配置參數查看與修改

-- 查看所有配置
db2 get db cfg for [dbname]

-- 修改單一參數 (如 LOGFILSIZ)
db2 update db cfg for [dbname] using LOGFILSIZ 4096

四、DB2 停啟與強制停止

-- 正常停止 (若有活動連線會失敗)
db2stop

-- 強制停止 (終止所有連線)
db2stop force

-- 啟動
db2start

遇到 SQL1025N (資料庫仍作用中) 時必須用 force。

五、緩衝池管理

-- 調整預設緩衝池大小 (單位: 頁)
db2 alter bufferpool ibmdefaultbp size 10240

-- 查看緩衝池狀態
db2 "select * from syscat.bufferpools"

六、日誌模式切換與管理

1. 循環日誌 ⇄ 歸檔日誌

-- 歸檔模式 (支援線上備份)
db2 update db cfg for [dbname] using LOGRETAIN ON
-- 修改後資料庫進入 BACKUP PENDING,需離線備份才能恢復正常

-- 循環模式 (僅離線備份)
db2 update db cfg for [dbname] using LOGRETAIN OFF

2. 歸檔日誌清理 (PRUNE)

-- 1. 確認日誌目錄與第一個作用中日誌
db2 get db cfg for [dbname]  # 看 "Path to log files" 與 "First active log file"

-- 2. 備份欲刪除的歸檔日誌

-- 3. 以實例擁有者執行 (假設第一作用中日誌為 S0015913.LOG)
db2 connect to [dbname]
db2 prune logfile prior to S0015913.LOG

嚴禁 直接用 rm 刪除日誌檔,會導致資料庫損壞。

七、db2diag.log 清理

# 1. 確認 DIAGPATH
db2 get dbm cfg | grep DIAGPATH

# 2. 停止應用與 DB2 實例
# 3. 備份舊日誌
# 4. 刪除 db2diag.log,DB2 會自動建立新檔

八、LOAD 失敗後的表狀態恢復

-- 強行中斷 LOAD 導致表處於 "Load Pending" (SQL0668N reason code 3)
-- 終止 LOAD 並釋放表鎖
db2 "load from /dev/null of del terminate into TABLENAME nonrecoverable"

-- 若未用 nonrecoverable,檢查表空間狀態
db2 list tablespaces show detail
-- 非正常狀態需離線備份恢復

九、跨表 UPDATE 與複合條件查詢

-- 多欄位關聯更新
db2 "update A set bal = (select txnamt from B where A.actno=B.actno and A.cnlno=B.cnlno)
      where A.actno||A.cnlno in (select actno||cnlno from B)"

-- LIKE 模糊查詢
db2 "select * from A where actno like 'B_AAA%'"
db2 "select * from A where actno like '%-AAA%'"

十、資料庫恢復與 ROLL FORWARD

-- 重導向恢復範例
restore db db1 to /tstdb2/catalog into db newlogpath /tstdb2/db2log buffer 2048
replace existing redirect parallelism 16;
set tablespace containers for 1 using (path '/tstdb2/db2tmp');
set tablespace containers for 2 using
  (device '/dev/rtstcontlv00' 2621440, ...);
restore db db1 continue;

-- 恢復完成後若報 ROLL-FORWARD PENDING
db2 rollforward db db1 to end of logs and complete
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved