使用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协议版本,有兴趣可以了解一下,简单描述如下所示:
关于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