0

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?

devXen
  • 3,013
  • 3
  • 35
  • 44
  • Why is it tagged websphere and ibm-midrange? Please correct. You have to share the code and the configuration, not just the error message. – data_henrik Aug 16 '22 at 05:41
  • 1
    You get this symptom when your `odbinst.ini` and `odbc.ini` are incorrectly configured. Until and unless your remote client `isql` can connect, then PHP on your remote-client will also not connect. So it is essential to get `isql` connecting __first__, then try php. Next, it is essential that your remote client is using the very __latest__ odbc driver (so get that right first). Ensure the odbc driver matches the Db2-server platform. If you still fail, then edit your question with relevant facts. – mao Aug 16 '22 at 07:19
  • Wrong driver. This driver is used for Db2 for IBM i (aka OS/400, iSeries, System i) connectivity, not for Db2 for Linux, Unix and Windows. – Mark Barinstein Aug 16 '22 at 07:19
  • @MarkBarinstein this drivers indeeds works. Tried on Pub400 (public as/400). – devXen Aug 16 '22 at 16:03
  • 2
    @devXen Just in case. Db2 for IBM i and Db2 for LUW are different products. Again, the driver you try to use is specific to Db2 for IBM i and can't be used to access Db2 for LUW databases. – Mark Barinstein Aug 16 '22 at 16:14
  • PDO ODBC should work on both products now AFAIK. Reference: https://www.gateway400.org/documents/Gateway400/Handouts/Back%20to%20the%20Future%20with%20ODBC%20on%20IBM%20i%20--%20Seiden%20Group%20--%20Gateway%20User%20Group%202021-01-14.pdf – devXen Aug 16 '22 at 16:44
  • @data_henrik just edit the post to include additional info – devXen Aug 16 '22 at 16:47
  • @devXen If your Db2-server is __also__ running on RHEL (i.e. the Db2-server is Db2-LUW) then you __cannot__ use the i-series access driver. You can __only__ use that that driver when Db2-server is running on as400 i-series. Instead, you can use the (free) clidriver for Db2-LUW downloadable from https://www.ibm.com/support/pages/db2-odbc-cli-driver-download-and-installation-information (or any supported Db2-client that has a CLI( = odbc) driver). The PDO will work with either driver, but the driver itself must be compatible with the Db2-server platform. – mao Aug 16 '22 at 16:51
  • Forgot to mention I'm a DB2 newbie. So what you are saying is The IBM Data Server Driver for ODBC is for DB2 Luw, IBM i Access DB2 Driver is only for DB2 as400 and cannot be used for Luw. Is that right? If so then both @MarkBarinstein and you are right. – devXen Aug 16 '22 at 17:29
  • 3
    Yes, that is what we are telling you. The clue is in the title "IBM i access driver..." is for accessing i-series. You need the clidriver on the hostname running PHP, and your odbcinst.ini must refer to the shared library of the clidriver, and your odbc.ini must specify the correct port number, hostname etc, otherwise you will fail to get isql connecting (and it is essential to get isql working before you can get php working). – mao Aug 16 '22 at 17:32
  • Thanks so much. Should've posted it here two days ago! – devXen Aug 16 '22 at 17:51

1 Answers1

1

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.

mao
  • 11,321
  • 2
  • 13
  • 29