DB2 How To

How to install a DB2 server on RHEL or CentOS

Disable selinux
Get prereqs
sudo yum install -y compat-libstdc++-33 pam-devel.i686 libstdc++.i686
This uses a compat repo - if it's not enabled use yum-config-manager --enable rhel-7-server-optional-rpms
Reboot
As root run

server/db2prereqcheck
server/db2setup -r /install/db2server.rsp

default DB2 instance "db2inst1" is created using the port number "50000".

Install the license


su needs -l so the DB2 environment variables are simulated

sudo su -lc "db2licm -a 'ese_o/db2/license/db2ese_o.lic' " db2inst1
sudo su -lc "db2licm -l" db2inst1

Should say Product name: "DB2 Enterprise Server Edition", License type: "Restricted", Expiry date: "Permanent"
To validate your installation files, instance, and database functionality. As any user;
/opt/ibm/db2/V10.5/bin/db2val

How to record response file for DB2 10.5

Connect via SSH with X forwarding. Run the following as root from the db2/server folder
./db2setup -t /root/db2setup.debug -l /root/db2setup.log
Then in one of hte screens select "save responses to a text file
By default the response file will be saved to /root/db2setup.rsp
validate it with
db10.5server/db2setup -r /root/db2server.rsp -c


How to get a snapshot for DB2

db2 get snapshot for dynamic sql write to file on dbname | tee output.file
or
db2 "select * from table( SNAPSHOT_DYN_SQL( ‘dbname',-1) as t"

How to check DB2 licence

/export/home/ldapdb2/sqllib/adm/db2licm -l

How to get the DB2 version

db2level

How to make sure DB2 starts and works with WAS on AIX.

Modify startServer.sh - add
. ~db2inst1/sqllib/db2profile
or add the lines below to the file /etc/profile, and .dtprofile:

#The following three lines have been added by UDB DB2.
if [ -f /home/wasadmin/sqllib/db2profile ]; then
. /home/wasadmin/sqllib/db2profile
fi

How to move a DB2 database

chown ldapdb2 /ldapfs
su - ldapdb2
db2start
db2 force application all
db2 termincate
db2 backup db ldapdb2 to /Another_filesystem
db2 drop db ldapdb2
db2 create db ldapdb2 on /ldapfs
db2 force application all
db2 termincate
db2 restore db ldapdb2 from /Another_filesystem replace existing redirect
db2 "set tablespace containers for 3 using (path '/disks/1/3', path '/disks/2/3', path '/disks/3/3', path '/disks/4/3', path '/disks/5/3')"
db2 restore db ldapdb2 continue

Additionally, I recommend you to set the path to the DB2 log file directory on another file system to eliminate output I/O wait time as follows:
db2 update database configuration for ldapdb2 using newlogpath [path]

How to stop db2 instance when it is still active

Often I met this problem when I want to stop the db2 instance using
db2stop
SQL1025N The database manager was not stopped because databases are still active.

Here you can find a solution

This appendix describes the necessary steps to stop and start a DB2 instance. There are many ways to stop and start a DB2 instance, but the following steps will guide you to stop a DB2 instance to ensure that any defunct DB2 processes, interprocess communications, and defunct DARI processes have been removed successfully.

Current configuration:Instance: db2inst1 Database: sample Server: phantom

Stop the DB2 instance. Check existing applications that are currently connected to the database by logging on to phantom server as DB2 instance owner db2inst1:


$ db2 list applications
Auth Id   Appl. Name   Appl.. Handle   Appl. Id                       DB Name   # of Agents
-------   ----------   -------------   ----------------------------   -------   -----------
DB2INST1  db2bp        207             *LOCAL.db2inst1.010824003917   SAMPLE    1
DB2INST1  java         276             CCF21FFC.E5D8.010829004049     SAMPLE    1
DB2INST1  java         51              CCF21FFC.E5D9.010829004051     SAMPLE    1

