Why can I connect to Azure MS SQL with tsql but no

2019-05-21 02:32发布

问题:

Where I am today:

TDSVER=7.3 tsql -H example.database.windows.net -U me -D ExampleDB -p 1433 -P notreallymypassword

This does not:

>>> import pymssql
>>> pymssql.connect('example.database.windows.net', user='me', password='notreallymypassword', database='ExampleDB', tds_version='7.3')

It fails with

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pymssql.pyx", line 635, in pymssql.connect (pymssql.c:10734)
  File "_mssql.pyx", line 1902, in _mssql.connect (_mssql.c:21821)
  File "_mssql.pyx", line 577, in _mssql.MSSQLConnection.__init__ (_mssql.c:6214)
  File "_mssql.pyx", line 1704, in _mssql._tds_ver_str_to_constant (_mssql.c:18845)
_mssql.MSSQLException: unrecognized tds version: 7.3

Okay. Well, that's... strange. So I went back and tried the tsql using TDSVER=7.2, which seemed to work fine.

Trying to connect with tds_version='7.2' gives me:

Traceback (most recent call last):
  File "pymssql.pyx", line 635, in pymssql.connect (pymssql.c:10734)
  File "_mssql.pyx", line 1902, in _mssql.connect (_mssql.c:21821)
  File "_mssql.pyx", line 637, in _mssql.MSSQLConnection.__init__ (_mssql.c:6581)
  File "_mssql.pyx", line 1630, in _mssql.maybe_raise_MSSQLDatabaseException (_mssql.c:17524)
_mssql.MSSQLDatabaseException: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (datawhse.database.
windows.net:1433)\n')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pymssql.pyx", line 641, in pymssql.connect (pymssql.c:10824)
pymssql.OperationalError: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (datawhse.database.windo
ws.net:1433)\n')

So, what gives?

Update 1: pyodbc also fails to connect:

conn = pyodbc.connect('SERVER=example.database.windows.net;Driver=FreeTDS;UID=me@example.database.windows.net;PWD=notmyrealpassword;'
, ansi=True)

My ~/.odbcinst.ini:

[FreeTDS]
Description     = MS SQL driver
Driver          = /usr/lib64/libtdsodbc.so.0
Driver64        = /usr/lib64/libtdsodbc.so.0
Setup           = /usr/lib64/libtdsS.so.0
Setup64         = /usr/lib64/libtdsS.so.0
UsageCount      = 1
CPTimeout       =
CPReuse         =
Trace           = Yes

And this output:

⚘ odbcinst -j
unixODBC 2.3.1
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /home/me/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

回答1:

It looks like Gord was right: The problem was that pymssql wheel does not have SSL bindings.

I uninstalled it:

python -m pip uninstall pymssql

Then installed it from source:

python -m pip install --no-binary pymssql pymssql

This required me to install a few dependencies. But now I can connect with

pymssql.connect('example.database.windows.net',
                user='me', 
                password='notreallymypassword', 
                database='ExampleDB',
                tds_version='7.2')


回答2:

Your connection string doesn't look right. It should be something like:

pymssql.connect(server='example.database.windows.net', user='me@example', password='notreallymypassword', database='ExampleDB')

Note that in your example call to connect(), you are missing a server= parameter; you only had the full server name.