简体   繁体   中英

How do I connect to DB2 via ibm_db python package

I'm trying to connect with an on-prem DB2 database via python but have been unsuccessful over the past few days. I am hoping the community can help me decipher error messages and offer help from their experience.

My goal is to install the Python ibm_db library on my Windows 7 laptop and connect to our DB2 instance

I cannot get the ibm_db.connect() method to recognize system DSN or user DSN that are known good (I use them to connect to DB2 via Sql Server Management Studio (SSMS))

I get the below errors from various connection combinations (shown below) but I have not found any reference that says what the errors mean, and what I may be able to do to resolve the issue. I have searched and read dozens of IBM websites and documents, but none mention this information. --> I have a feeling I am missing some simple step, but i don't know what i don't know at this point :)

Notes on System Configuration ::

  • Windows 7 laptop

  • DB2 version 7.3

  • Python 2.7.13 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:17:26) [MSC v.1500 64 bit (AMD64)] on win32

  • Python library versions installed that seem pertinent

  • ibm-db (2.0.8)
  • ibm-db-sa (0.3.3)
  • pyodbc (4.0.16)
  • pyOpenSSL (17.0.0)

  • I have the iSeries Navigator installed and working, which included the iSeries ODBC driver. It works perfectly to connect to on-prem DB2 instances

  • I have the DB2 Command Line interface installed, which added a DRIVER with name IBM DB2 ODBC DRIVER - C_clidriver in the registry and in the 64-bit ODBC Manager driver list

  • Here is a File DSN I created from the iSeries ODBC driver that works, but look quite different from the DSN I'm using for the IBM CLI driver:

    [ODBC] DRIVER=iSeries Access ODBC Driver DATABASE=DBASE PKG=QGPL/DEFAULT(IBM),2,0,1,0,512 LANGUAGEID=ENU DFTPKGLIB=QGPL DBQ=QGPL SYSTEM=DBSYS

First Attempt :: basic connection with a full DSN specified:

>>> import ibm_db
>>> dsn = "DATABASE=dbname;HOSTNAME=hostname.domain;PORT=466;PROTOCOL=TCPIP"
>>> conn = ibm_db.connect(dsn,'mytextuid','password')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver] SQL10007N Message "0" could not be retrieved.  
  **Reason code: "6". SQLCODE=-1366**

Next :: Add a driver with name with EXACT driver name as is in the registry

>>> dsn = "DRIVER={IBM DB2 ODBC DRIVER - C_clidriver};HOSTNAME=hostname.domain;PORT=446;PROTOCOL=TCPIP;UID=mytextuid;PWD=password;"
>>> conn = ibm_db.connect(dsn,'CROWLEY','July2017')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver]  
  **SQLSTATE=01S00 SQLCODE=-99999**

Next :: try a different format DSN, combining DATABASE, HOSTNAME, AND PORT

>>> dsn = "DATABASE=Dbname:hostname.domain:466;PROTOCOL=TCPIP"
>>> conn = ibm_db.connect(dsn,'mytextuid','password')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver] SQL10007N Message "0" could not be retrieved.  
  **Reason code: "6". SQLCODE=-1024**

Next :: try a System DSN that is Known Good because I use it in SSMS

>>> conn = ibm_db.connect('DSN=SYSTEM_DB2_DSN','mytextuid','password')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver] SQL10007N Message "0" could not be retrieved.  
  **Reason code: "6". SQLCODE=-1013**

Next :: try a User DSN with same configuration as above

>>> conn = ibm_db.connect("DSN=USER_DB2_DSN","mytextuid","password")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver]  
  **SQLSTATE=HY009 SQLCODE=-99999**

Next :: Enabled Trace on the ODBC System DSN (result: ODBC Admin Hangs on connection)

C:\clidriver\db2trace.log>cat p8516t4948.cli


