Search code examples
phpubuntupdodb2odbc

PHP to DB2 with PDO_IBM Error : SQLDriverConnect: -1390 [IBM] [CLIDRIVER] SQL10007N


In Ubuntu 22.04 I am attempting to connect PHP with a DB2 database via the driver pdo_ibm.so

I have followed this guide, along with IBM documentation: https://github.com/php/pecl-database-pdo_ibm

I've compiled version 1.5.0. into the ".so" file and placed it on the respective driver's folder.

However I am getting the following error

enter image description here

SQLDriverConnect: -1390 [IBM] [CLIDRIVER] SQL10007N Message "0" could not be retrieved. Reason code "3".

Using the following PHP code:

var_dump($dsn);
echo "". PHP_EOL;
var_dump($this->_config['username']);
var_dump($this->_config['password']);
var_dump($this->_config['driver_options']);

$this->_connection = new PDO(
$dsn,
$this->_config['username'],
$this->_config['password'],
$this->_config['driver_options']
);

/*$this->_connection = new PDO("ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=testdb;" .
"HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;", "db2inst1", "123456", array(
PDO::ATTR_PERSISTENT => FALSE, 
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
); */

I am using the follwing env variables, set on the ~/.bashrc file:

ODBCHOME=/home/poc-dev/etc

ODBCINI=/home/poc-dev/clidriver/system_odbc.ini

DB2INSTANCE=db2inst1

DB2HOME=/home/poc-dev/clidriver

IBM_DB_HOME=/home/poc-dev/clidriver

LD_LIBRARY_PATH=/home/poc-dev/clidriver/lib

_=/usr/bin/printenv

I have succesfully connected with DBeaver and ISql to the database

What am I missing?

Edit: The user running PHP is "www-data" image of apache envvars: enter image description here


Solution

  • Apache2 uses the "www-data" user to run PHP. As that "www-data" has to have full access to the DB2 CLIDRIVER in all senses as user mao pointed out.

    I have found that I had to place the exact same variables as ~/.bashrc in the etc/enviroment file (I was mistakenly thinking I made them global), after I did that I could see the enviroment variables as the "www-data" user which I verified via the command:

    sudo -u www-data printenv
    

    I confirmed the ENV-variables for the DB2 CLIDRIVER files folder: /home/poc-dev/clidriver were there, but they had 755 permissions which I needed to change to 777 permission via the command:

    sudo chmod 777 -R /home/poc-dev/ 
    

    After that I tested the ISQL command on the user "www-data" by the command (since the first time I realized I was getting a message where ISQL could not find one of the libraries, which turned out to be a permissions problem):

    sudo -u www-data isql TEST db2inst1 123456
    

    "TEST" being the registered DSN in odbc.ini, "db2inst1" being the user on the dataserver (in a virtual machine in this case)

    Then I proceded to use:

    $this->_connection = new PDO("ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=testdb;" .
    "HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;", "db2inst1", "123456", array(
    PDO::ATTR_PERSISTENT => FALSE, 
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
    );
    

    In PHP. Hopefully this helps someone