What are the steps required to configure pymssql with SSL support on Ubuntu so I can connect to a SQL Server instance that requires an encrypted connection (e.g., Azure)?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Ubuntu 16.04 LTS
(See this answer for Ubuntu 18.04 LTS.)
The following worked for me on a clean install of Xubuntu 16.04 LTS x64:
The first challenge is that the FreeTDS we get from the Ubuntu 16.04 repositories does not support SSL "out of the box", so we need to build our own. Start by installing
python3-pip
(which also installs build-essentials, g++, and a bunch of other stuff we'll need) andlibssl-dev
(the OpenSSL libraries required for building FreeTDS with SSL support)Download the source code for FreeTDS by clicking the "Stable Release" link at freetds.org. Unpack the archive, switch to the directory you just created (e.g., freetds-1.00.104), and then do
Check the build with
and ensure that "TDS version: auto" and "OpenSSL: yes" are listed. Then use
tsql
to test a "raw" FreeTDS connection, e.g.,Now to install pymssql. By default, recent versions ship as a pre-compiled "wheel" file that does not support encrypted connections so we need to install from the pymssql source. Starting with pymssql 2.1.4, the build process relies on Cython, so first do
and then do
When the build is complete, pymssql is installed.
But... it won't work (yet). When we try to do
import pymssql
in Python we getbecause apparently that file is in the "wrong" place. The fix (ref: here) is to create a symlink in the "right" place that points to the actual file
Now pymssql works with SSL connections.
For me, anyway.
Ubuntu 18.04 LTS
The Ubuntu 18.04 repositories will install a version of FreeTDS that supports GnuTLS so it is not absolutely necessary to build FreeTDS from source. However, we still need to build pymssql from source because simply doing the usual
will install a pre-compiled "wheel" that does not support secure connections. Instead, we need to do