tsql 实用程序 (FreeTDS) 可以连接到 SQL 服务器,但 pymssql 不能

tsql utility (FreeTDS) can connect to SQL Server but pymssql cannot

我遇到了一个非常奇怪的问题:我可以用 tsql 连接到我的 SQL 服务器,但我不能用 pymssql 做同样的事情。让我告诉你更多细节。

使用这种 tsql 命令似乎一切正常:

$ tsql -H '10.10.10.2' -U 'DOMAIN\myuser' -p 63849
Password: 
locale is "fr_FR.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>

但是使用以下基本 python 脚本我遇到了问题:

$ cat test_mssql.py
# -*- coding: utf8 -*-
import pymssql
import pandas as pd

conn = pymssql.connect(server=r'10.10.10.2', user=r'DOMAIN\myuser', password='mypassword', port='63849')

$ python test_mssql.py
Traceback (most recent call last):
File "api-vinci-rh/current/test_mssql.py", line 60, in <module>
conn = pymssql.connect(server=r'10.10.10.2', user=r'DOMAIN\myuser', password='mypassword', port='63849')
File "pymssql.pyx", line 641, in pymssql.connect (pymssql.c:10788)
pymssql.OperationalError: (20002, 'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')

通过激活 TDSDUMPCONFIG 环境变量,我可以看到 tls 命令和我的 python 脚本之间的区别:

TDSDUMPCONFIG tls trace
config.c:224:Final connection parameters:
config.c:225:            server_name = 10.10.10.2
config.c:226:       server_host_name = 10.10.10.2
config.c:227:                ip_addr = 10.10.10.2
config.c:228:          instance_name = 
config.c:229:                   port = 63849
config.c:230:          major_version = 7
config.c:231:          minor_version = 1
config.c:232:             block_size = 0
config.c:233:               language = us_english
config.c:234:         server_charset = 
config.c:235:        connect_timeout = 0
config.c:236:       client_host_name = myhost
config.c:237:         client_charset = UTF-8
config.c:238:               app_name = TSQL
config.c:239:              user_name = DOMAIN\myuser
config.c:242:                library = TDS-Library
config.c:243:              bulk_copy = 0
config.c:244:      suppress_language = 0
config.c:245:          encrypt level = 0
config.c:246:          query_timeout = 0
config.c:249:               database = 
config.c:250:              dump_file = /tmp/freetds.log
config.c:251:            debug_flags = 0


TDSDUMPCONFIG pymssql trace
config.c:224:Final connection parameters:
config.c:225:            server_name = 10.10.10.2:63849
config.c:226:       server_host_name = 10.10.10.2
config.c:227:                ip_addr = 10.10.10.2
config.c:228:          instance_name = 
config.c:229:                   port = 63849
config.c:230:          major_version = 7
config.c:231:          minor_version = 1
config.c:232:             block_size = 0
config.c:233:               language = us_english
config.c:234:         server_charset = 
config.c:235:        connect_timeout = 0
config.c:236:       client_host_name = myhost
config.c:237:         client_charset = UTF-8
config.c:238:               app_name = pymssql=2.1.2
config.c:239:              user_name = 
config.c:242:                library = DB-Library
config.c:243:              bulk_copy = 0
config.c:244:      suppress_language = 0
config.c:245:          encrypt level = 0
config.c:246:          query_timeout = 0
config.c:249:               database = 
config.c:250:              dump_file = /tmp/freetds.log
config.c:251:            debug_flags = 0
config.c:252:              text_size = 64512
config.c:253:           broken_dates = 0
config.c:254:     emul_little_endian = 0
config.c:255:      server_realm_name = 

我认为 2 个主要区别在于 user_name 与 python 脚本是空的,libraryDB-Library 而不是 TDS-Library.

我目前找不到在 python 脚本中强制使用这些参数的方法,而且我找不到关于我的问题的任何线索(错误消息是通用的)。

我正在使用 python 2.7.9 和 pymssql==2.1.2 关于我的环境的更多信息:

$ tsql -C
Compile-time settings (established with the "configure" script)
                            Version: freetds v0.91
             freetds.conf directory: /etc/freetds
     MS db-lib source compatibility: no
        Sybase binary compatibility: yes
                      Thread safety: yes
                          iconv library: yes
                        TDS version: 4.2
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: yes

$ grep -v '^#' /etc/freetds/freetds.conf 
[global]
    tds version = 7.1
    dump file = /tmp/freetds.log
    text size = 64512
    enable gssapi delegation = off

如果您有任何线索可以帮助我,请告诉我。

编辑:我有另一个 SQL 服务器实例用于我的测试,我的 python 脚本工作...... username 在TDSDUMPCONFIG 已设置。所以我猜我的环境有些奇怪,但不知道是什么

我找到了一条线索,对此我感到非常惊讶...如果我使用少于 32 个字符的 username,我对 pymssql 就没有问题了!我没有发现任何有关这个奇怪错误 (?) 的信息,但我保证它可以解决我的问题。

我需要查看 pymssql 的代码,看看我是否发现任何长度限制或关于 username

的长度的问题