内容目录
1. 获得 db2 命令行的总体帮助: db2 ?2. 获得 db2 具体命令的帮助: db2 ? db2-command3. 获得 db2 错误码、状态码、类型码的帮助
做过 IT 运维的朋友们对 shell 的命令都非常熟悉了,你甚至可以轻松地使用以下 shell 命令统计出你最常使用用前 10 个命令。
history | awk '{CMD[$2]++;count++;} END { for (a in CMD )print CMD[ a ]" " CMD[ a ]/count*100 "% " a }' | grep -v "./" | column -c3 -s " " -t |sort -nr | nl | head -n10
统计最常用的前10条命令
然而对于刚入职的小白,看得一愣一愣的,这太牛逼了,不禁要问这是怎么做到的。当然你可以靠记忆,通过多次重复来强化,然而,且不说 shell 指令有多少,单单一个 db2 数据库的命令就够你花半年的时间来熟悉,一个人的精力毕竟是有限的,而命令是无限多的,那么如何使用有限的精力来操纵无限多的命令呢? 有人会说靠搜索引擎,比如百度,想想吧,你本来是找 awk 的帮助的,结果使用了百度,被转移了注意力,后来不知道自己要干嘛来了…
经过这几年的工作经验,我认为学习一个命令最佳的方法还是使用命令本身的帮助文档,它不仅非常专业,而且让你保持专注。学会使用命令本身的帮助文档,才能以不变应万变,弹指间,文思泉涌,命令已跃上屏幕。一般情况下可以使用 man command 或 command --help 来获取 command 的帮助文档。而 db2 命令却使用 “?” ,本文介绍如何使用 db2 的帮助命令,以便帮助你通过阅读帮助文档来学习如何使用 db2 命令。
1. 获得 db2 命令行的总体帮助: db2 ?
$ db2 ?
db2 [option ...] [db2-command | sql-statement |
[? [phrase | message | sqlstate | class-code]]]
option: -a, -c, -d, -e{c|s}, -finfile, -i, -lhistfile, -o, -m, -n,
-p, -q, -rreport, -s, -t, -td;, -v, -w, -x, -zoutputfile.
db2-command:
ACTIVATE DATABASE GET CONTACTGROUPS RECONCILE
ADD CONTACT GET CONTACTS RECOVER
ADD CONTACTGROUP GET/UPDATE DB CFG REDISTRIBUTE DB PARTITION
ADD DATALINKS MANAGER GET/UPDATE DBM CFG REFRESH LDAP
ADD DBPARTITIONNUM GET DBM MONITOR SWITCHES REGISTER LDAP
ADD XMLSCHEMA GET DESCRIPTION FOR HEALTH REGISTER XMLSCHEMA
ARCHIVE LOG GET NOTIFICATION LIST REGISTER XSROBJECT
ATTACH GET HEALTH SNAPSHOT REORG INDEXES/TABLE
AUTOCONFIGURE GET INSTANCE REORGCHK
BACKUP DATABASE GET MONITOR SWITCHES RESET ADMIN CFG
BIND GET RECOMMENDATIONS RESET ALERT CFG
CATALOG APPC NODE GET ROUTINE RESET DB CFG
CATALOG APPN NODE GET SNAPSHOT RESET DBM CFG
CATALOG DATABASE HELP RESET MONITOR
CATALOG DCS DATABASE HISTORY RESTART DATABASE
CATALOG LDAP DATABASE IMPORT RESTORE DATABASE
CATALOG LDAP NODE INITIALIZE TAPE REWIND TAPE
CATALOG LOCAL NODE INSPECT ROLLFORWARD DATABASE
CATALOG NPIPE NODE LIST ACTIVE DATABASES RUNCMD
CATALOG NETBIOS NODE LIST APPLICATIONS RUNSTATS
CATALOG ODBC DATA SOURCE LIST COMMAND OPTIONS SET CLIENT
CATALOG TCPIP NODE LIST DATABASE DIRECTORY SET RUNTIME DEGREE
CHANGE DATABASE COMMENT LIST DB PARTITION GROUPS SET TABLESPACE CONTAINERS
CHANGE ISOLATION LEVEL LIST DATALINKS MANAGERS SET TAPE POSITION
COMPLETE XMLSCHEMA LIST DBPARTITIONNUMS SET UTIL_IMPACT_PRIORITY
CREATE DATABASE LIST DCS APPLICATIONS SET WRITE
CREATE TOOLS CATALOG LIST DCS DIRECTORY START DATABASE MANAGER
DEACTIVATE DATABASE LIST DRDA INDOUBT START HADR
DECOMPOSE XML DOCUMENT LIST HISTORY STOP DATABASE MANAGER
DECOMPOSE XML DOCUMENTS LIST INDOUBT TRANSACTIONS STOP HADR
DEREGISTER LIST NODE DIRECTORY TAKEOVER HADR
DESCRIBE LIST ODBC DATA SOURCES TERMINATE
DETACH LIST PACKAGES/TABLES UNCATALOG DATABASE
DROP CONTACT LIST TABLESPACE CONTAINERS UNCATALOG DCS DATABASE
DROP CONTACTGROUP LIST TABLESPACES UNCATALOG LDAP DATABASE
DROP DATABASE LIST UTILITIES UNCATALOG LDAP NODE
DROP DATALINKS MANAGER LOAD UNCATALOG NODE
DROP DBPARTITIONNUM LOAD QUERY UNCATALOG ODBC DATA
DROP TOOLS CATALOG MIGRATE DATABASE UNQUIESCE DATABASE
ECHO PING UNQUIESCE INSTANCE
EDIT PREP/PRECOMPILE UPDATE ALERT CFG
EXPORT PRUNE HISTORY/LOGFILE UPDATE COMMAND OPTIONS
FORCE APPLICATION PUT ROUTINE UPDATE CONTACT
GET/UPDATE ADMIN CFG QUERY CLIENT UPDATE CONTACTGROUP
GET ALERT CFG QUIESCE DATABASE UPDATE NOTIFICATION LIST
GET AUTHORIZATIONS QUIESCE INSTANCE UPDATE HISTORY
GET/UPDATE CLI CFG QUIESCE TABLESPACES UPDATE LDAP NODE
GET CONNECTION STATE QUIT UPDATE MONITOR SWITCHES
GET CONTACTGROUP REBIND XQUERY
Note: Some commands are operating system specific and may not be available.
For further help:
? db2-command - help for specified command
? OPTIONS - help for all command options
? HELP - help for reading help screens
The preceding three options can be run as db2 <option> from an OS prompt.
!db2ic - DB2 Information Center (Windows only)
This command can also be run as db2ic from an OS prompt.
从输出信息的第一行开始看起,可以看到,db2 后面到底可以执行什么命令:
db2 [option ...] [db2-command | sql-statement |
[? [phrase | message | sqlstate | class-code]]]
option: -a, -c, -d, -e{c|s}, -finfile, -i, -lhistfile, -o, -m, -n,
-p, -q, -rreport, -s, -t, -td;, -v, -w, -x, -zoutputfile.
注意: [ ] 里的内容是可选项,即可以有,也可以没有。
| 分隔的内容表示可以选择其中的一个。
{} 里的内容表示必选项,必须有。
这里的意思就是:
db2 后面第一个位置有个可选的参数,可以有,也可以没有,如果有的话,只有这些选项
-a, -c, -d, -e{c|s}, -finfile, -i, -lhistfile, -o, -m, -n, -p, -q, -rreport, -s, -t, -td;, -v, -w, -x, -zoutputfile.
第二个位置也是个可选的参数,可以有,也可以没有,如有有的话,只能有这些选项:
db2 的命令 db2-command。
sql语句。
可选项 ? [phrase | message | sqlstate | class-code] ,? 后可以跟 phrase | message | sqlstate | class-code。
有人可能会问了,你怎么知道 "{} 里的内容表示必选项,必须有", -a, -c, -d 那些选项都是啥意思呢?
答案:
第一:
如果你看不懂这些帮助信息,请看上述输入信息的倒数第四行的提示:
For further help:
? db2-command - help for specified command
? OPTIONS - help for all command options
? HELP - help for reading help screens
有个 ?HELP 就是帮助你阅读帮助信息的。
执行 db2 ? HELP 得到如下输出信息
$ db2 ? HELP
Instructions for reading help screens:
[ ] Encloses optional parameters
{ } Encloses mandatory parameters
| Separates two or more items, only one of which may be chosen
... Indicates a repeatable parameter.
KEY and RESERVED words are in uppercase - they are mandatory.
For example,
CONNECT TO database-alias [IN {SHARE | EXCLUSIVE} MODE]
[USER username [USING password]]
indicates that the following are valid statements:
CONNECT TO sample
CONNECT TO sample IN SHARE MODE
CONNECT TO sample in EXCLUSIVE MODE
CONNECT TO sample IN SHARE MODE USER smith USING pw
while the following are invalid:
CONNECT TO sample IN MODE (SHARE or EXCLUSIVE is required)
CONNECT TO sample IN SHARE EXCLUSIVE MODE (cannot use SHARE and EXCLUSIVE)
CONNECT TO sample USING pw (cannot use password without username)
On some platforms, HELP is also available as a command to access
the on-line Command Reference and the SQL Reference.
The syntax of the HELP command is:
HELP [character-string]
这里说的够清楚了吧,如果看不懂,请再加强下英语的学习,英语非常重要,它的用处也不只是为了阅读帮助文档。
第二:
那些选项是啥意思呢,直接 ? OPTIONS 即可,注意帮助命令中大写单词,如这里的 OPTIONS ,是可以直接写在命令行的。
$ db2 ? OPTIONS
db2 [option ...] [db2-command | sql-statement |
[? [phrase | message | sqlstate | class-code]]]
option: -a, -c, -d, -e{c|s}, -finfile, -i, -lhistfile, -m, -n, -o,
-p, -q, -rreport, -s, -t, -td;, -v, -w, -x, -zoutputfile.
Option Description Default Setting
------ ---------------------------------------- ---------------
-a Display SQLCA OFF
-c Auto-commit ON
-d Retrieve and display XML declarations OFF
-e Display SQLCODE/SQLSTATE OFF
-f Read from input file OFF
-i Display XML data with indentation OFF
-l Log commands in history file OFF
-m Display the number of rows affected OFF
-n Remove new line character OFF
-o Display output ON
-p Display db2 interactive prompt ON
-q Preserve whitespaces and linefeeds OFF
-r Save output report to file OFF
-s Stop execution on command error OFF
-t Set statement termination character OFF
-v Echo current command OFF
-w Display FETCH/SELECT warning messages ON
-x Suppress printing of column headings OFF
-z Save all output to output file OFF
Notes:
Use the DB2OPTIONS environment variable to customize option defaults.
An option takes on the opposite value if the minus sign (-) is changed to a
plus (+).
Use UPDATE COMMAND OPTIONS to change option settings in interactive or
file input mode.
看到这里,想必你已经非常清楚了。
2. 获得 db2 具体命令的帮助: db2 ? db2-command
这里只举三个例子,其他命令类比即可。比如
(1)获取常见的导数命令 export 的帮助信息:
$ db2 ? export
EXPORT TO filename OF {IXF | DEL | WSF}
[LOBS TO lob-path [ {,lob-path} ... ] ][LOBFILE lob-file [ {,lob-file} ... ] ]
[XML TO xml-path [ {,xml-path} ... ] ][XMLFILE filename [ {,filename} ... ] ]
[MODIFIED BY {filetype-mod ...}][XMLSAVESCHEMA]
[METHOD N ( column-name [ {,column-name} ... ] )] [MESSAGES message-file]
{select-statement | XQUERY xquery-statement |
HIERARCHY {STARTING sub-table-name |
(sub-table-name [{, sub-table-name} ...])} [WHERE ...] }
filetype-mod:
NODOUBLEDEL, LOBSINFILE, CHARDELx, COLDELx, DECPLUSBLANK
DECPTx, DATESISO, 1, 2, 3, 4, CODEPAGE=x, STRIPLZEROS, NOCHARDEL,
LOBSINSEPFILES, XMLINSEPFILES, XMLCHAR, XMLGRAPHIC, XMLNODECLARATION,
TIMESTAMPFORMAT=x
NOTE: From the operating system prompt, prefix commands with 'db2'.
Special characters MAY require an escape sequence (\), for example:
db2 \? change database
db2 ? change database xxx comment with \"text\"
看到这里,想必已经非常清楚了,实在不清楚的可以找个例子对比下,也不用太费心记忆,相信下次你看到这个帮助信息时,不需要查看他人的脚本即可写出自己的 export 语句。
(2)获取 get命令 的帮助信息:
在监控数据库时常常要 get 一些信息,到底哪些信息可以通过 get 命令获取呢?
$ db2 ? get
GET ADMIN CONFIGURATION [FOR NODE node-name [USER username USING password]]
GET ALERT CONFIGURATION FOR {{CONTAINERS | DATABASE MANAGER | DATABASES |
TABLESPACES} [DEFAULT] | {CONTAINER container-name FOR tblspace-name |
DATABASE | TABLESPACE tblspace-name} ON database_alias}
[USING health-indicator-name [ {,health-indicator-name} ... ]]
GET AUTHORIZATIONS
GET CLI CONFIGURATION [AT GLOBAL LEVEL] [FOR SECTION section-name]
GET CONNECTION STATE
GET CONTACTGROUP contact-group-name
GET CONTACTGROUPS
GET CONTACTS
GET DATABASE CONFIGURATION [FOR database-alias] [SHOW DETAIL]
GET DATABASE MANAGER CONFIGURATION [SHOW DETAIL]
GET DATABASE MANAGER MONITOR SWITCHES
[AT DBPARTITIONNUM db-partition-number | GLOBAL]
GET DESCRIPTION FOR HEALTH INDICATOR shortname
GET {HEALTH NOTIFICATION CONTACT | NOTIFICATION} LIST
GET HEALTH SNAPSHOT FOR {DATABASE MANAGER | ALL DATABASES |
{ALL | DATABASE | TABLESPACES} ON database-alias}
[AT DBPARTITIONNUM db-partition-number | GLOBAL] [SHOW DETAIL]
[WITH FULL COLLECTION]
GET INSTANCE
GET MONITOR SWITCHES
[AT DBPARTITIONNUM db-partition-number | GLOBAL]
GET RECOMMENDATIONS FOR HEALTH INDICATOR shortname
[FOR {DBM | TABLESPACE tablespace-name ON database-alias
| CONTAINER container-name FOR TABLESPACE tablespace-name ON database-alias
| DATABASE ON database-alias} [AT DBPARTITIONNUM db-partition-number | GLOBAL]]
GET ROUTINE INTO filename FROM [SPECIFIC] PROCEDURE routine-name [HIDE BODY]
GET SNAPSHOT FOR {DATABASE MANAGER | ALL [DCS] DATABASES |
ALL [DCS] APPLICATIONS | ALL BUFF ERPOOLS | [DCS] APPLICATION
{APPLID appl-id | AGENTID appl-handle} | FCM FOR ALL DBPARTITIONNUMS |
LOCKS FOR APPLICATION {APPLID appl-id | AGENTID appl-handle} |
{ALL | [DCS] DATABASE | [DCS] APPLICATIONS | TABLES |
TABLESPACES | LOCKS | BUFFERPOOLS | DYNAMIC SQL [write to file]}
ON database-alias} [AT DBPARTITIONNUM db-partition-number | GLOBAL]
NOTE: From the operating system prompt, prefix commands with 'db2'.
Special characters MAY require an escape sequence (\), for example:
db2 \? change database
db2 ? change database xxx comment with \"text\"
(3)查询具体的命令:
$ db2 ? get snapshot
GET SNAPSHOT FOR {DATABASE MANAGER | ALL [DCS] DATABASES |
ALL [DCS] APPLICATIONS | ALL BUFFERPOOLS | [DCS] APPLICATION
{APPLID appl-id | AGENTID appl-handle} | FCM FOR ALL DBPARTITIONNUMS |
LOCKS FOR APPLICATION {APPLID appl-id | AGENTID appl-handle} |
{ALL | [DCS] DATABASE | [DCS] APPLICATIONS | TABLES |
TABLESPACES | LOCKS | BUFFERPOOLS | DYNAMIC SQL [write to file]}
ON database-alias} [AT DBPARTITIONNUM db-partition-number | GLOBAL]
NOTE: From the operating system prompt, prefix commands with 'db2'.
Special characters MAY require an escape sequence (\), for example:
db2 \? change database
db2 ? change database xxx comment with \"text\"
3. 获得 db2 错误码、状态码、类型码的帮助
(1) 如 SQLCODE 968 错误
$ db2 ? sql968
SQL0968C The file system is full.
Explanation:
One of the file systems containing the database is full. This file
system may contain the database directory, the database log files, or a
table space container.
The statement cannot be processed.
User response:
Free system space by erasing unwanted files. Do not erase database
files. If additional space is required, it may be necessary to drop
tables and indexes identified as not required.
On unix-based systems, this disk full condition may be due to exceeding
the maximum file size allowed for the current userid. Use the chuser
command to update fsize. A reboot may be necessary.
This disk full condition may be caused when containers are of varying
sizes. If there is sufficient space in the file system, drop the table
space and recreate it with containers of equal size.
If the statement that could not be processed referenced LOB data types:
* Ensure that any cursors used in the application are closed
immediately after their use.
* Ensure that within the application that COMMIT statements are
periodically executed.
* Add additional containers to the system temporary tablespace to hold
the temporary LOB data during this statement's execution.
sqlcode: -968
sqlstate: 57011
(2)如 sqlstate 42501
$ db2 ? 42501
SQLSTATE 42501: The authorization ID does not have the privilege to perform
the specified operation on the identified object.
(3)如类型码 class code 01
$ db2 ? 01
01: Warning
现在,你可以使用帮助命令来获取相关命令的详细使用方法了。
总结:帮助命令可以大大减轻记忆的压力,我们可以简单的记忆一些命令的含义,这一点很容易做到,在具体使用的时候去查询命令的帮助信息,帮助信息体现了命令设计的逻辑结构,经常使用帮助信息有助于更深刻的理解命令,达到举一反三,触类旁通学习的效果。