简体   繁体   中英

Error connecting to SQL Server from Ubuntu+PYODBC

I found many similar question but none of the solutions worked for me. It seems to be a pretty simple matter but still I can't find a solution.

I'm using a headless ubuntu server to connect to a SQL Server on a windows Server 2008 using a python script that does it using pyodbc. The script runs perfectly on my local windows machines, but when I try it on ubuntu server I get the error:

Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')

The connection string I'm using is:

connectionString='DRIVER={dbserverdsn};SERVER=10.23.11.10;DATABASE=Market;UID=usr;PWD=pwd;TDS_VERSION=7.2;PORT=1433'

Any thoughts ? thanks in advance

EDIT : Adding the main files

odbc.ini

[dbserverdsn]
    Driver = FreeTDS
    Server = 10.23.11.10
    Port = 1433
    Database=Markets
    TDS_Version = 7.2

odbcinst.ini

[FreeTDS]
    Description = v0.91 with protocol v7.2
    Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

freetds.conf

[global]
    # TDS protocol version, use:
    # 7.3 for SQL Server 2008 or greater (tested through 2014)
    # 7.2 for SQL Server 2005
    # 7.1 for SQL Server 2000
    # 7.0 for SQL Server 7
    tds version = 7.2
    port = 1433
    text size = 64512

# A typical Microsoft server
[dbserverdsn]
    host = 10.23.11.10
    port = 1433
    tds version = 7.2

Microsoft comes pre-installed with a driver to speak to SQL Server, Ubuntu does not. FreeTDS provides the best driver I've used (the one provided by MS has given me problems). Here's how... on Ubuntu, install your pre-requisites:

# Install pre-requesite packages
sudo apt-get install unixodbc unixodbc-dev freetds-dev freetds-bin tdsodbc

You need to configure /etc/freetds/freetds.conf to bridge unixODBC to SQL Server next:

[global]
    # TDS protocol version, use:
    # 7.3 for SQL Server 2008 or greater (tested through 2014)
    # 7.2 for SQL Server 2005
    # 7.1 for SQL Server 2000
    # 7.0 for SQL Server 7
    tds version = 7.2
    port = 1433
    text size = 64512

# A typical Microsoft server
[dbserverdsn]
    host = dbserver.domain.com
    port = 1433
    tds version = 7.2

Here's where you plug unixODBC into FreeTDS in /etc/odbcinst.ini and give the path to the FreeTDS driver:

[FreeTDS]
Description = v0.91 with protocol v7.2
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

Last but not least, edit /etc/odbc.ini so Python / unixODBC can see the entire stack and FreeTDS data source:

[dbserverdsn]
Driver = FreeTDS
Server = dbserver.domain.com
Port = 1433
TDS_Version = 7.2

You can test from problems on Ubuntu's command line with the 'tsql' and 'isql' command line utilities and post any errors you receive.

Then, for your connection string:

connectionString='DRIVER={FreeTDS};SERVER=10.23.11.10;PORT=1433;DATABASE=Market;UID=*;PWD=*;TDS_VERSION=7.2'

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM