Summary of DB2 Common Commands

PS: Before executing the command, you need to enter the DB2 account: su db2inst1

Change password: You can change the corresponding operating system password. You can change the db2admin password on Windows, and the db2inst1 password on Linux. The database password of db2 is bound to the operating system, just like you said, you only need to modify the operation. The password of the user on the system is sufficient. If you want to change that user's password, you can change that user's password on the operating system.

Installation and configuration tutorial under DB2 linux

================================Experience is the most valuable ================= ====================

1. Open the command line window 
  #db2cmd 
2. Open the control center 
  # db2cmd db2cc 
3. Open the command editor 
 db2cmd db2ce 
=====Operate the database command ===== 

4. Start the database instance 
 ======== ================ 
26. Backup database 
#db2 backup db <db name> 
Note: Before executing the above command, you need to disconnect the database connection 

27. Backup database online 
#db2 -v " BACKUP DATABASE <database name> ONLINE TO <path> WITH 2 BUFFERS BUFFER 1024 INCLUDE LOGS WITHOUT PROMPTING" 
28. Restore database 
#db2 restore db <source db name> 

29. Restore database online 
#db2 "RESTORE DB <database name> TO < db path> LOGTARGET <logpath> WITHOUT PROMPTING" 
#db2 "ROLLFORWARD DB <database name> TO END OF LOGS AND STOP" ... 
30. Export data file 

#db2move < db name> export 

[-sn <schema name, usually db2admin>] 
[-tn <table name, separated by commas>] 
31. Import data files 
#db2move <db name> import 
32. Obtain db2 database management configuration environment information 
# db2 get dbm cfg 
33. Obtain the database management configuration environment information of a db2 database 
#db2 get db cfg for <db name> 

Or: execute db2 get db cfg after connecting to a database 

34. 
Change the size of the db2 log space Notes: The following commands are designed to prevent the db2 database from overusing hard disk space, and are only used for db2 on the developer's own machine. If it is a server, the parameters need to be modified. 

#db2 UPDATE DB CFG FOR <db name> USING logretain OFF logprimary 3 logsecond 2 logfilsiz 25600; 
If the page size is 4KB, the above command creates three log files of 100M, occupying 300MB of hard disk space. 25600*4KB=102400KB. 
35. Create a temporary table space 
#DB2 CREATE USER TEMPORARY TABLESPACE STMASPACE PAGESIZE 32 K MANAGED BY DATABASE USING (FILE 'D:\DB2_TAB\STMASPACE.F1' 10000) 
EXTENTSIZE 256 
36. Obtain the snapshot data of the database manager 
#db2 –v get snapshot for dbm 
37. Display process number 
#db2 list applications show detail 
=============================== ====================== 
First, load data: 
1. Load with the default separator, the default is "," 
db2 "import from btpoper.txt of del insert into btpoper" 
2. Load db2 with the specified separator "|" 
"import from btpoper.txt of del modified by coldel| insert into btpoper" 
2. Unload data: 
1. Unload all data in a table 
db2 "export to btpoper.txt of del select * from btpoper" 
db2 "export to btpoper.txt of del modified by coldel| select * from btpoper" 
2. Unload data in a table with conditions 
db2 "export to btpoper.txt of del select * from btpoper where brhid= '907020000'" 
db2 "export to cmmcode. txt of del select * from cmmcode where codtp='01'" 
db2 "export to cmmcode.txt of del modified by coldel| select * from cmmcode where codtp='01'" 
3. Query data structure and data: 
db2 "select * from btpoper" 
db2 "select * from btpoper where brhid='907020000 ' and oprid='0001'" 
db2 "select oprid,oprnm,brhid,passwd from btpoper" 
Fourth, delete the data in the table: 
db2 "delete from btpoper" 
db2 "delete from btpoper where brhid='907020000' or brhid='907010000 '" 
Fifth, modify the data in the table: 
db2 "update svmmst set prtlines=0 where brhid='907010000' and jobtp='02'" 
db2 "update svmmst set prtlines=0 where jobtp='02' or jobtp='03' " 
6. Connect the database 
db2 connect to btpdbs 
7. Clear the database connection 
db2 connect reset Disconnect the database connection 
db2 terminate Disconnect the database connection 
db2 force applications all Disconnect all database connections 
8. Backup database 
1. db2 backup db btpdbs 
2. db2move btpdbs export 
db2look -d btpdbs -e -x [-a] -o crttbl.sql 
9. Restore database 
1. db2 restore db btpdbs without rolling forward 
2, db2 -tvf crtdb.sql 
crtdb.sql file content: create db btpdbs on /db2catalog  db2 
-stvf  crttbl.sql  db2move 
btpdbs  import 4 digits, not enough 4 digits, add 0 in front.  11. Bind command: Bind the application to the database. After each database recovery, it is recommended to do a bind  (1) db2 bind br8200.bnd  (2) / btp/bin/bndall /btp/bnd  /btp/bin/bndall /btp/tran/bnd  12. View database parameters:  db2 get dbm cfg  db2 get db cfg for btpdbs  13. Modify database parameters: 













db2 update db cfg for btpdbs using LOGBUFSZ 20 
db2 update db cfg for btpdbs using LOGFILSIZ 5120 
After the modification, the following command should be executed to make it take effect: 
db2 stop 
db2 start 

Supplement: 
db2 set schema btp Modify the current schema to "btp" 
db2 list tablespaces show detail View the current database tablespace allocation 
db2 list tablespace containers for 2 show detail View tablespace id=2 Use the directory where the container is located 
db2 list application 
db2 list db directory List all databases 
db2 list active databases List all active databases 
db2 list tables for all List all tables in the current database 
db2 list tables for schema btp List the tables whose schema is btp in the current database 
db2 list tablespaces show detail Display database space usage 
db2 list packages for all 

db2 "import from tab76.ixf of ixf commitcount 5000 insert into achact" 
db2 "create table achact_t like achact" 
db2 "rename table achact_t to achact" 
db2 "insert into achact_t select * from achact where txndt>=(select lstpgdt from 
acmact where actno=achact.actno)" 
db2 get snapshot for dynaimic sql on jining 
Drop an instance: 
# cd /usr/lpp/db2_07_01/instance 
# ./db2idrop InstName 
List all DB2 instances: 
# cd /usr/lpp/db2_07_01/bin 
# ./db2ilist 
Catalog the database 
$ db2 catalog db btpdbs on / db2catalog 
Cancels cataloged databases btpdbs 
$ db2 uncatalog db btpdbs 
View version 
# db2level 
Displays the current database management instance 
$ db2 get instance 
Sets whether the instance is automatically started when the system starts. 
$ db2iauto -on to start automatically 
$ db2iauto -off to not start 
database optimization commands automatically: 
reorg, runstats 
When the database is used for a period of time, the data space will become larger and larger. Some deleted 
data are still stored in the database, occupying data space and affecting system performance. Therefore, it is necessary to run the reorg and runstats commands regularly 
to clear the deleted data and optimize the data structure. 
db2 reorg table table name 
db2 runstats on table table name with distribution and indexes all 
Because there are many tables to be optimized, a sh program runsall is provided in the /btp/bin directory, which 
can be run after the end of the day's business. Database optimization 

In the development process of DB2, a very important part of the work throughout the development process is the maintenance of the database; it is very necessary to maintain a huge information system; keep a simple maintenance manual for future use. required; some maintenance orders have been collected below for our maintenance engineers and project managers. 
===================================================== ============== 
38. Change the 
size of the db2 log space Note: The following commands are designed to prevent the db2 database from overusing the hard disk space, and are only used for db2 on the developer's own machine. If it is a server, the parameters need to be modified. 
# db2 UPDATE DB CFG FOR <db name> USING logretain OFF logprimary 3 logsecond 2 logfilsiz 25600; 
If the page size is 4KB, the above command creates three log files of 100MB, occupying 300MB of hard disk space. 25600*4KB=102400KB. 
39. Create temporary table space 
#DB2 CREATE USER TEMPORARY TABLESPACE STMASPACE PAGESIZE 32 K MANAGED BY DATABASE USING (FILE 'D:\DB2_TAB\STMASPACE.F1' 10000) EXTENTSIZE 256 
40. Create table space 
rem Create buffer pool space 8K 
#db2 connect to gather 
#db2 CREATE BUFFERPOOL STMABMP IMMEDIATE SIZE 25000 PAGESIZE 8K 
rem Create tablespace: STMA 
rem must confirm the path is correct 
rem D:\DB2Container\Stma 
#db2 drop tablespace stma 
#db2 CREATE REGULAR TABLESPACE STMA PAGESIZE 8 K MANAGED BY SYSTEM USING (' D:\DB2Container\Stma' ) EXTENTSIZE 8 OVERHEAD 10.5 PREFETCHSIZE 8 TRANSFERRATE 0.14 BUFFERPOOL STMABMP DROPPED TABLE RECOVERY OFF 
#db2 connect reset 
41, restore the suspended data to the rollforward state 
#db2 ROLLFORWARD DATABASE TESTDB TO END OF LOGS AND COMPLETE NORETRIEVE 

42. Backup tablespace 
#BACKUP DATABASE YNDC TABLESPACE ( USERSPACE1 ) TO "D:\temp" WITH 2 BUFFERS BUFFER 1024 PARALLELISM 1 WITHOUT PROMPTING 
43. Create db2 tool database 
#db2 create tools catalog systools create new database toolsdb 
44. How to perform incremental/differential backup 
Incremental: the data part added between the last full backup and this backup; 
delta: since the last backup (may be a full backup , incremental backup or differential backup) to the data part added between this backup; 
45. Update the statistics of all tables 
#db2 -v connect to DB_NAME 
#db2 -v "select tbname, nleaf, nlevels, stats_timefrom sysibm. sysindexes" 
#db2 -v reorgchkupdate statistics on table all 
#db2 -v "select tbname, nleaf, nlevels, stats_timefrom sysibm.sysindexes" 
#db2 -v terminate 

46. Run statistics on a table 
#db2 -v runstatson table TAB_NAMEand indexes all 
47. Check if RUNSTATS is executed on the database 
#db2 -v "select tbname, nleaf, nlevels, stats_timefrom sysibm.sysindexes" 
48. Change the size of the  buffer pool In the
buffer pool, when syscat.bufferpools When npages is -1, the size of the buffer pool is controlled by the database configuration parameter bufferpage. 
Command to change the value of npages to -1: 
#db2 -v connect to DB_NAME 
#db2 -v select * from syscat.bufferpools 
#db2 -v alter bufferpoolIBMDEFAULTBP size -1 
#db2 -v connect reset 
#db2 -v terminate 
change database The command to configure the parameter BufferPages is as follows: 
#db2 -v update db cfgfor dbnameusing BUFFPAGE bigger_value 
#db2 -v terminate 
49. See the database monitoring content list 
#db2 -v get monitor switches 
50. Open a database monitoring content 
#db2 -v update monitor switches using bufferpoolon 
51. Get database snapshot 
#db2 -v get snapshot for all databases > snap.out 
#db2 -v get snapshot for dbm>> snap.out 
#db2 -v get snapshot for all bufferpools>> snap.out 
#db2 -v terminate 

52. Reset database snapshot 
#db2 -v reset monitor all 
53. Calculate buffer pool hit rate 
Ideally, the buffer pool hit rate should be above 95%. The calculation formula is as follows: 
(1 -((buffer pool data physical reads + buffer pool index physical reads) 
/(buffer pool data logical reads + pool index logical reads))) *100% 
=========Database instance================== ====== 
54, create a db2 instance 
#db2icrt <instance name> 
55, delete a db2 instance 
#db2idrop <instance name> 
56, set the current db2 instance 
#set db2intance=db2 
57, display the instance owned by db2 
#db2ilist 
58, Command to restore offline incremental backup database 
#DB2 RESTORE DATABASE YNDC INCREMENTAL AUTOMATIC FROM D:\backup\autobak\db2 TAKEN AT 20060314232015 
59. To create a sample database 
on unix platform, use: 
#sqllib/bin/db2sampl <path> 
On windows, os/2 platform, use: db2sampl e,e are optional parameters, specify the drive where the database will be created 

60, set the federated database to be available (the default federated database is unavailable) 

#db2 update dbm cfg using federated yes 

61, list all tables in the database 
#db2 list tables 
62 、Data migration method 1 
export script example 
#db2 connect to testdb user test password test 
#db2 "export to aa1.ixf of ixf select * from table1" 
#db2 "export to aa2.ixf of ixf select * from table2" 
#db2 connect reset 
import script example 
#db2 connect to testdb user test password test 
#db2 "load from aa1.ixf of ixf replace into table1 COPY NO without prompting " 
#db2 "load from aa2.ixf of ixf replace into table2 COPY NO without prompting " 
#db2 connect reset

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324872363&siteId=291194637
Recommended