One day I needed a DB2 for proof-of-concept testing. IBM has a "free" Express-C-edition available with restricted license.
Altough there is a wide range of native drivers for DB2 available, but I discovered that most of them required whole lot of more work than generic ODBC (in case of PHP for example). For the sake of simplicity using ODBC was suggested in some other postings I found, so that was the approach I decided to take as well. Generally said, this is not production-grade setup, please don't use this as a one.
Here's what steps should be taken to get it up and running:
1) Download "DB2 Express-C for Linux 64-bit" from IBM, it's located at the time of writing here: http://www-01.ibm.com/software/data/db2/express-c/download.html
2) On the test system:
tar zxvf v10.5_linuxx64_expc.tar.gz
3) Install the database (without X11 installer). I found out that the X11 installer ("db2setup") does not always work as expected, so skip that one and go directly to manual installation. Execute using root privileges:
./expc/db2prereqcheck
This will report the issues with the prerequirements which I did have apparently plenty. Fortunately for testing you just need fulfill the requirements for "Checking prerequisites for DB2 installation. Version "10.5.0.5". Operating system "Linux"" and not for the pureScale option, so just check the first part of the tools output.
These 32-bit packages were missing from the installation I had to install:
yum install libstdc++.i686 pam.i686
And then (horrible) put selinux on permissive mode (this a test system):
edit /etc/selinux/config, mode=permissive
hint: Remember to reboot to get the selinux config active.
4) You should be ready to install the database itself regardless of complaint about the db2_install-tool is deprecated:
[root@localhost ~]# ./expc/db2_install
Answer "yes" for the question about the path under /opt/ibm/db2. The installation should run successfully.
5) Add the users and configure the instance and populate with sample data:
[root@localhost ~]# useradd db2inst1
[root@localhost ~]# passwd db2inst1 (use the password of your choice)
[root@localhost ~]# /opt/ibm/db2/V10.5/instance/db2icrt -p 50000 -u db2inst1 db2inst1
Then start up the server and populate the sample data:
[root@localhost ~]# su - db2inst1
[db2inst1@localhost ~]$ db2start
SQL1063N DB2START processing was successful.
[db2inst1@localhost ~]$ db2sampl
Creating database "SAMPLE"...
Connecting to database "SAMPLE"...
Creating tables and data in schema "DB2INST1"...
Creating tables with XML columns and XML data in schema "DB2INST1"...
'db2sampl' processing complete.
[db2inst1@localhost ~]$
6) Test you're able to see some sample data:
[db2inst1@localhost ~]$ db2
...
db2 => select * from EMPLOYEE
...
EMPNO FIRSTNME MIDINIT LASTNAME WORKDEPT PHONENO HIREDATE JOB EDLEVEL SEX BIRTHDATE SALARY BONUS COMM
------ ------------ ------- --------------- -------- ------- ---------- -------- ------- --- ---------- ----------- ----------- -----------
000010 CHRISTINE I HAAS A00 3978 01/01/1995 PRES 18 F 08/24/1963 152750.00 1000.00 4220.00
000020 MICHAEL L THOMPSON B01 3476 10/10/2003 MANAGER 18 M 02/02/1978 94250.00 800.00 3300.00
...
7) Success - It's working. Now you should have DB2 running on port 50000/tcp. You can have a try with a SQL tool and DB2 driver (java drivers have been included with the package, db2jcc_license_cu.jar and db2jcc4.jar in /opt/ibm/db2/V10.5/java).
JDBC URL: jdbc:db2://[host or ip]:50000/SAMPLE
Username: db2inst1
Password: [whatever you used in step 5]
Hint: Make sure there is no firewall blocking the port.
8) Now to the ODBC setup:
[root@localhost ~]# yum install unixODBC
Add to /etc/odbcinst.ini the following lines:
[DB2]
Description = DB2 Driver
Driver64 = /opt/ibm/db2/V10.5/lib64/libdb2.so
FileUsage = 1
And for testing purposes create a file /etc/odbc.ini with contents:
9) Test the setup using ODBC isql tool:
[root@localhost ~]# isql -v SAMPLE db2inst1 [your password here]
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> SELECT * FROM EMPLOYEE
+-------+-------------+--------+----------------+---------+--------+-----------+---------+--------+----+-----------+------------+------------+------------+
| EMPNO | FIRSTNME | MIDINIT| LASTNAME | WORKDEPT| PHONENO| HIREDATE | JOB | EDLEVEL| SEX| BIRTHDATE | SALARY | BONUS | COMM |
+-------+-------------+--------+----------------+---------+--------+-----------+---------+--------+----+-----------+------------+------------+------------+
| 000010| CHRISTINE | I | HAAS | A00 | 3978 | 1995-01-01| PRES | 18 | F | 1963-08-24| 152750.00 | 1000.00 | 4220.00 |
...
10) It's working. Now proceed to use the ODBC with PHP or whatever you like.
ODBC DSN using host/port without odbc.ini would be:
'DRIVER={DB2};DATABASE=SAMPLE;SYSTEM=localhost;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP'
ODBC DSN without odbc.ini (the database on the same system) would be:
'DRIVER={DB2};DATABASE=SAMPLE'
ODBC DSN using the database alias defined in odbc.ini would be as follows:
'SAMPLE'
Happy hacking with DB2 and ODBC!