|
Getting Started With IBM DB2
I've sucessfully installed DB2 V7.2 on Linux. I've never had access to any other version of DB2 for Linux or any version of DB2 for other platforms. Install pdkshFirst and foremost, you need pdksh (Public Domain Korn Shell). If it did not come with your distribution, you'll need to download and install it. For RPM based distributions, look for pdksh at rpmfind and install it using rpm -i. For Debian Linux, on newer Debian releases, if you have configured apt as illustrated in the APT-HOWTO, run apt-get install rpm. If your distribution came with pdksh, you may be prompted to enter a CD. If not, it will be downloaded from the internet.
For Slackware Linux, the pdksh package is available from ftp.slackware.com. You can install it using installpkg. Install libncurses.so.4DB2 requires libncurses.so.4. Many older distributions come with libncurses.so.4. Many newer distributions come with libncurses.so.5 (or newer). Some distributions come with both. If your distribution does NOT have libncurses.so.4, create a symbolic link to the newer library as follows. (These instructions assume that your system has ncurses version 5.2 and that libncurses.so.5.2 resides in /usr/lib. If your system has a different version of ncurses or if it resides somewhere else (for example, in /lib) then modify the instructions accordingly) cd /usr/libInstall RPM On non-RPM based systems, you'll need to install RPM. For Slackware Linux, the RPM package is available from ftp.slackware.com. You can install it using installpkg. Once RPM is installed, log in as root and run rpm --initdb. On newer Debian releases, if you have configured apt as illustrated in the APT-HOWTO, run apt-get install rpm. If your distribution came with RPM, you may be prompted to enter a CD. If not, it will be downloaded from the internet. Once RPM is installed, log in as root and run rpm --initdb. On systems where the rpm command resides in /usr/bin, you need to create a symbolic link between /usr/bin/rpm and /bin/rpm as follows. ln -s /usr/bin/rpm /bin/rpmInstall Compatibility Packages DB2 V7.2 for Linux requires glibc 2.1. Many newer Linux distributions come with glibc 2.2. On these distributions it is necessary to install some compatibility packages. Distributions that come with glibc 2.1 include RedHat 6, Debian 2.2, Mandrake 7 and Slackware 7. Distributions that come with glibc 2.2 include RedHat 7, Mandrake 8 and Slackware 8. RedHat provides compatability RPM's for RedHat 7 that can actually be used on any Linux that comes with glibc 2.2. These RPM's can be downloaded from rpmfind. You'll need the following RPM's.
You can install the compatibility RPM's using rpm -i. PATH Environment VariableOn TurboLinux (and possibly other distributions as well), the DB2 installer tries to run some command line programs that are found in /sbin and /usr/sbin. Make sure that these directories are in the root user's PATH. DB2 InstallationIBM DB2 V7.2 Personal Developer's Edition is available from the IBM website as a tarball. As root, create a temporary directory, move the tarball into it and extract it using tar xf. The extraction process creates a directory called 018_EEE_LNX_NLV. Change directories to 018_EEE_LNX_NLV. On Debian Linux and Slackware Linux, edit the db2_install script and comment out the line: rpm -qa | grep rpm 1>/dev/null 2>/dev/null Run the db2_install script. When prompted to specify a keyword, type DB2.EENT and press return. When the script completes, run rpm -qa and verify that the following RPM's were installed: db2cnvc71-7.1.0-40Restore unixODBC if Necessary The DB2 installation creates symbolic links from include files in /usr/IBMdb2/V7.1/include to /usr/include. If you had a unixODBC-devel RPM installed prior to installing DB2, the include files provided by the unixODBC-devel RPM will have been overwritten by these symbolic links. You should probably reinstall the unixODBC-devel RPM. SQL Relay looks in /usr/IBMdb2/V7.1/include for DB2 includes, so not having them in /usr/include won't hurt, but not having the proper unixODBC includes in /usr/include will cause the ODBC database connection build to fail. Ignore the MisinformationThere are many, many instances where this DB2 distribution identifies itself as version 7.1 instead of 7.2. For example, the RPM's have a 7.1.0 version number and the installation process created the directory /usr/IBMdb2/V7.1. These clues might lead one to think that he or she has accidentally installed version 7.1 instead of version 7.2. Ignore the clues. Rest assured that you have installed version 7.2 despite the misinformation. Install the DocumentationOnce the RPM's are installed, you can install the documentation by running /usr/IBMdb2/V7.1/doc/db2insthtml en_US. To view the documentation, point your browser at /usr/IBMdb2/V7.1/doc/en_US/html. Creating a DatabaseA DB2 installation consists of an administration server and one or more instances. Each instance contains one or more databases. The administration server coordinates the instances. The administration server is owned by an OS-level user. Each instance is owned by a seperate OS-level user as well. To create an instance and an administration server, run /usr/IBMdb2/V7.1/install/db2setup, a text-based installation program. Select [ Create... ] Should you need to create additional DB2 instances, follow this procedure again using a different user name and skipping the steps pertaining to the Administration Server. The /usr/IBMdb2/V.1/instance/db2ilist command lists all of the DB2 instances on the local computer. If you need to drop an instance, run /usr/IBMdb2/V.1/instance/db2idrop instancename, substituting the name of the instance you want to drop for instancename. The /usr/IBMdb2/V.1/instance/dasilist command lists all of the DB2 administration servers on the local computer. If you need to drop an administration server, run /usr/IBMdb2/V.1/instance/dasidrop servername, substituting the name of the administration server you want to drop for servername. Now that you have created an instance, you must create a database within the instance. Log in as db2inst1 and run the following command to create a database named testdb. db2 "create database testdb" Should you need to drop a database, the following command drops a database named testdb. db2 "drop database testdb" By default, a DB2 instance only accept connections from clients running on the local machine. To enable connections from clients running on a remote machine over TCP, execute commands like the following. These commands set up the db2inst1 instance on port 50000. log in as root Now the db2inst1 instance should be accessible to remote clients on port 50000. This should be enough you get you started. To set up more complex configurations, consult the documentation that came with your DB2 distribution. Starting the Database at Boot TimeYou can use the following script to start/stop the database at boot/shutdown time. #!/bin/sh case "$1" in start) /usr/IBMdb2/V7.1/instance/db2istrt ;; stop) /usr/IBMdb2/V7.1/instance/db2ishut ;; *) echo $"Usage: $0 {start|stop}" exit 1 esac exit 0 Install this script and run it with the "start" option to start up the database. Running it with the "stop" option shuts the database down. To access a database, it must be running. Accessing a DatabaseAccessing a Local Database Accessing a local DB2 database using the db2 client tool is simple. For example, to access a database called testdb in the db2inst1 instance, owned by the db2inst1 user on the local machine, log in as db2inst1 and run the db2 command. Then use the CONNECT command to connect to a particular database. Accessing a Remote Database[db2inst1@localhost db2inst1]$ db2 c(c) Copyright IBM Corporation 1993,2001 Command Line Processor for DB2 SDK 7.2.0 You can issue database manager commands and SQL statements from the command prompt. For example: db2 => connect to sample db2 => bind sample.bnd For general help, type: ?. For command help, type: ? command, where command can be the first few keywords of a database manager command. For example: ? CATALOG DATABASE for help on the CATALOG DATABASE command ? CATALOG for help on all of the CATALOG commands. To exit db2 interactive mode, type QUIT at the command prompt. Outside interactive mode, all commands must be prefixed with 'db2'. To list the current command option settings, type LIST COMMAND OPTIONS. For more detailed help, refer to the Online Reference Manual. db2 => connect to testdb Database Connection Information Database server = DB2/LINUX 7.2.0 SQL authorization ID = DB2INST1 Local database alias = TESTDB If you want to access a database on a remote machine, the process is more complex. DB2 or "DB2 Connect" must be installed on the local and remote machines and a DB2 or "DB2 Connect" instance must be created on the local machine. "DB2 Connect" is a lightweight version of the DB2 database system whose instances cannot support a local database, and can only provide connectivity to a remote instance. In the instance on the local machine, use the CATALOG command to create aliases for the remote instance and database. Once these aliases are created, you can use the database alias to connect to the remote database. Take the following scenario:
On localhost, in the db2inst1 instance, to create an instance alias named remoteinst referring to testinst on remotehost, and a database alias named remotedb referring to testdb, follow this procedure. log in as db2inst1 on localhost Now that the aliases have been created, you can connect to the database alias using the CONNECT comamnd. Note that you must supply the user name and password. [db2inst1@localhost db2inst1]$ db2 c(c) Copyright IBM Corporation 1993,2001 Command Line Processor for DB2 SDK 7.2.0 You can issue database manager commands and SQL statements from the command prompt. For example: db2 => connect to sample db2 => bind sample.bnd For general help, type: ?. For command help, type: ? command, where command can be the first few keywords of a database manager command. For example: ? CATALOG DATABASE for help on the CATALOG DATABASE command ? CATALOG for help on all of the CATALOG commands. To exit db2 interactive mode, type QUIT at the command prompt. Outside interactive mode, all commands must be prefixed with 'db2'. To list the current command option settings, type LIST COMMAND OPTIONS. For more detailed help, refer to the Online Reference Manual. db2 => connect to remotedb user testinst using testpass Database Connection Information Database server = DB2/LINUX 7.2.0 SQL authorization ID = TESTINST Local database alias = REMOTEDB If you need to get a list of instance aliases, you can use the LIST NODE DIRECTORY command as follows. db2 "list node directory" To list the database aliases, you can use the LIST DATABASE DIRECTORY command. Note that this command lists local databases as well as remote databases. db2 "list database directory" If you need to drop a database or instance alias, use the UNCATALOG command. The following commands remove the remotedb database alias and the remoteinst instance alias. db2 "uncatalog database remotedb"Using the DB2 Client Program When run with no arguments, the db2 client program provides an interactive shell, prompting you to enter commands or an SQL queries. Commands or queries must be entered on a single line and will run when the Enter or Return key is pressed. To exit, type quit. A sample db2 session follows. Using the DB2 Information and Control Centers[db2inst1@localhost db2inst1]$ db2 c(c) Copyright IBM Corporation 1993,2001 Command Line Processor for DB2 SDK 7.2.0 You can issue database manager commands and SQL statements from the command prompt. For example: db2 => connect to sample db2 => bind sample.bnd For general help, type: ?. For command help, type: ? command, where command can be the first few keywords of a database manager command. For example: ? CATALOG DATABASE for help on the CATALOG DATABASE command ? CATALOG for help on all of the CATALOG commands. To exit db2 interactive mode, type QUIT at the command prompt. Outside interactive mode, all commands must be prefixed with 'db2'. To list the current command option settings, type LIST COMMAND OPTIONS. For more detailed help, refer to the Online Reference Manual. db2 => connect to testdb Database Connection Information Database server = DB2/LINUX 7.2.0 SQL authorization ID = DB2INST1 Local database alias = TESTDB db2 => create table testtable (col1 char(40), col2 int) DB20000I The SQL command completed successfully. db2 => list tables Table/View Schema Type Creation time ------------------------------- --------------- ----- -------------------------- TESTTABLE DB2INST1 T 2002-03-01-01.55.19.671629 1 record(s) selected. db2 => describe table testtable show detail Column Type Column Partitioning key Code name schema Type name Length Scale Nulls number sequence page Default ------------------------------ ------------------------------- ------------------------------- --------- -------- -------- ---------- ------------------- -------- ---------------------------------------- COL1 SYSIBM CHARACTER 40 0 Yes 0 0 819 COL2 SYSIBM INTEGER 4 0 Yes 1 0 0 2 record(s) selected. db2 => insert into testtable values ('hello',50) DB20000I The SQL command completed successfully. db2 => insert into testtable values ('hi',60) DB20000I The SQL command completed successfully. db2 => insert into testtable values ('bye',70) DB20000I The SQL command completed successfully. db2 => select * from testtable COL1 COL2 ---------------------------------------- ----------- hello 50 hi 60 bye 70 3 record(s) selected. db2 => update testtable set col2=0 where col1='hi' DB20000I The SQL command completed successfully. db2 => select * from testtable COL1 COL2 ---------------------------------------- ----------- hello 50 hi 0 bye 70 3 record(s) selected. db2 => delete from testtable where col2=50 DB20000I The SQL command completed successfully. db2 => select * from testtable COL1 COL2 ---------------------------------------- ----------- hi 0 bye 70 2 record(s) selected. db2 => drop table testtable DB20000I The SQL command completed successfully. db2 => quit DB20000I The QUIT command completed successfully. IBM DB2 comes with a set of user-friendly, Java-based GUI utilities for browsing and configuring database instances. These utilities require the Java Runtime Enviroment version 1.1.8 or higher. For Linux, JRE's are available as RPM's or tarballs from Blackdown, Sun and IBM. Of course, IBM recommends that you use their JRE. To run the DB2 Information Center or Control Center, you have to log in as a database instance or adminstrative server owner such as db2as or db2inst1. Make sure that the jre command is in the PATH environment variable for that user. If it isn't, modify that user's .bashrc to include it. If you are running a 2.4 (or higher) kernel, add the following line to the user's .bashrc as well. export LD_ASSUME_KERNEL=2.2.5 To run the Information Center or Control Center, you first have to run the DB2 Java Server and give it a port number as follows. db2jstrt 6720 Once it's running, you can run the DB2 Information Center or Control Center, also supplying the port. db2ic 6720 Both utilities will ask for the username and password of an instance owner. Once you supply these credentials, you can browse and/or configure the instance owned by that user. Accessing a Database With SQL RelayAccessing DB2 from SQL Relay requires an instance entry in your sqlrelay.conf file for the database that you want to access. Here is an example sqlrelay.conf which defines an SQL Relay instance called db2test. This SQL Relay instance connects to the testdb database in the db2inst1 DB2 instance. Important Note: For DB2 connections, sqlr-start must be run as the user that owns the DB2 instance that it is running against. In this example, the db2inst1 user owns the db2inst1 instance where the testdb database resides, so sqlr-start must be run as db2inst1. <!DOCTYPE instances SYSTEM "sqlrelay.dtd"> <instances> <instance id="db2test" port="9000" socket="/tmp/db2test.socket" dbase="db2" connections="3" maxconnections="5" maxqueuelength="0" growby="1" ttl="60" endofsession="commit" sessiontimeout="600" runasuser="db2inst1" runasgroup="db2inst1" cursors="5" authtier="listener" handoff="pass"> <users> <user user="db2test" password="db2test"/> </users> <connections> <connection connectionid="db2test" string="db=db2inst1" metric="1"/> </connections> </instance> </instances> Now you can start up this instance with the following command. su - db2inst1 -c "sqlr-start -id db2test" To connect to the instance and run queries, use the following command. sqlrsh -id db2test The following command shuts down the SQL Relay instance. su - db2inst1 -c "sqlr-stop db2test" |