使用Anaconda的Python 3版本创建到MSSQL服务器的ODBC连接时遇到错误:
pyodbc.Error: (‘HY000’, ‘[HY000] [Microsoft][ODBC Driver 17 for SQL Server]SSPI Provider: No Kerberos credentials available (default cache: KEYRING:persistent:1918003883) (851968) (SQLDriverConnect)’)
服务器已通过SSSD加入Windows Active Directory域和Kerberos域.我可以通过SSH连接到服务器,并使用kinit检索TGT.我甚至可以使用klist查看凭证缓存.但是python进程似乎无法找到Kerberos TGT或Kerberos凭据缓存.
设置:
Python
$/mnt/ds/anaconda3/bin/python --version
Python 3.6.5 :: Anaconda, Inc.
test.py
from pyodbc import connect
connection = connect('DSN=MyDSN')
/etc/odbc.ini
[MyDSN]
#Driver=ODBC Driver 13 for SQL Server
Driver=ODBC Driver 17 for SQL Server
Description=MyMSSQL ODBC Driver
Trace=No
Server=MyMSSQL
Trusted_Connection=Yes
/etc/odbcinst.ini
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.1.so.0.1
UsageCount=1
红帽企业Linux
$cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.5 (Maipo)
$uname -r
3.10.0-862.2.3.el7.x86_64
msodbcsql17
$sudo yum info msodbcsql17
Loaded plugins: amazon-id, rhui-lb, search-disabled-repos
Installed Packages
Name : msodbcsql17
Arch : x86_64
Version : 17.1.0.1
Release : 1
Size : 17 M
Repo : installed
From repo : packages-microsoft-com-prod
Summary : ODBC Driver for Microsoft(R) SQL Server(R)
License : https://aka.ms/odbc170eula
Description : This package provides an ODBC driver that can connect to Microsoft(R) SQL Server(R).
的unixODBC
$sudo yum info unixODBC
Loaded plugins: amazon-id, rhui-lb, search-disabled-repos
Installed Packages
Name : unixODBC
Arch : x86_64
Version : 2.3.1
Release : 11.el7
Size : 1.2 M
Repo : installed
From repo : rhui-REGION-rhel-server-releases
Summary : A complete ODBC driver manager for Linux
URL : http://www.unixODBC.org/
License : GPLv2+ and LGPLv2+
Description : Install unixODBC if you want to access databases through ODBC.
: You will also need the mysql-connector-odbc package if you want to access
: a MySQL database, and/or the postgresql-odbc package for PostgreSQL.
$/mnt/ds/anaconda3/bin/conda list unixodbc
# packages in environment at /mnt/ds/anaconda3:
#
# Name Version Build Channel
unixodbc 2.3.6 h1bed415_0
pyodbc
$/mnt/ds/anaconda3/bin/conda list pyodbc
# packages in environment at /mnt/ds/anaconda3:
#
# Name Version Build Channel
pyodbc 4.0.23 py36hf484d3e_0
以下是我尝试过的一些事情:
>使用由Anaconda2打包的Python-2.7.15.那很有效!
>使用isql.我运行isql MyDSN并连接.
>有两个unixODBC库(一个通过yum安装;另一个通过conda安装).默认情况下,它将使用conda,但我强制它使用系统unixODBC包和LD_PRELOAD.同样的错误.
>我尝试将数据库驱动程序降级为msodbcsql-13.1.9.2-1,然后降级为msodbcsql-13.0.1.0-1.同样的错误.
>我尝试换掉另一个Python ODBC库TurbODBC的PyODBC.同样的错误.
>我使用python-3.5在conda中创建了一个单独的环境.这很有效!仍然不确定为什么.
>我写了一个与unixODBC接口的简单C程序.该程序能够通过Kerberos连接到MSSQL服务器就好了.
>我通过strace运行python2正面测试用例和python3负面测试用例来检查系统调用.我认为这可能会揭示一些东西.看起来他们都开始在文件系统上查找client.keytab文件.然后,在正面测试案例中,它将回退到搜索内核的密钥环,它将成功找到凭证缓存并继续.但是,在否定测试用例中,它只是重试找到client.keytab,并且从不尝试搜索密钥环.
>我启用了unixODBC跟踪选项,一个使用Python3测试用例,另一个使用Python2测试用例.不幸的是,痕迹(如下所示)并没有向我揭示任何内容.
PY3-unixodbc.trace
[ODBC][8741][1527046794.480751][__handles.c][460]
Exit:[SQL_SUCCESS]
Environment = 0x55eea73ed130
[ODBC][8741][1527046794.480806][SQLSetEnvAttr.c][189]
Entry:
Environment = 0x55eea73ed130
Attribute = SQL_ATTR_ODBC_VERSION
Value = 0x3
StrLen = 4
[ODBC][8741][1527046794.480824][SQLSetEnvAttr.c][363]
Exit:[SQL_SUCCESS]
[ODBC][8741][1527046794.480843][SQLAllocHandle.c][375]
Entry:
Handle Type = 2
Input Handle = 0x55eea73ed130
[ODBC][8741][1527046794.480861][SQLAllocHandle.c][493]
Exit:[SQL_SUCCESS]
Output Handle = 0x55eea7400500
[ODBC][8741][1527046794.481176][SQLDriverConnectW.c][290]
Entry:
Connection = 0x55eea7400500
Window Hdl = (nil)
Str In = [DSN=MyDSN][length = 15]
Str Out = (nil)
Str Out Max = 0
Str Out Ptr = (nil)
Completion = 0
UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'
[ODBC][8741][1527046794.575566][__handles.c][460]
Exit:[SQL_SUCCESS]
Environment = 0x55eea746e360
[ODBC][8741][1527046794.575614][SQLGetEnvAttr.c][157]
Entry:
Environment = 0x55eea746e360
Attribute = 65002
Value = 0x7ffd399177f0
Buffer Len = 128
StrLen = 0x7ffd3991778c
[ODBC][8741][1527046794.575632][SQLGetEnvAttr.c][264]
Exit:[SQL_SUCCESS]
[ODBC][8741][1527046794.575651][SQLFreeHandle.c][219]
Entry:
Handle Type = 1
Input Handle = 0x55eea746e360
PY2-unixodbc.trace
[ODBC][8746][1527046842.073439][__handles.c][460]
Exit:[SQL_SUCCESS]
Environment = 0x185e2e0
[ODBC][8746][1527046842.073530][SQLSetEnvAttr.c][189]
Entry:
Environment = 0x185e2e0
Attribute = SQL_ATTR_ODBC_VERSION
Value = 0x3
StrLen = 4
[ODBC][8746][1527046842.073552][SQLSetEnvAttr.c][363]
Exit:[SQL_SUCCESS]
[ODBC][8746][1527046842.073572][SQLAllocHandle.c][375]
Entry:
Handle Type = 2
Input Handle = 0x185e2e0
[ODBC][8746][1527046842.073590][SQLAllocHandle.c][493]
Exit:[SQL_SUCCESS]
Output Handle = 0x1857d40
[ODBC][8746][1527046842.073613][SQLDriverConnectW.c][290]
Entry:
Connection = 0x1857d40
Window Hdl = (nil)
Str In = [DSN=MyDSN][length = 15]
Str Out = (nil)
Str Out Max = 0
Str Out Ptr = (nil)
Completion = 0
UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'
[ODBC][8746][1527046842.208760][__handles.c][460]
Exit:[SQL_SUCCESS]
Environment = 0x1967210
[ODBC][8746][1527046842.208830][SQLGetEnvAttr.c][157]
Entry:
Environment = 0x1967210
Attribute = 65002
Value = 0x7ffe1153fcf0
Buffer Len = 128
StrLen = 0x7ffe1153fc8c
[ODBC][8746][1527046842.208849][SQLGetEnvAttr.c][264]
Exit:[SQL_SUCCESS]
[ODBC][8746][1527046842.208869][SQLFreeHandle.c][219]
Entry:
Handle Type = 1
Input Handle = 0x1967210
我只想说,我的智慧结束了.任何想法将不胜感激!
解决方法:
迟到的回复,但我希望有人会发现它有用.
krb5软件包与系统范围的kerberos安装共存,这对我来说也是同样的问题:从环境中删除它解决了问题(我试图让它工作但没有成功).