首页 > 解决方案 > Anaconda3 的 python 找不到 kerberos 凭证缓存

问题描述

使用 Anaconda 的 Python 3 版本创建与 MSSQL 服务器的 ODBC 连接时遇到错误:

pyodbc.Error: ('HY000', '[HY000] [Microsoft][ODBC Driver 17 for SQL Server]SSPI 提供程序:没有可用的 Kerberos 凭据(默认缓存:KEYRING:persistent:1918003883) (851968) (SQLDriverConnect)')

服务器已通过 SSSD 加入 Windows Active Directory 域和 Kerberos 领域。我可以通过 SSH 连接到服务器,并使用kinit. 我什至可以使用klist. 但是 python 进程似乎无法找到 Kerberos TGT 或 Kerberos 凭据缓存。

设置:

Python

$ /mnt/ds/anaconda3/bin/python --version
Python 3.6.5 :: Anaconda, Inc.

测试.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

以下是我尝试过的一些事情:

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

可以这么说,我已经无计可施了。任何想法将不胜感激!

标签: pythonsql-serverkerberos

解决方案


迟到的回复,但我希望有人会觉得它有用。

对我来说,同样的问题是由krb5与系统范围的 kerberos 安装共存的包引起的:从环境中删除它解决了问题(我试图让它工作但没有成功)。


推荐阅读