使用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

验证过程:

# tsql -C

Compile-time settings (established with the "configure" script)

                            Version: freetds v1.1.15

             freetds.conf directory: /usr/local/etc

     MS db-lib source compatibility: no

        Sybase binary compatibility: no

                      Thread safety: yes

                      iconv library: yes

                        TDS version: 7.4

                              iODBC: no

                           unixodbc: no

              SSPI "trusted" logins: no

                           Kerberos: no

                            OpenSSL: yes

                             GnuTLS: no

                               MARS: yes

# pip show pymssql

Name: pymssql

Version: 2.1.4

Summary: DB-API interface to Microsoft SQL Server for Python. (new Cython-based version)

Home-page: http://pymssql.org

Author: Damien Churchill

Author-email: damoxc@gmail.com

License: LGPL

Location: /usr/local/lib/python3.6/site-packages

Requires: 

Required-by: 

然后我测试验证pymssql是否可以访问Azure SQL Database,但是还是继续报这个错误,相当纳闷,然后我用tsql验证了一下,确认FreeTDS是可以访问Azure SQL Database的。如下所示:

#tsql -H eworker.database.chinacloudapi.cn -U EWorkerAdmin -D EWorker -p 1433 -P 5#wJ9p@#T46ncy3Zf

locale is "en_US.UTF-8"

locale charset is "UTF-8"

using default charset "UTF-8"

Setting EWorker as default database in login packet

1> select @@version

2> go

 

Microsoft SQL Azure (RTM) - 12.0.2000.8 

        Jul  3 2019 10:02:53 

        Copyright (C) 2019 Microsoft Corporation

 

(1 row affected)

1> 

瞬时懵逼了,静下心来仔细看了官方文档(这里就要说到通读文档的重要性了,很多时候,我们去看官方文档,都是挑一下重要的看或是敷衍了事的看看),才发现Linux下的pymssql Wheel软件包捆绑了FreeTDS不支持SSL,而且它是静态的dll,即使我安装了支持SSL的FreeTDS,但是pymssql依然不会用到后续安装的FreeTDS的dll,因此连接不到Azure SQL Database:

The statically-linked FreeTDS version bundled with our official pymssql Linux Wheel package doesn’t have SSL support so it can’t be used to connect to Azure. Also it doesn’t have Kerberos support so it can’t be used to perform domain logins to SQL Server

另外github上面 https://github.com/pymssql/pymssql/issues/399 也有相关问题的较详细的介绍:

From Ramiro on the ML regarding this issue:

For 2.1.2, I made the decision to stop shipping FreeTDS linked in staticall in pymssql and to depend on external FreeTDS build as DLLs instead.

In our docs (http://pymssql.org/en/v2.1.2/freetds.html#windows) we link to binaries of FreeTDS DLLS I maintain (https://github.com/ramiro/freetds/releases).

But there is more: These FreeDTS DLLs also depend on other DLLs: The OpenSSL ones chosen are these maintained at: http://www.npcglib.org/~stathis/blog/precompiled-openssl/.

(I decided to go with these because it's a binary distribution of OpenSSL which includes binaries built with the full gamut of Visual C versions used to build the official Windwos binaries of our supported Python versions [VC of VS 2008 for Python 2.7, VC of VS2010 for Python 3.3 and 3.4 and VC of VS2015 for Python 3.5] and thus won't risk causing strange errors at runtime because of mixed C runtime DLLs. This is also true for the FreeTDS DLLs above).

So, the situacion on Windows has changed like this:

pymlssql 2.1.1                                           pymssql 2.1.2

+

+------------------------------------------+             +-------------------------------------------+

|

|                                          |             |                                           |

|

| pymssql                                  |             | pymssql                                   |

|

|                                          |             |                                           |

|

|                                          |             |                                           |

|

|                                          |  +------->  |                                           |

|

|                                          |             +-------------------------------------------+

|

|  +-----------------------------------+   |

|

|  | freetds (statically linked)       |   |             +-------------------------------------------+

|

|  |                                   |   |             |                                           |

|

|  |                                   |   |             | freetds (in DLL form)                     |

|

|  +-----------------------------------+   |             |                                           |

+

+------------------------------------------+             +-------------------------------------------+

 

 

 

                                                         +-------------------------------------------+

 

                                                         |                                           |

 

                                                         | OpenSSL (in DLL form)                     |

 

                                                         |                                           |

 

                                                         +-------------------------------------------+

 

 

Unfortunately, as the Python package tooling and file format don't allow to ship third party binaries, these DLLs can't be bundled with the pymssql Wheel binary file. And it woldn't be a good idea anyway (being the package responsible of putting extraneous DLL files on the global site-packages dir of the users' Python installation woldn't be something t be proud of.)

Why this? Because SSL is needed to open secure connections to SQL Server. Particularly it's required when one is trying to use pymssql to connect to Azure 'cloud' SQL Server flavor. And I didn't want to include OpenSSL linked statically with the pymssql binaries because we have a somewhat slow release cycle and in case security adivsores/udpates are published for OpenSSL I didn't want to either a) expose users to keep running older, insecure binaries or b) Have to rush to a new release of pymssql simply because a new version of OpenSSL was released to fix security problems.

I'm still not sure this was a good deicision, and we might revisit it in future releases.

An alternative (which has been kown and used by many in prior releases) is to use pymssql binaries maintained (among many other) by Christoph Gohlke at http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql

I hope this helps clifying the situation and the reasoning I followed when making these technical decisiones.

 

 

pymssql IS a wrapper for FreeTDS: It can't use other drivers/stacks/libraries.

If you are using an official pymsql 2.1.3 wheel downloaded from pypi then you copy of pymssql includes a statically linked cpy of FreeTDS. But Such copy doesn't include the feature to use TLS-protected connection which are a mandatory requirement to connect to Azure.

To be able to do so you will need to build/use a copy of FreeTDS linked against OpenSSL or similar. and then build pymssql against that version of FreeTDS.

This means your setup should heve never worked.

Pleae post support quetions like this to our mailing list, not here. I'm closing this issue.

所以我们卸载pymssql,然后用pip3从源码安装pymmsql,必须先安装Cython,因为pymssql依赖Cython,另外,必须创建libsybdb.so.5的软连接,否则会报错(如下所示)

[root@db-server python]# pip uninstall pymssql

[root@db-server python]# pip3 install --user Cython

[root@db-server python]# pip3 install --user --no-binary pymssql pymssql

[root@db-server python]# python test.py

Traceback (most recent call last):

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

import pymssql

ImportError: libsybdb.so.5: cannot open shared object file: No such file or directory

[root@db-server ~]# ls /usr/local/lib/libsybdb.so.5

/usr/local/lib/libsybdb.so.5

[root@db-server ~]# ln -s /usr/local/lib/libsybdb.so.5 /usr/lib/libsybdb.so.5

[root@db-server ~]# ldconfig

然后测试验证确认可以连接Azure SQL Database了。问题终于解决。

参考资料:

https://www.freetds.org/userguide/

http://pymssql.org/en/stable/azure.html

https://github.com/pymssql/pymssql/issues/399

https://stackoverflow.com/questions/39395548/how-to-configure-pymssql-with-ssl-support-on-ubuntu/52749308#52749308

pymssql连接Azure SQL Database的更多相关文章

  1. Java连接Azure SQL Database

    Azure SQL Database是Azure上的数据库PAAS服务,让用户可以快速的创建和使用SQL数据库而不用担心底层的备份,安全,运维,恢复等繁琐的工作,本文简单介绍如何使用Java程序连接到 ...

  2. 用SSMS连接Azure Sql Database 与连接本地库的一些操作区别

    背景 我们知道Azure Sql Database 可以降低运维成本.是一种Pass模式,有资源弹性设置,可以自由调整资源自动对应不同业务高峰(当然也可以降低费用成本),也方便项目后期的资源扩展,以及 ...

  3. 【数据库-Azure SQL Database】JDBC 如何连接 SQL Azure 数据库

    使用 JAVA 代码连接 Azure SQL Database 时产生了 SSL 错误,对于此问题大多数用户都是因为不知如何编写 JDBC 连接字符串而产生的,以下为相关示例代码,供您参考:   pa ...

  4. Azure SQL Database (24) 使用新管理界面,创建跨数据中心标准地域复制(Standard Geo-Replication)

    <Windows Azure Platform 系列文章目录> 文本是对:SQL Azure (17) SQL Azure V12 - 跨数据中心标准地域复制(Standard Geo-R ...

  5. 使用SQL Database Migration Wizard把SQL Server 2008迁移到Windows Azure SQL Database

    本篇体验使用SQL Database Migration Wizard(SQLAzureMW)将SQL Server 2008数据库迁移到 Azure SQL Database.当然,SQLAzure ...

  6. Azure Sql Database为某个数据库创建单独的访问账户

    由于SQL Management Studio对Azure SQL Database支持不完美,不能使用图形界面,因此配置数据库就会有不同的麻烦,下面是本人配置访问账户的一些经验: 1.以管理员登陆之 ...

  7. [转]Azure 表存储和 Windows Azure SQL Database - 比较与对照

    本文转自:https://msdn.microsoft.com/library/azure/jj553018 更新时间: 2014年10月 作者:Valery Mizonov 和 Seth Manhe ...

  8. Azure SQL Database (19) Stretch Database 概览

    <Windows Azure Platform 系列文章目录>  Azure SQL Database (19) Stretch Database 概览      Azure SQL Da ...

  9. Azure SQL Database (20) 使用SQL Server 2016 Upgrade Advisor

    <Windows Azure Platform 系列文章目录>  Azure SQL Database (19) Stretch Database 概览      Azure SQL Da ...

随机推荐

  1. MySQL InnoDB如何保证事务特性

    如果有人问你"数据库事务有哪些特性"?你可能会很快回答出原子性.一致性.隔离性.持久性即ACID特性.那么你知道InnoDB如何保证这些事务特性的吗?如果知道的话这篇文章就可以直接 ...

  2. MapReduce案例-好友推荐

    用过各种社交平台(如QQ.微博.朋友网等等)的小伙伴应该都知道有一个叫 "可能认识" 或者 "好友推荐" 的功能(如下图).它的算法主要是根据你们之间的共同好友 ...

  3. Kafka 学习笔记之 Topic日志清理

    Topic日志清理 server.properties: log.cleanup.policy=delete (默认) 1. 按时间维度进行Kafka日志清理 log.retention.hours= ...

  4. Python 必备面试基础知识-3

    今天继续分享 Python 相关的面试题,你准备好了嘛! 网络编程篇 1. 简述 OSI 七层协议 是网络传输协议,人为的把网络传输的不同阶段划分成不同的层次. 七层划分为:应用层.表示层.会话层.传 ...

  5. Spring系列(六):Spring事务源码解析

    一.事务概述 1.1 什么是事务 事务是一组原子性的SQL查询,或者说是一个独立的工作单元.要么全部执行,要么全部不执行. 1.2 事务的特性(ACID) ①原子性(atomicity) 一个事务必须 ...

  6. 【JavaScript】使用纯JS实现多张图片的懒加载(附源码)

    一.效果图如下 上面的效果图,效果需求如下 1.还没加载图片的时候,默认显示加载图片背景图 2.刚开始进入页面,自动加载第一屏幕的图片 3.下拉界面,当一张图片容器完全显露出屏幕,即刻加载图片,替换背 ...

  7. C# 8 - Range 和 Index(范围和索引)

    C# 7 的 Span C# 7 里面出现了Span这个数据类型,它可以表示另一个数据结构里连续相邻的一串数据,并且它是内存安全的. 例子: 这个图的输出是3,4,5,6. C# 8 的Range类型 ...

  8. macOS10.14.2 gem 更新问题

    macOS10.14.2,最近cocoapods不能正常使用了. 终端输入 sudo gem update –system 显示如下错误 ERROR: While executing gem … (G ...

  9. Vue躬行记(3)——样式和表单

    Vue对DOM元素的class和style两个特性做了专门的增强,即对CSS类和内联样式做了一层封装,通过v-bind指令来处理它们,而接收的表达式既可以是简单的字符串.对象或数组,也可以是复杂的计算 ...

  10. 教你用Vue写一个开心消消乐

    之前做过一个算法题,算法要求就是写一个开心消消乐的逻辑算法,当时也是考虑了一段时间才做出来.后来想了想,既然核心算法都有了,能不能实现一个开心消消乐的小游戏呢,于是花了两天时间做了一个小游戏出来. 效 ...