Linked Questions

Popular Questions

Pyodbc unable to connect with FreeTDS, all on LAN

Asked by At

Below is my code:

import pyodbc, os, sys
#
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
#
cnxn_str = ("Driver={FreeTDS};"
            "DNS=ganymede;"
            "Port=1433;"
            "Database=players;"
            "Trusted_Connection=yes;"
            "TrustServerCertificate=yes;"
            "TDS_Version=7.1;")
#
db = pyodbc.connect(cnxn_str)

The entry in my /etc/hosts file reads:

192.168.1.231 ganymede

Below is my /etc/odbc.ini file:

[ganymede]
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Description=ganymede
Trace=No
#TraceFile = /tmp/sql.log
Server=tcp:192.168.1.231
#Port = 1433
#instance=SQLEXPRESS
#Database=players
#TDS_Version = 7.4

Below is my /etc/odbcinst.ini file:

[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.2.so.1.1
UsageCount=1

[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.2.1
UsageCount=1

[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/odbc/libtdsS.so
UsageCount=1

And finally, below is my stack trace:

Traceback (most recent call last):
  File "/media/Dock1/Git/.../sql_migration.py", line 17, in <module>
    db = pyodbc.connect(cnxn_str)
pyodbc.OperationalError: ('08001', '[08001] [FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')

I've tried going with just the IP address in the code. I've tried changing DNS= to say Server=. I've tried changing FreeTDS versions. I even turned off UFW to see if firewall settings were the problem. I cannot for the life of me get this thing to connect, and it's all on LAN. Anyone have any ideas?


I was able to resolve the issue. Below are my corrected files.

freetds.conf entry:

[ganymede]
    host = 192.168.1.231
    port = 1433
    tds version = 7.0

odbc.ini:

[ganymede]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = No
Servername = ganymede

odbcinst.ini:

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

connection string:

Driver={FreeTDS};Server=<FQDN>;UID=<UID>;PWD=<password>;Database=players;TDS_Version=7.0;

Related Questions