If there is any application connected to the database, you can tell who is currently connected and from which location they are connected. In this case, there is one local connection from db2inst1 user ID, and there are two remote connections from IP address: xxx.xxx.31.252 converted from hex to decimal: CCF21FFC.

For remote connections, after you get the IP address, you can get the hostname by issuing the nslookup command:

$ /usr/sbin/nslookup xxx.242.31.252
Server:  charter.xxx.com
Address:  xxx.242.31.83
Name:    phantom.xxx.com
Address:  xxx.242.31.252 

If there are any applications connected to the database, verify that they are not currently executing:
$ db2 list applications show detail | egrep -i "executing|pending" 

If there are applications executing or pending, you can now force them off. Then verify to make sure there is no application connected to the database. If you see the following message, you're ready to stop the DB2 instance:


$ db2 force application all

DB20000I  The FORCE APPLICATION command completed successfully.
DB21024I  This command is asynchronous and may not be effective immediately.

$ db2 list applications

SQL1611W  No data was returned by Database System Monitor.  SQLSTATE=00000

Now you can stop the DB2 instance. When you get the message "SQL1064N DB2STOP processing was successful" you're ready to do the next step. If you get the message below, you must start this step again:


$ db2stop

SQL1025N  The database manager was not stopped because databases are still active.

LAST RESORT. If for some reason you cannot stop the DB2 instance or DB2 commands are hung, you must run this utility to remove the DB2 engine and client's IPC resources for that instance. This is your lifesaver:


$ ipclean

ipclean: Removing DB2 engine and client's IPC resources for db2inst1. 

Stop the DB2 Administration Server instance. Skip this step if DB2 Admin instance is not running; otherwise, execute this command:

$ db2admin stop 

Remove defunct DARI processes, DB2 background processes, or other defunct threads. List all DB2 processes for this instance:


$ ps -ef | grep db2

db2as 23797 23796  0   Aug 28 ?        0:00 db2sysc
db2as 23800 23798  0   Aug 28 ?        0:00 db2sysc
db2inst1 22229     1  0 13:08:01 pts/5    0:00 /db2/dbhome/db2inst1/sqllib/bin/db2bp 20580 5
db2as 23802 23797  0   Aug 28 ?        0:00 db2sysc
db2as 23801 23797  0   Aug 28 ?        0:00 db2sysc
db2as 23799 23797  0   Aug 28 ?        0:00 db2sysc

From the list above, we notice that there are processes belonging to the DB2 Admin services instance, so you must leave them alone. There is only one process that belongs to db2inst1, and that is a DB2 background process that did not get cleaned up after executing ipclean. Get the PID number and kill that process:

$ kill -9 22229 

Most of the time, you will see many defunct processes, and to save time, you should execute the following command instead of executing the kill -9 ${PID} command many times:


$ ps -ef | grep db2inst1 | awk '{print "kill -9 "$2}' > /tmp/kpid
$ chmod +x /tmp/kpid
$ /tmp/kpid 

Verify that no defunct processes are left. Repeat this step if necessary:


$ ps -ef | grep db2inst1 

Remove defunct interprocess communication segments.

List all memory segments:

$ ipcs -am | grep db2inst1


IPC status from  as of Thu Aug 30 13:16:55 2001
T      ID            KEY            MODE           OWNER          GROUP
Shared Memory:
m      9910          0x74006380     --rw-rw-rw-    db2inst1       db2grp
m      59714         0x61006380     --rw-------    db2inst1       db2grp 

From the list above, you notice that there are two memory segments that were not removed when executing ipclean. You must remove them manually:


$ ipcrm -m 9910
$ ipcrm -m 59714 

List all semaphore segments:


$ ipcs -as | grep db2inst1


IPC status from  as of Thu Aug 30 13:16:55 2001
T      ID          KEY          MODE         OWNER        GROUP
Shared Memory:
s      1900549     0x74006380   --ra-ra-ra-  db2inst1     db2grp   1
s      1310727     00000000     --ra-ra----  db2inst1     db2grp   1
s      2031624     0x73006380   --ra-ra-ra-  db2inst1     db2grp   1 

