简体   繁体   English

如何通过 ibm_db python 包连接到 DB2

[英]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.我正在尝试通过 python 连接本地 DB2 数据库,但在过去几天没有成功。 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我的目标是在我的 Windows 7 笔记本电脑上安装 Python ibm_db 库并连接到我们的 DB2 实例

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))我无法使用 ibm_db.connect() 方法来识别已知良好的系统 DSN 或用户 DSN(我使用它们通过 Sql Server Management Studio (SSMS) 连接到 DB2)

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.我搜索并阅读了数十个 IBM 网站和文档,但没有一个提到这些信息。 --> 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 Windows 7 笔记本电脑

  • DB2 version 7.3 DB2 7.3 版

  • Python 2.7.13 |Anaconda 4.4.0 (64-bit)| Python 2.7.13 |Anaconda 4.4.0(64 位)| (default, May 11 2017, 13:17:26) [MSC v.1500 64 bit (AMD64)] on win32 (默认,2017 年 5 月 11 日,13:17:26)win32 上的 [MSC v.1500 64 位 (AMD64)]

  • Python library versions installed that seem pertinent安装的 Python 库版本似乎相关

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

  • I have the iSeries Navigator installed and working, which included the iSeries ODBC driver.我已安装并运行 iSeries Navigator,其中包括 iSeries ODBC 驱动程序。 It works perfectly to connect to on-prem DB2 instances它可以完美地连接到本地 DB2 实例

  • 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我安装了 DB2 命令行界面,它在注册表和 64 位 ODBC 管理器驱动程序列表中添加了一个名为 IBM DB2 ODBC DRIVER - C_clidriver 的驱动程序

  • 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:这是我从 iSeries ODBC 驱动程序创建的文件 DSN,但它看起来与我用于 IBM CLI 驱动程序的 DSN 完全不同:

    [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 [ODBC] DRIVER=iSeries Access ODBC 驱动程序 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:第一次尝试 :: 与指定的完整 DSN 的基本连接:

>>> 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 Next :: 在注册表中添加名称为 EXACT 驱动程序名称的驱动程序

>>> 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 Next :: 尝试不同格式的 DSN,结合 DATABASE、HOSTNAME 和 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下一个 :: 尝试使用已知良好的系统 DSN,因为我在 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 Next :: 尝试使用与上述相同配置的用户 DSN

>>> 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) Next :: 在 ODBC 系统 DSN 上启用跟踪(结果:ODBC Admin 在连接时挂起)

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 Next :: 看 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我在 ODBC Admin 中测试了 DSN 的连接,这是我收到的条目...注意::“DB”、APPID 和 HOSTNAME 在下面被混淆了...我不知道为什么需要许可证使用这个驱动程序,如果这甚至是问题

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):试试这个(Python3 和 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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