[ Process: 8516, Thread: 4948 ]
[ Date & Time:               02/27/2018 15:28:34.282526 ]
[ Product:                   QDB2/NT64 DB2 v11.1.2021.1452 ]
[ Level Identifier:          0203010F ]
[ CLI Driver Version:        10.01.0000 ]
[ Informational Tokens:      "DB2 v11.1.2021.1452","s1709221349","DYN1709221349W
IN64","Fixpack 2a" ]
[ Install Path:              C:\clidriver ]
[ db2cli.ini Location:       C:\Users\mytextuid\db2cli.ini ]
[ db2cli.ini Location2:      C:\ProgramData\IBM\DB2\C_clidriver\cfg\db2cli.ini ]

[ db2dsdriver.cfg Location:  C:\ProgramData\IBM\DB2\C_clidriver\cfg\db2dsdriver.
cfg ]
[ CLI Driver Type:           IBM Data Server Driver For ODBC and CLI ]



[0000008516 0000004948] [02/27/2018 15:28:34.283197] SQLAllocEnv(phEnv=&0000000000392b00 )
[0000008516 0000004948] [02/27/2018 15:28:34.284702]     ---> Time elapsed - 0 seconds

[0000008516 0000004948] [02/27/2018 15:28:34.299819] SQLAllocEnv( phEnv=0:1 )
[0000008516 0000004948] [02/27/2018 15:28:34.301458]     <--- SQL_SUCCESS   Time elapsed - +1.826100E-002 seconds

[0000008516 0000004948] [02/27/2018 15:28:34.302199] SQLAllocConnect( hEnv=0:1,phDbc=&0000000000392b08 )
[0000008516 0000004948] [02/27/2018 15:28:34.304124]     ---> Time elapsed - +7.410000E-004 seconds

[0000008516 0000004948] [02/27/2018 15:28:34.305055] SQLAllocConnect( phDbc=0:1)
[0000008516 0000004948] [02/27/2018 15:28:34.305902]     <--- SQL_SUCCESS   Time
 elapsed - +3.703000E-003 seconds

[0000008516 0000004948] [02/27/2018 15:28:34.306285] SQLDriverConnect( hDbc=0:1,hwnd=0:0,szConnStrIn="DSN=DSNNAME;UID=mytextuid;PWD=********;AUTOCOMMIT=0;CONNECTTYPE=1;", cbConnStrIn=-3, szConnStrOut=&0000000000103230, cbConnStrOutMax=256,pcbConnStrOut=&00000000001031f6,fDriverCompletion=SQL_DRIVER_NOPROMPT )
[0000008516 0000004948] [02/27/2018 15:28:34.308920]     ---> Time elapsed - +3.830000E-004 seconds
    sqlccconnr( timeout - +0.000000E+000, protocol - 0x03 )

Next :: watching c:\\ProgramData\\IBM\\DB2\\C_clidriver\\db2diag.log

I test the connect for the DSN in ODBC Admin and this is the entry I receive... Note :: the "DB", APPID, and HOSTNAME have been obfuscated in the below... I have no idea why a license is required to use this driver, if that is even the issue

2018-02-27-16.29.43.560000-480 E27622F847           LEVEL: Error
PID     : 13452                TID : 4948           PROC : odbcad32.exe
INSTANCE:                      NODE : 000           DB   : DBNAME
APPID   : my.ipa.ddr.ess.65253.180228002943
HOSTNAME: ${COMPUTERNAME}
EDUID   : 4948
FUNCTION: DB2 UDB, Connection Manager, sqleuPerformServerActivationCheck, probe:110
MESSAGE : ADM12008C  The product "IBM Data Server Driver For ODBC and CLI" does 
          not have a valid license key installed and the evaluation period has 
          expired. Functions specific to this product are not enabled. If you 
          have licensed this product, ensure the license key is properly 
          installed. You can install the license using the db2licm command. The 
          license file can be obtained from your licensed product CD.

Try this(Python3 and Python2):

import ibm_db as db2

def make_connexion(server, port, database, username, password, timeout):
    return db2.connect('DATABASE={0};'.format(database) +
                  'HOSTNAME={0};'.format(server) +
                  'PORT={0};'.format(str(port)) +
                  'PROTOCOL=TCPIP;' +
                  'UID={0};'.format(username) +
                  'PWD={0};'.format(password) +
                  'ConnectTimeout={0};'.format(str(timeout))
                  , '', '')

# sample connexion object
connexion = make_connexion('localhost', '50000', 'database', 'username', 'password', '30')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM