使用pymssql访问Azure SQL Database时遇到DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (xxxx:1433)\n')这样的错误,具体错误如下所示:

 

 

# python test.py

Traceback (most recent call last):

  File "src/pymssql.pyx", line 636, in pymssql.connect

  File "src/_mssql.pyx", line 1957, in _mssql.connect

  File "src/_mssql.pyx", line 676, in _mssql.MSSQLConnection.__init__

  File "src/_mssql.pyx", line 1683, in _mssql.maybe_raise_MSSQLDatabaseException

_mssql.MSSQLDatabaseException: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (xxxx.database.chinacloudapi.cn:1433)\n')

 

During handling of the above exception, another exception occurred:

 

Traceback (most recent call last):

  File "test.py", line 7, in <module>

    charset="utf8")

  File "src/pymssql.pyx", line 642, in pymssql.connect

pymssql.OperationalError: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (xxxx.database.chinacloudapi.cn:1433)\n')

 

 

 

官方文档关于如何使用pymssql连接Azure SQL Database有明确的规定(http://pymssql.org/en/stable/azure.html):

 

·         Use FreeTDS 0.91 or newer

·         Use TDS protocol 7.1 or newer

·         Make sure FreeTDS is built with SSL support

·         Specify the database name you are connecting to in the database parameter of the relevant connect() call

·         IMPORTANT: Do not use username@server.database.windows.net for the user parameter of the relevant connect() call! You must use the shorter username@server form instead!

·          

 

 

具体翻译如下:

 

必须使用FreeTDS 0.91 或后续版本

必须是使用TDS protocol 7.1或后续版本

必须确保FreeTDS编译安装时支持SSL

在相关的connect()调用的数据库参数中指定要连接的数据库名称

重要提示:相关的connect()调用时,参数不要使用username@server.database.windows,而是应该使用username@server替代,否则会遇到错误(请见下文)

 

 

官方文档https://www.freetds.org/userguide/ 有介绍TDS协议版本,有兴趣可以了解一下,简单描述如下所示:

 

Product

TDS Version

Comment

Sybase before System 10, Microsoft SQL Server 6.x

4.2

Still works with all products, subject to its limitations.

Sybase System 10 and above

5.0

Still the most current protocol used by Sybase.

Sybase System SQL Anywhere

5.0 only

Originally Watcom SQL Server, a completely separate codebase. Our best information is that SQL Anywhere first supported TDS in version 5.5.03 using the OpenServer Gateway (OSG), and native TDS 5.0 support arrived with version 6.0.

Microsoft SQL Server 7.0

7.0

Includes support for the extended datatypes in SQL Server 7.0 (such as char/varchar fields of more than 255 characters), and support for Unicode.

Microsoft SQL Server 2000

7.1

Include support for bigint (64 bit integers), variant and collation on all fields. Collation is not widely used.

Microsoft SQL Server 2005

7.2

Includes support for varchar(max), varbinary(max), xml datatypes and MARS[a].

Microsoft SQL Server 2008

7.3

Includes support for time, date, datetime2, datetimeoffset.

Microsoft SQL Server 2012 or 2014

7.4

Includes support for session recovery.

Notes:
a. Multiple Active Result Sets
.

 

 

关于FreeTDS的安装,我们可以从FreeTDS的从官方网址下载(FreeTDS的官方网址:https://www.freetds.org/),目前的稳定版本为Stable Release (1.1.15),下载地址ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.1.15.tar.gz

 

安装过程:

 

#tar -xzvf freetds-1.1.15.tar.gz
#cd freetds-1.1.15
# ./configure --prefix=/usr/local --with-openssl=/usr/include/openssl --with-tdsver=7.4
#make
#make install

相关文章: