phpdb2odbczend-serverpdo-odbc

Cannot connect to DB2 via PDO_ODBC


I got odbc.ini setup with DSN to local DB2 Community edition v11.5.7 database. It runs on port 25010. I've verified it can connect via telnet localhost 25010. PDO_ODBC extension is also installed and enabled.

The problem came when trying to connect through remote client via PDO_ODBC using PHP, it returns error below:

Fatal error: Uncaught PDOException: SQLSTATE[08004] SQLDriverConnect: 10061 [unixODBC][IBM][System i Access ODBC Driver]Communication link failure. comm rc=10061 - CWBCO1049 - The IBM i server application is not started, or the connection was blocked by a firewall

After ran tcpdump, it seems that it ALWAYS tries to use port 8471.

My code

<?php
$conn = new PDO('odbc:DSN=SAMPLE;NAM=1;UID=db2inst1;PWD=password');
$sql = "select * from SAMPLE.EMPLOYEE";
$query = $conn->prepare($sql);

$query->execute(); 

$rows = $query->fetchAll();
print_r($rows); 

Also tried isql client, the exactly same result.

isql localhost SAMPLE

PHP is 7.4 on ZendServer 2021. OS is Redhat Enterprise 7.9.

Below is odbc.ini

[SAMPLE]
Description             = IBM i Access ODBC Driver
Driver                  = IBM i Access ODBC Driver
System                  = localhost
Port                    = 25010
ServiceName             = db2c_db2inst1
#Trace=Yes
#TraceFile=/tmp/sql.log
UserID                  = DB2INST1
Password                = password
Naming                  = 0
Database                = SAMPLE
CommitMode              = 1

Why is it always trying to connect to the wrong port 8471? How can I fix it?


Solution

  • To access a Db2-server from PHP, it is necessary to have the correct flavour of ODBC driver that matches your Db2-server platform. The platform of Db2-server is one of Db2-LUW, or i-series (as400), or Z/OS mainframe. In your case, the platform is Db2-LUW because your question says you are using RHEL Redhat enterprise linux.

    Separately, it is essential to get isql connecting to your Db2-server from the same hostname that will run PHP - in other words, if isql cannot connect to your Db2-database, then PHP also will not connect.

    In this case, the Db2-server is running on RHEL , so the platform is "Db2-LUW" (Db2 for Linux/Unix/Windows), and to access that platform, a CLI driver is needed. CLI is IBM's word (Call Level Interface) for ODBC.

    You can get a CLI driver for Db2-LUW either by downloading a free tiny footprint one (called clidriver) via this link , or by installing a (much bigger footprint full function ) Db2-client product (such as the IBM Db2 data server client).

    After downloading and installing your clidriver by carefully following the instructions provided by IBM, you will need to configure both the odbcinst.ini and the odbc.ini with the correct details before isql can connect.

    Depending on the operating system that will run PHP, you may also need to configure additional environment variables specifically for the userid/account that will run PHP before the connection can succeed, which on Linux x64 usually means LD_LIBRARY_PATH (see instructions here and here )

    Further information provided by IBM Db2-LUW documentation for setting up PHP to Db2-LUW is here.

    There are also additional worked examples, and some troubleshooting information here.