From the list above, notice that there are three semaphore segments that were not removed after executing ipclean. You must remove them manually:


$ ipcrm -s 1900549
$ ipcrm -s 1310727
$ ipcrm -s 2031624 

List all message queue segments:


$ ipcs -aq | grep db2inst1

IPC status from  as of Thu Aug 30 13:16:55 2001
T           ID         KEY         MODE         OWNER     GROUP
Shared Memory:
q           1572868    0x01dadd16  -Rrw-------  db2inst1  db2grp 65535
q           901125     0x01eba5ed  --rw-------  db2inst1  db2grp 65535
q           1609739    00000000    --rw-------  db2inst1  db2grp 65535
q           659468     00000000    -Rrw-------  db2inst1  db2grp 65535 

From the list above, notice that there are four message queue segments that were not removed after executing ipclean. You must remove them manually:


$ ipcrm -q 1572868
$ ipcrm -q 901125
$ ipcrm -q 1609739
$ ipcrm -q 659468 

Verify that there are no defunct interprocess communications left. Repeat this step if necessary:

$ ipcs -a | grep db2inst1 

Before you start the DB2 instance, it is best practice to back up the previous db2diag.log, any event logs, notification log, and the associated trap files, and start with a fresh copy. Move the current db2diag.log to the backup directory:


$ mkdir -p /db2/backup/db2inst1/diaglogSep12
$ cd /db2/dbhome/db2inst1/sqllib/db2dump
$ mv db2diag.log /db2/backup/db2inst1/diaglogSep12/
$ mv db2eventlog* /db2/backup/db2inst1/diaglogSep12/
$ mv db2inst1.nfy /db2/backup/db2inst1/diaglogSep12/
$ touch db2diag.log db2inst1.nfy db2eventlog.nnn where nnn is the database partition number
$ chmod 664 db2diag.log db2inst1.nfy db2eventlog.*

If there are any trap files, group them together:


$ cd /db2/dbhome/db2inst1/sqllib/db2dump
$ tar -cvf /db2/backup/db2inst1/diaglog/trapAug292001.tar t* c* l* [0-9]* 

Or execute this keepDiagLog.sh script:


#!/bin/ksh
#
# Clean up db2diag.log, trap files, dump files, etc
#
# Usage:  keepDiagLog.sh
#
# Execute as DB2 instance owner
#
LOGTIME=`date '+%y%m%d%H%M%S'`
DIAGDIR=${HOME}/sqllib/db2dump
typeset instname=${1-db2inst1}
typeset ROOTDIR=${2-/dbbackup}
typeset dbname=${3-sample}
typeset OLDDIR=${4-${ROOTDIR}/${instname}/${dbname}/db2diag${LOGTIME}}
mkdir -p ${OLDDIR}
cd ${DIAGDIR}
cp -r * ${OLDDIR}/
for j in `ls`
do
  if  [ -d "${j}" ]; then
    rm -r ${j}
  else
    rm ${j}
  fi
done
touch db2diag.log ${instname}.nfy
chmod 666 db2diag.log ${instname}.nfy
exit 0
# You need to add the steps for the event log files based on the
# number of database partitions defined on your server.

Now you're ready to start the DB2 instance. Start the DB2 instance:


$ db2start

SQL1063N  DB2START processing was successful. 

And you're ready to start the DB2 Admin instance. Start the DB2 Admin instance:


$ db2admin start 

Verify the database connection.

Connect to the sample database:


$ db2 connect to sample

   Database Connection Information
 Database server        = DB2/SUN 8.1.0
 SQL authorization ID   = DB2INST1
 Local database alias   = SAMPLE 

Disconnect from the sample database:

 $ db2 terminate 

Reactivate the database to improve performance.

Activate the sample database:


$ db2 activate database sample
DB20000I  The ACTIVATE DATABASE command completed successfully. 

1


@HowTo @IBM