Jonathan Hutchins' Blog
Thursday, March 16, 2017
 
Connecting to a remote DB2 Database with PHP from RHEL6
Connecting to a remote database using PHP on RHEL6.

Starting from a default server installation with Apache and PHP.

Jonathan R. Hutchins, 10 February 2012
------------------------------------------------------------------

Add the "Optional" software channel to your channel subscriptions for the machine. This is usually done from the administrative subscription management screen at http://rhn.redhat.com.

This will allow you to get php-devel, which is required for installing modules with pecl.
You will also need to install gcc.


Add user db2inst1 which will have access to the CLI db2 connection for testing.


mkdir /db2home
groupadd -g 999 db2iadm1
useradd -u 1004 -g db2iadm1 -m -d /db2home/db2inst1 db2inst1
passwd db2inst1
?


Install the DB2 Data Server Client. This is currently provided in v9.7fp5_linuxia32_client.tar.gz from IBM. Un-tar the file, change to the ~/client directory it creates, and run ./db2_install

Create the db2 "instance" which creates the files, links, and sets the environment for user db2inst1:

/opt/ibm/db2/V9.7/instance/db2icrt db2inst1

Map the remote connections to local aliases for easier connectivity. (This also allows the remote database to be changed and re-mapped without having to change internal code.)

su - db2inst1

The following commands catalog remote database DMTEST on server GENDATAT port 50015 as local database DMTEST and remote database DMPROD on GENDATAB3 port 50010 as DMPROD:

db2 "catalog tcpip node GDT remote GENDATAT server 50015"
db2 "catalog tcpip node GDB3 remote GENDATAB3 server 50010"
db2 terminate
db2 "catalog database DMTEST as DMTEST at node GDT"
db2 "catalog database DMPROD as DMPROD at node GDB3"
db2 terminate

The terminate command flushes the cache and is necessary for the catalogged items to be used.

Typeos can be corrected with the command

db2 "uncatalog [node|database] "

Note that the local database alias and node names can not exceed 8 characters, hence the abbreviated node names above.

Now db2 connect to DMTEST user should prompt for a password.

Next, install the PHP module:

pecl install ibm_db2

Either add the following line to php.ini (/etc/php.ini) or create a file in /etc/php.d/ with this line:

extension=ibm_db2.so

Current practice is to use the file in php.ini - may require an ini extension.

Restart the apache server. You can use phpinfo () to check that it's enabled.

You should now be able to use php -r "echo db2_connect ('DMTEST','', '');", which should return the connection status.


Powered by Blogger