pymssql和Adaptive Server连接失败

时间:2016-03-25 09:30:37

标签: python azure ubuntu freetds pymssql

当我尝试通过python中的Pymssql连接到Azure数据库时,我遇到了这个错误:

pymssql.OperationalError: (20002, 'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (iprice-bi.database.windows.net:1433)\n')

我通过tsql命令连接到数据库:

 tsql -H server -p 1433 -U username -P password

locale is "en_US.UTF-8"

locale charset是" UTF-8" 使用默认字符集" UTF-8" 1 GT; SELECT @@版本 2 - ; GO

Microsoft SQL Azure (RTM) - 12.0.2000.8 
Mar  1 2016 22:36:40 
Copyright (c) Microsoft Corporation

(1 row affected)

我还检查了freetds.conf,以确保我使用的是正确版本。

[global]
# TDS protocol version
tds version = 7.0
dump file = /tmp/freetds.log
dump file append = yes

日志文件的结果也没有给出任何线索。

log.c:167:Starting log file for FreeTDS 0.95.87
on 2016-03-25 16:50:51 with debug flags 0x4fff.
dblib.c:1237:tdsdbopen: Calling tds_connect_and_login(0x23f9b00, 0x23fde90)
iconv.c:328:tds_iconv_open(0x23f9b00, UTF-8)
iconv.c:187:local name for ISO-8859-1 is ISO-8859-1
iconv.c:187:local name for UTF-8 is UTF-8
iconv.c:187:local name for UCS-2LE is UCS-2LE
iconv.c:187:local name for UCS-2BE is UCS-2BE
iconv.c:346:setting up conversions for client charset "UTF-8"
iconv.c:348:preparing iconv for "UTF-8" <-> "UCS-2LE" conversion
iconv.c:395:preparing iconv for "ISO-8859-1" <-> "UCS-2LE" conversion
iconv.c:400:tds_iconv_open: done
net.c:202:Connecting to 23.100.117.95 port 1433 (TDS version 7.1)
net.c:275:tds_open_socket: connect(2) returned "Operation now in progress"
net.c:314:tds_open_socket() succeeded
packet.c:740:Sending packet
0000 12 01 00 34 00 00 00 00-00 00 15 00 06 01 00 1b |...4.... ........|
0010 00 01 02 00 1c 00 0c 03-00 28 00 04 ff 08 00 01 |........ .(......|
0020 55 00 00 02 4d 53 53 51-4c 53 65 72 76 65 72 00 |U...MSSQ LServer.|
0030 d7 33 00 00            -                        |.3..|

packet.c:639:Received packet
0000 04 01 00 25 00 00 01 00-00 00 15 00 06 01 00 1b |...%.... ........|
0010 00 01 02 00 1c 00 01 03-00 1d 00 00 ff 0c 00 02 |........ ........|
0020 bf 00 00 03 00         -                        |.....|

login.c:1106:detected flag 3
login.c:472:login packet rejected
query.c:3772:tds_disconnect() 
util.c:165:Changed query state from IDLE to DEAD
util.c:322:tdserror(0x24f4290, 0x23f9b00, 20002, 0)
dblib.c:7925:dbperror(0x23f8fb0, 20002, 0)
dblib.c:7993:dbperror: Calling dblib_err_handler with msgno = 20002;      msg->msgtext = "Adaptive Server connection failed (SERVERNAME:1433)"
dblib.c:8015:dbperror: dblib_err_handler for msgno = 20002; msg->msgtext  = "Adaptive Server connection failed (SERVERNAME:1433)" -- returns 2 (INT_CANCEL)
util.c:352:tdserror: client library returned TDS_INT_CANCEL(2)
util.c:375:tdserror: returning TDS_INT_CANCEL(2)
dblib.c:1241:tdsdbopen: tds_connect_and_login failed for "SERVERNAMR:1433"!
dblib.c:1463:dbclose(0x23f8fb0)
dblib.c:243:dblib_del_connection(0x7f066cb036a0, 0x23f9b00)
mem.c:648:tds_free_all_results()
dblib.c:290:dblib_release_tds_ctx(1)
dblib.c:5873:dbfreebuf(0x23f8fb0)
dblib.c:743:dbloginfree(0x24439f0)

有人面对这样的问题还是可以给我一些解决方法的建议?!

3 个答案:

答案 0 :(得分:1)

您可以尝试使用此连接字符串形式而不是显式吗?您的登录被拒绝:

login.c:472:login packet rejected

conn = pymssql.connect(
    server="yourhost.example.com",
    port=1433,
    user="your_user",
    password="your_pw",
    database="your_db")

我假设您的Azure数据库在1433上运行。这是我用于SQL Server的连接字符串,我过去只使用了与Azure的pyodbc。您还可以在配置中将TDS版本设置为7.1而不是7.0。如果那不能解决问题,还有一些问题(在我们解决之前,我会修改我的答案):

  • 您的用户名和密码的字符有多长?仅出于测试目的,如果用户名和密码非常长,请尝试使用10个字符的用户名和密码。
  • 您使用的是SQL Server登录还是Windows身份验证登录?

答案 1 :(得分:0)

根据我的经验,我认为您可以尝试使用适用于MS SQL Server 2008+的7.3修改tds版本。对于Azure SQL数据库,tds版本7.0太低了。

请参阅我对其他SO帖子pymssql: Connection to the database only works sometimes的回答。

答案 2 :(得分:0)

就我而言,这只是一个身份验证问题。我认为Nagios不支持Windows身份验证模式。启用S​​QL Server身份验证模式后,消息消失,并显示一些严重错误。在一些周期之后,这些清除了,计数器填充了合理的数字。