Cannot connect to Db2 from python

倾然丶 夕夏残阳落幕 提交于 2021-02-11 12:14:40

问题


I installed ibm_db in python with pip install ibm_db and created a db2dsdriver.cfg file with the following content:

<configuration>
  <dsncollection>
    <dsn alias="BANK0002" name="BANK0002" host="localhost" port="50000"/>
  </dsncollection>
  <databases>
    <database name="BANK0002" host="BANK0002" port="50000"/>
  </databases>
</configuration> 

Running this command line I can connect with the database and allows me to run a select statement with no issues:

db2cli execsql -user DB2INST1 -passwd xxxxx -dsn bank0002

The problem is that I cannot connect from Python:

conn = ibm_db.connect("UID=DB2INST1;PWD=xxxxxx;DATABASE=bank0002;
       HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;", "", "")

throws:

SQLCODE=-30082n: [IBM][CLI Driver] SQL30082N Security processing failed with reason "17" ("UNSUPPORTED FUNCTION"). SQLSTATE=08001

What is this problem and how to fix it?

python -V
Python 3.8.5


pip show ibm_db
Name: ibm-db
Version: 3.0.2
Summary: Python DBI driver for DB2 (LUW, zOS, i5) and IDS
Home-page: http://pypi.python.org/pypi/ibm_db/
Author: IBM Application Development Team
Author-email: opendev@us.ibm.com
License: Apache License 2.0
Location: c:\tools\miniconda3\envs\env38\lib\site-packages

回答1:


You are using the python build as delivered by miniconda / anaconda, and this may be the determining factor.

A workaround is to use conda to build ibm_db locally and to install the newly built package from local. Other workarounds may be possible.

The instructions for building conda packages and installing them are here and related pages.

For Win10 x64 20H2 with anaconda 2020.11 (4.9.2) the following commands let me use python ibm_db module to connect to Db2, at least from the anaconda prompt.

  • Install Anaconda 2020.11 for Microsoft Windows x64.
  • In a administrator-mode Anaconda3 prompt window run these commands (ensuring each command completes successfully before running the next one):
  • conda install conda-build git anaconda-clean m2-patch
  • conda update conda conda-build git m2-patch
  • cd /d %userprofile%
  • if directory ibm_db exists the rename it or remove it.
  • conda skeleton pypi ibm_db
  • conda-build ibm_db
  • conda install --use-local ibm_db

If you now run conda list, it's output should contain ibm_db module, and the python (3.8.5) will connect to Db2 , at least when used in the anaconda prompt window.

If you continue to get the same SQL1042C symptom, verify that the PATH has no other Db2-products (other than clidriver\bin) on it, and then add two additional directories to the PATH before launching python. These are the fully qualified pathnames to clidriver\bin\icc64 and clidriver\bin\amd64.vc12.crt (or whatever the amd64.vc.XX.crt is present under clidriver\bin). This is not normally needed, there is is a defect in clidriver for some specific configurations that makes it necessary, and IBM should deliver a fix at some future time (IT34327).



来源:https://stackoverflow.com/questions/65688716/cannot-connect-to-db2-from-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!