Ubuntu – Can’t connect to MS SQL with pyodbc

microsoftpythonsql

I just spent hours trying to acomplish a simple task. I had python code that I used to run in windows, and it access a MS SQL db on a windows server 2008. I set up a ubuntu server and want to run this script in this server. after having installed pyodbc, freetds and a bunch of other packages I still can't connect to the DB. The windows connection string I was using was:

connectionString='DRIVER={SQL Server};SERVER=IP;DATABASE=Market;UID=usr;PWD=psw

after researching a lot I ended up with three connection files odbc.ini odbcinst.ini and freetds.conf and they look, respectively like this:

odbc.ini:

[sqlserverdatasource] Driver = FreeTDS Description = ODBC connection
via FreeTDS Trace = No Servername = sqlserver Database = Market;

odbcinst.ini

[FreeTDS] Description = TDS driver (Sybase/MS SQL) Driver =
/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so Setup =
/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so CPTimeout = CPReuse =
FileUsage = 1

freetds.conf

[sqlserver]
host = 10.23.11.10;
port = 1433;
tds version = 5.0

Now i've been trying to test the connection with the following

tsql -S sqlserver

but i get the following error

Error 20017 (severity 9):
Unexpected EOF from the server
OS error 115, "Operation now in progress" Error 20002 (severity 9):
Adaptive Server connection failed

I thought it would take a couple of minutes to make it work but I've wasted many hours. any help is deeply appreciated. thank you.

Best Answer

If you are trying to connect to SQL Server from your linux machine I highly recommend you follow this link https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-python-simple-ubuntu-linux/

This uses another python driver pymssql which uses FreeTDS to connect to SQL Server. Let me know if this helps.

Best,
Meet