《數據庫管理命令》sql
1. 查看數據庫大小,單位byte數據庫
1). 經過存儲過程查看 db2 "CALL GET_DBSIZE_INFO(?, ?, ?, 0) " 2). 經過表來查詢數據庫大小 snaphot_tbs_cfg db2 "select (SUM(total_pages)*4)/1024.0/1024 TOTAL_ALLOCATED_SPACE_IN_GB from table (snapshot_tbs_cfg('dbname',-1)) TBS_SPCE"
2. 查看當前數據庫名安全
db2 "select current server from sysibm.sysdummy1"
《表空間管理工具》服務器
1. 查看錶空間狀態架構
1)使用一條命令便可查看錶空間的狀態信息,格式化輸出 db2 list tablespaces show detail 工具
db2 list tablespaces show detail | while read line ; do w1=$(echo "$line" | cut -d" " -f1); if [ "$w1" == "Tablespace" ];then tabid=$(echo "$line" | cut -d'=' -f2);fi; if [ "$w1" == "Name" ];then name=$(echo "$line" | cut -d'=' -f2); fi; if [ "$w1" == "Type" ];then type=$(echo "$line" | cut -d'=' -f2);type1=$(echo "$type" | awk '{print $1}'); if [ "$type1" == "System" ];then Ttype='SMS'; else Ttype='DMS'; fi; fi; if [ "$w1" == "Contents" ];then contents=$(echo "$line" | cut -d'=' -f2); fi; if [ "$w1" == "State" ];then state=$(echo "$line" | cut -d'=' -f2);fi; if [ "$w1" == "Total" ];then total=$(echo "$line" | cut -d'=' -f2);fi;if [ "$w1" == "Used" ]; then used=$(echo "$line" | cut -d'=' -f2); fi; if [ "$w1" == "Free" ]; then free=$(echo "$line" | cut -d'=' -f2); fi; if [ "$w1" == "Page" ];then page=$(echo "$line" | cut -d'=' -f2); printf "%-25s%-5s%-5s%-5s%-10s%-20s%-20s%-30s\n" $name $tabid $page $Ttype $state $total $used; fi done
咱們有時候須要查看錶空間狀態,方式有不少,db2pd, db2 list, 當表空間較多的狀況下,每每信息太多,致使不是很方便的查看信息。對於db2pd 方便之處在於不用鏈接數據庫,便可以查詢表空間信息,而且對錶空間信息進行了較全的輸出;db2 list,能夠以表空間爲單位輸出對應表空間的信息;可是這兩種方式對於表空間較多的數據庫來講用起來很不方便。
查看錶空間狀態
db2 list tablespaces show detail | while read line ; do w1=$(echo "$line" | cut -d" " -f1); if [ "$w1" == "Tablespace" ];then tabid=$(echo "$line" | cut -d'=' -f2); fi; if [ "$w1" == "Name" ];then name=$(echo "$line" | cut -d'=' -f2);fi; if [ "$w1" == "Type" ];then type=$(echo "$line" | cut -d'=' -f2);type1=$(echo "$type" | awk '{print $1}'); if [ "$type1" == "System" ];then Ttype='SMS'; else Ttype='DMS'; fi; fi; if [ "$w1" == "State" ];then state=$(echo "$line" | cut -d'=' -f2); printf "%-25s%-10s%-10s%10s\n" $name $tabid $Ttype $state; fi; donespa
===DISPLAY:
SYSCATSPACE 0 DMS 0x0000
TEMPSPACE1 1 SMS 0x0000
USERSPACE1 2 DMS 0x0000
SYSTOOLSPACE 3 DMS 0x0000
SYSTOOLSTMPSPACE 4 SMS 0x0000code
2)經過視圖查看錶空間信息,包括表空間狀態,總page數,可以使用的page數,使用的page數,空閒的page數,是否自動擴展 server
db2 "select substr(TBSP_NAME,1,20) as TBSPNAME, TBSP_TYPE, substr(TBSP_STATE,1,10) as TBSPSTATE , substr(TBSP_TOTAL_PAGES,1,10) as TOTAL_PAGES, substr(TBSP_USABLE_PAGES,1,10) as USABLE_PAGES, substr(TBSP_USED_PAGES,1,5) as USED_PAGES, substr(TBSP_FREE_PAGES,1,5) as FREE_PAGES, substr(TBSP_MAX_SIZE,1,5) as MAX_SIZE, substr(TBSP_USING_AUTO_STORAGE,1,2) as AUTOStr, substr(TBSP_AUTO_RESIZE_ENABLED,1,2) as AUTORes from SYSIBMADM.TBSP_UTILIZATION"
2. 表空間自動擴展設置對象
1)查看錶空間是否爲自動擴展
db2 "select substr(TBSP_NAME,1,30) as DMS_tablespace_without_autoresize, TBSP_USING_AUTO_STORAGE, TBSP_AUTO_RESIZE_ENABLED from SYSIBMADM.TBSP_UTILIZATION where TBSP_TYPE='DMS' and TBSP_AUTO_RESIZE_ENABLED=0"
2)生成修改表空間爲自動擴展
db2 -x "select 'ALTER TABLESPACE '|| TBSP_NAME ||' AUTORESIZE YES;' from SYSIBMADM.TBSP_UTILIZATION where TBSP_TYPE='DMS' and TBSP_AUTO_RESIZE_ENABLED=0" step 1:
db2 -x "select 'ALTER TABLESPACE '|| TBSP_NAME ||' AUTORESIZE YES;' from SYSIBMADM.TBSP_UTILIZATION where TBSP_TYPE='DMS' and TBSP_AUTO_RESIZE_ENABLED=0" | tee changeTBSauto.sql step 2:
db2 -tvf changeTBSauto.sql
3. 擴表空間步驟
查看錶空間使用率 db2 "select TBSP_ID,substr(TBSP_NAME,1,30) as TBSP_NAME, TBSP_TYPE, TBSP_TOTAL_SIZE_KB/1024 as TOTAL_M,TBSP_USED_SIZE_KB/1024 as USED_M,TBSP_UTILIZATION_PERCENT as Pct, TBSP_USING_AUTO_STORAGE, TBSP_AUTO_RESIZE_ENABLED from SYSIBMADM.TBSP_UTILIZATION" |grep CLOBS_TSP 查看錶空間屬性(是否自動擴展,看AR/auto resize字段) db2pd -db PMOR11 -tablespace 5 查看擴展到目標使用率須要擴展多少容量 db2 "select substr(TBSP_NAME,1,30) as TBSP_NAME,round((TBSP_USED_SIZE_KB/1024/0.75),0) as After_Extend_M,TBSP_USED_SIZE_KB/1024/0.75-TBSP_TOTAL_SIZE_KB/1024 Should_Extend_M from SYSIBMADM.TBSP_UTILIZATION where TBSP_NAME='CLOBS_TSP'" 查看目標服務器是否爲高可用架構 db2pd -db CWAMAPP -hadr 查看是否有備份恢復task正在執行中 db2 list utilities show detail 擴展表空間 alter tablespace CLOBS_TSP extend (all 30M)
《數據庫對象管理工具》
1.查看錶大小
db2 "SELECT SUBSTR(TabSchema,1,15), TabName, TabType, (Data_Object_P_Size + Index_Object_P_Size + Long_Object_P_Size + Lob_Object_P_Size + Xml_Object_P_Size)/1024 as Total_P_Size_MB FROM SysIbmAdm.AdminTabInfo ORDER BY Total_P_Size_MB desc" >table_size.log
2.查詢存儲過程的內容:
db2 " select PROCNAME,text from syscat.procedures where PROCNAME = 'SP_A_TSKRES'"
檢查數據庫中是否有對象狀態處於完整性檢查,並生成執行語句
db2 connect to sample db2 -tx +w "with gen(tabname, seq) as( select rtrim(tabschema) || '.' || rtrim(tabname) as tabname, row_number() over (partition by status) as seq from syscat.tables WHERE status='C' ),r(a, seq1) as (select CAST(tabname as VARCHAR(3900)), seq from gen where seq=1 union all select r.a || ','|| rtrim(gen.tabname), gen.seq from gen , r where (r.seq1+1)=gen.seq ), r1 as (select a, seq1 from r) select 'SET INTEGRITY FOR ' || a || ' IMMEDIATE CHECKED;' from r1 where seq1=(select max(seq1) from r1)" > db2FixCheckPending.sql
db2 -tvf db2FixCheckPending.sql
《數據庫安全管理工具》
1.查看用戶權限
db2 " SELECT SUBSTR(GRANTOR, 1, 10) AS GRANTOR, -- Grantor of the authority SUBSTR(GRANTEE, 1, 10) AS GRANTEE, -- Holder of the authority -- G = Grantee is a group R = Grantee is a role GRANTEETYPE, -- U = Grantee is an individual user BINDADDAUTH, CONNECTAUTH, CREATETABAUTH, DBADMAUTH, IMPLSCHEMAAUTH, DATAACCESSAUTH, LOADAUTH FROM SYSCAT.DBAUTH ORDER BY GRANTEE WITH UR " db2 "select * from syscat.dbauth where grantee='CHUBALA'" db2 " SELECT SUBSTR(GRANTOR, 1, 10) AS GRANTOR, -- Grantor of the authority SUBSTR(GRANTEE, 1, 10) AS GRANTEE, -- Holder of the authority SECURITYADMAUTH, QUIESCECONNECTAUTH, LIBRARYADMAUTH, SQLADMAUTH, WLMADMAUTH, EXPLAINAUTH, ACCESSCTRLAUTH FROM SYSCAT.DBAUTH ORDER BY GRANTEE WITH UR "
2.查看錶權限
db2 "select * from syscat.tabauth where grantee='CHUBALA'" db2 "select substr(GRANTOR,1,30), GRANTORTYPE, substr(GRANTEE,1,30), GRANTEETYPE, substr(TABSCHEMA,1,30), substr(TABNAME,1,30), CONTROLAUTH, ALTERAUTH, DELETEAUTH, INDEXAUTH, INSERTAUTH, REFAUTH, SELECTAUTH, UPDATEAUTH from syscat.tabauth where GRANTEE='AIXDBA3'"
db2 "select substr(GRANTOR,1,30), GRANTORTYPE, substr(GRANTEE,1,30), GRANTEETYPE, substr(TABSCHEMA,1,30), substr(TABNAME,1,30), CONTROLAUTH from syscat.tabauth where GRANTEE='AIXDBA3'"
《HADR維護》
HADR 基本維護操做
1. hadr 的啓停操做
起 HADR
備 :
db2 deactivate database sample
db2 start hadr on database sample as standby
主 :
db2 deactivate database sample
db2 start hadr on database sample as primary
##啓動完成後 ,查看hadr 狀態
1. db2 get snapshot for db on sample
2. db2pd -db sample -hadr
停 HADR
主 :
db2 deactivate database sample
db2 stop hadr on database sample
備 :
db2 deactivate database sample /[db2inst1@hadr1 logtarget]$ db2 deactivate database sample user db2inst1 using db2inst1
db2 stop hadr on database sample
### 有時 deactivate database 不加用戶名密碼時,在stop hadr 時會報錯
[db2inst1@hadr1 logtarget]$ db2 stop hadr on database sample
SQL1769N Stop HADR cannot complete. Reason code = "2".
[db2inst1@hadr1 logtarget]$
2. 在備用服務器上執行數據庫接管
db2 takeover hadr on database sample (by force)