How to install freetds in Linux? - sql-server

I am trying to connect to MSSQL server from Ubuntu. I have installed freetds like suggested here.
However, when I try to configure /etc/odbc.ini and enter a driver path I have no driver at location /usr/local/freetds/lib/libtdsodbc.so.
Can someone help me to install freetds and to configure odbc to use it?
*edit1: I have found libtdsodbc.so in /usr/lib/x86_64-linux-gnu/odbc. Should I use that driver/path?

I've created a Vagrant box which has a full installation example here: https://github.com/FlipperPA/django-python3-vagrant/
...but here are the basic steps.
# Install pre-requesite packages
sudo apt-get install unixodbc unixodbc-dev freetds-dev freetds-bin tdsodbc
Point odbcinst.ini to the driver in /etc/odbcinst.ini:
[FreeTDS]
Description = v0.91 with protocol v7.2
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Create your DSNs in odbc.ini:
[dbserverdsn]
Driver = FreeTDS
Server = dbserver.domain.com
Port = 1433
TDS_Version = 7.2
...and your DSNs in 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
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# A typical Microsoft server
[dbserverdsn]
host = dbserver.domain.com
port = 1433
tds version = 7.2
After completing this, you can test your connection by attempting to connect with tsql (to test the FreeTDS layer) and isql (for the unixODBC through FreeTDS stack).

The version available via apt-get is a really old version. To get a newer version:
sudo apt-get install wget
sudo apt-get install build-essential
sudo apt-get install libc6-dev
# find latest version of FreeTDS ftp://ftp.freetds.org/pub/freetds/stable/
wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.2.tar.gz
tar -xzf freetds-1.2.tar.gz
cd freetds-1.2
./configure --prefix=/usr/local --with-tdsver=7.3
sudo make
sudo make install

in freedts.conf
[Server80]
host = example.com
port = 1433
tds version = 8.0
client charset = UTF-8
in odbc.ini
[MSSQL8]
Driver = FreeTDS
Description = Sybase JDBC Server
Trace = No
Servername = Server80
Database = DBNAME
UID = sa
ClientCharset = UTF-8
in odbcinst.ini
[FreeTDS]
Description=v0.63 with protocol v8.0
Driver=/usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
UsageCount=2

Related

MAC OSX - How to solve "Could not SQLConnect" error

I'm trying to connect to my sql server database but it's not working.
The freetds is working, see the command:
tsql -S myserver.database.windows.net -U myuser -P mypassword
output:
locale is "C/UTF-8/C/C/C/C"
locale charset is "UTF-8"
using default charset "UTF8"
1>
but when I try to run the command isql, it returns me error.
command:
isql -v MYSERVER myuser mypassword
output:
[IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified
[ISQL]ERROR: Could not SQLConnect
My files:
freetds.conf:
# $Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
tds version = 8.0
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# If you experience TLS handshake errors and are using openssl,
# try adjusting the cipher list (don't surround in double or single quotes)
# openssl ciphers = HIGH:!SSLv2:!aNULL:-DH
[MYSERVER]
host = myserver.database.windows.net
port = 1433
tds version = 8.0
client charset = UTF8
odbcinst.ini:
[FreeTDS]
Description = FreeTDS
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
UsageCount = 1
Trace = Yes
TraceFile = /Users/giorgecaique/Documents/sql.log
odbc.ini:
[MYSERVER]
Description = MYSERVER
Driver = FreeTDS
Database = mydatabase
Server = myserver.database.windows.net
UserName = myuser
Password = mypassword
TDS_Version = 8.0
Can anyone help me? I already saw a lot of tutorials of how to solve this, but none of it worked for me.
In odbc.ini file, you need a Servername attribute which points to the name of the DSN in freetds.conf. Also, 8.0 is not a valid version (long story, see the docs), and it is UTF-8 not UTF8. For example:
freetds.conf:
[MYSERVER]
host = myserver.database.windows.net
port = 1433
tds version = 7.1
client charset = UTF-8
And then in odbc.ini:
[MYSERVER]
Description = MYSERVER
Driver = FreeTDS
Database = mydatabase
Servername = MYSERVER
UserName = myuser
Password = mypassword
TDS_Version = 7.1
Good luck!
Have you checked that you have the library /usr/local/lib/libtdsodbc.so?
I ran into a similar error. I fixed my error following this guide at link.
I believe you may not have installed the drivers for freetds, which you can install via Homebrew with:
$ brew install freetds --with-unixodbc
Did you try emehex's answer from here:
Can't open lib 'ODBC Driver 13 for SQL Server'? Sym linking issue?
sudo ln -s /usr/local/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /usr/local/etc/odbc.ini /etc/odbc.ini

Connect to MSSQL database with arm linux board using unixODBC and FreeTDS problems

I am having problems to query a remote MSSQL database from my arm linux board.
I compiled unixODBX and FreeTDS, copied all files to the arm board with the same path as in the Ubuntu machine where they where compiled.
I also compiled everything for the Ubuntu machine.
In the Ubuntu machine I can connect and query the database with tsql and isql.
In the arm board I can only connect and query with tsql, not with isql.
COMPILATION FOR THE ARM BOARD IN THE UBUNTU MACHINE:
unixODBX:
./configure --prefix=/usr/local/unixodbc-arm --host=arm-linux
make
make install
I removed /usr/local/unixodbc-arm/bin/odbc_config otherwise I could´t compile it.
FreeTDS:
./configure --prefix=/usr/local/freetds-arm --with-unixodbc=/usr/local/unixodbc-arm --host=arm-linux --disable-libiconv
make
make all
I used --disable-libiconv because without this option was not working.
Than I copied the files in the "bin", "lib" and "etc" directories to the arm board with the same path as in the Ubuntu machine.
/usr/local/unixodbc-arm/bin
/usr/local/unixodbc-arm/lib
/usr/local/unixodbc-arm/etc
/usr/local/freetds-arm/bin
/usr/local/freetds-arm/lib
/usr/local/freetds-arm/etc
The configuartion files are as follows:
freetds.conf:
root#ok335x:/# cat /usr/local/freetds-arm/etc/freetds.conf
# $Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same on a multi-user system
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
; tds version = 4.2
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 5.0
# A typical Microsoft server
[egServer70]
host = ntmachine.domain.com
port = 1433
tds version = 7.0
# Microsoft SQL Server
[SQLEXPRESS]
host = 192.168.0.195
port = 1433
tds version = 7.0
client charset = UTF-8
root#ok335x:/#
odbcinst.ini:
root#ok335x:/# cat /usr/local/unixodbc-arm/etc/odbcinst.ini
[FreeTDS]
Description=TDS driver (Sybade/MS SQL)
Driver=/usr/local/freetds-arm/lib/libtdsodbc.so
UsageCount=2
root#ok335x:/#
odbc.ini
root#ok335x:/# cat /usr/local/unixodbc-arm/etc/odbc.ini
[SQLEXPRESS]
Description = Test to FreeTDS
Driver = FreeTDS
Trace = Yes
TraceFile = sql.log
Database = gti
Servername = SQLEXPRESS
UserName = gti_monitor
Password = gti_monitor
Port = 1433
ReadOnly = No
RowVersioning = No
ShowSystemTables = No
ShowOidColumn = No
FakeOidIndex = No
ConnSettings =
root#ok335x:/#
tsql configuration:
root#ok335x:/# tsql -C
Compile-time settings (established with the "configure" script)
Version: freetds vdev.0.99.134
freetds.conf directory: /usr/local/freetds-arm/etc
MS db-lib source compatibility: no
Sybase binary compatibility: no
Thread safety: yes
iconv library: no
TDS version: 5.0
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: no
OpenSSL: no
GnuTLS: no
root#ok335x:/#
odbcinst configuration:
root#ok335x:/# odbcinst -j
unixODBC 2.3.4
DRIVERS............: /usr/local/unixodbc-arm/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/unixodbc-arm/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/unixodbc-arm/etc/ODBCDataSources
USER DATA SOURCES..: /home/root/.odbc.ini
SQLULEN Size.......: 4
SQLLEN Size........: 4
SQLSETPOSIROW Size.: 2
root#ok335x:/#
Running tsql:
root#ok335x:/# tsql -S SQLEXPRESS -U gti_monitor -P gti_monitor
locale is "C"
locale charset is "ANSI_X3.4-1968"
using default charset "UTF-8"
1> select * from pressures
2> go
id datetime press1 press2
NULL NULL 1 2
(1 row affected)
1> quit
root#ok335x:/#
Running isql:
root#ok335x:/# isql SQLEXPRESS gti_monitor gti_monitor -v
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[ISQL]ERROR: Could not SQLConnect
root#ok335x:/#
These commands are both working in the Ubuntu machine with the same configuration files.
Can anyone help on that?
Thank you very much.
From the isql man page...
isql DSN [USER [PASSWORD]] [options]
DSN. The Data Source Name, which should be used to make connection to the database. The data source is looked for in the /etc/odbc.ini and $HOME/.odbc.ini files in that order, with the latter overwriting the former.
Tsql with -S is using your freetds.conf file. But it looks like you have the odbc.ini file in /usr/local/unixodbc-arm/etc/odbc.ini

ubuntu tsql working and isql not working

I'm trying to connect MSSQL server to ubuntu 14.04 with FreeTDS and unixODBC.
I can connect using tsql with:
~$ tsql -S MSSQL -U <username> -P <password>
This works correctly.
When trying to use isql i get the following
isql -v MSSQL <username> <password>
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[ISQL]ERROR: Could not SQLConnect
Here are my configs.
odbc.ini
[MSSQL]
Driver = FreeTDS
TDS_Version = 8.0
Server = <server>
Instance = dev
Database = <database>
odbcinst.ini
[FreeTDS]
Description = FreeTDS
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
Trace = Yes
TraceFile = /tmp/freetds.log
ForceTrace = Yes
freetds.conf
# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
# tds version = 4.2
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 5.0
# A typical Microsoft server
[egServer70]
host = ntmachine.domain.com
port = 1433
tds version = 7.0
[MSSQL]
host = <host>
instance = dev
tds version = 8.0
I think the issue is being caused by the driver, I don't believe i should get an error saying the driver isn't executable. here is the osql output:
osql -S MSSQL -U <username> -P <password>
checking shared odbc libraries linked to isql for default directories...
strings: '': No such file
trying /tmp/sql ... no
trying /tmp/sql ... no
trying /etc ... OK
checking odbc.ini files
reading /home/<user>/.odbc.ini
[MSSQL] found in /home/<user>/.odbc.ini
found this section:
[MSSQL]
Driver = FreeTDS
TDS_Version = 8.0
Server = <server>
Database = <database>
looking for driver for DSN [MSSQL] in /home/<user>/.odbc.ini
found driver line: " Driver = FreeTDS"
driver "FreeTDS" found for [MSSQL] in .odbc.ini
found driver named "FreeTDS"
"FreeTDS" is not an executable file
looking for entry named [FreeTDS] in /etc/odbcinst.ini
found driver line: " Driver = /usr/local/lib/libtdsodbc.so"
found driver /usr/local/lib/libtdsodbc.so for [FreeTDS] in odbcinst.ini
/usr/local/lib/libtdsodbc.so is an executable file
"Server" found, not using freetds.conf
Server is <server_name>
Configuration looks OK. Connection details:
DSN: MSSQL
odbc.ini: /home/<user>/.odbc.ini
Driver: /usr/local/lib/libtdsodbc.so
Server hostname: <host>
Address: <ip_address>
Attempting connection as <username> ...
+ isql MSSQL <username> <password> -v
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[ISQL]ERROR: Could not SQLConnect
Any help is appreciated, Thanks.
I found the answer to my question. I needed to add the instance to the server in the odbc.ini file like so
MSSQL]
Driver = FreeTDS
TDS_Version = 8.0
Server = <server\dev>
Database = <database>
This let me execute isql.

Linux python3 - Can't open lib 'SQL Server'

I am trying to connect to an Microsoft Azure SQL server database.
This is how i am trying to connect:
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=%s' % (self.config.get("Sql", "DataSource")),
user= self.config.get("Sql", "UserId"),
password=self.config.get("Sql", "Password"),
database=self.config.get("Sql", "Catalog"))
I am getting an error while excuting this line. The error:
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")
Can't figure why this is happening, Any idea?
replace DRIVER={SQL Server} with DRIVER={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1}
Update - December 2022
The current installation instructions for the ODBC driver are here
I also recommend you install the ODBC Driver and then try to use pyodbc. I am assuming you are on an Ubuntu 15.04+ machine.
To install the ODBC Driver follow the following instructions:
sudo su
wget https://gallery.technet.microsoft.com/ODBC-Driver-13-for-Ubuntu-b87369f0/file/154097/2/installodbc.sh
sh installodbc.sh
Once you do that, install pyodbc using pip and try the following script:
import pyodbc
server = 'tcp:myserver.database.windows.net'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT ##version;")
row = cursor.fetchone()
while row:
print row
row = cursor.fetchone()
Let me know how that goes.
Cheers,
Meet
Download Dependencies depends on your platform,
(for other OS Download your Dependencies)
This example for Ubuntu:
# sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version
#Ubuntu 14.04
# curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
#Ubuntu 16.04
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
# #Ubuntu 18.04
# curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
# #Ubuntu 18.10
# curl https://packages.microsoft.com/config/ubuntu/18.10/prod.list > /etc/apt/sources.list.d/mssql-release.list
# #Ubuntu 19.04
# curl https://packages.microsoft.com/config/ubuntu/19.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install unixodbc-dev
and then change,
DRIVER={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1}
DRIVER={ODBC Driver 17 for SQL Server}
Check those links. It solved my problems which were kind of similar.
Installing FreeTDS
Connecting to SQL-Azure using FreeTDS
If you are using an offline REHL server, then follow the below method to setup connection to Microsoft SQL Server.
Download UNIXODBC & MSSQLTools packages—e.g., unixODBC-2.3.7-1.rh.x86_64.rpm/mssql-tools-17.9.1.1-1.x86_64.rpm—from https://packages.microsoft.com/rhel/, as per your REHL version.
Place downloaded files on the REHL server via winscp or any ssh client.
Install these two files in sequence given below:
yum localinstall unixODBC-2.3.7-1.rh.x86_64.rpm
yum localinstall mssql-tools-17.9.1.1-1.x86_64.rpm)
Go to the installation folder, and copy the path as shown in e.g.,
/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1
Put this path in code:
driverpath = r"/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1"
Your problem will get solved.
I see that this post has an accepted answer but it did not work for me running Python 3.8 on Ubuntu 20.04. I tried several things but I think that ultimately, it was a security issue having to do with the openSSL package. Here are the steps I recommend.
Check the openSSL version. Do this in the Linux console
openssl version
The problematic version was 1.1.1f on my system. If that is the version you have, update it with these commands
wget https://www.openssl.org/source/openssl-1.1.1p.tar.gz -O openssl-1.1.1p.tar.gz
tar -zxvf openssl-1.1.1p.tar.gz
cd openssl-1.1.1p
./config
make
sudo make install
sudo ldconfig
openssl version
Then you have to edit /etc/ssl/openssl.cnf . Put this in the beginning
openssl_conf = default_conf
And this at the end
[ default_conf ]
ssl_conf = ssl_sect
[ssl_sect]
system_default = system_default_sect
[system_default_sect]
MinProtocol = TLSv1.2
CipherString = DEFAULT:#SECLEVEL=0
Like I said, that worked for me. Hopefully it can help someone else too.
Hi I am using this snippet, which searches and return lastest available version of ODBC Driver, or raises an error:
def select_driver():
"""Find least version of: ODBC Driver for SQL Server."""
drv = sorted([drv for drv in pyodbc.drivers() if "ODBC Driver " in drv and " for SQL Server" in drv])
if len(drv) == 0:
raise Exception("No 'ODBC Driver XX for SQL Server' found.")
return drv[-1]
print(select_driver()) # ODBC Driver 17 for SQL Server

FreeTDS can't read Servername from odbc.ini

I am trying to setup FreeTDS to connect into a mssql server in Ubuntu 14.04 and FreeTDS 4.2
However osql can't read Servername setting due unknow reasons.
If I use isql, it works. However my programs use odbc.ini settings.
What I am doing wrong?
My odbc.ini;
[ODBC Data Sources]
Test = My Test Server
[mssql]
Description = My Test Server
Driver = FreeTDS
Trace = No
TDS_Version = 4.2
Servername = mssql
Command
~$ osql -S mssql -U user -P password
checking shared odbc libraries linked to isql for default directories...
strings: '': No hay tal fichero
trying /tmp/sql ... no
trying /tmp/sql ... no
trying /etc ... OK
checking odbc.ini files
reading /home/business/.odbc.ini
[mssql] not found in /home/business/.odbc.ini
reading /etc/odbc.ini
[mssql] found in /etc/odbc.ini
found this section:
[mssql]
Description = My Test Server
Driver = FreeTDS
Trace = No
TDS_Version = 4.2
Servername = mssql
looking for driver for DSN [mssql] in /etc/odbc.ini
found driver line: " Driver = FreeTDS"
driver "FreeTDS" found for [mssql] in odbc.ini
found driver named "FreeTDS"
"FreeTDS" is not an executable file
looking for entry named [FreeTDS] in /etc/odbcinst.ini
found driver line: " Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so"
found driver /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so for [FreeTDS] in odbcinst.ini
/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so is an executable file
Using ODBC-Combined strategy
DSN [mssql] has servername "" (from /etc/odbc.ini)
A few things you'll want to fix up here. First, in odbc.ini, is it "Server" and not "Servername". Here's an example.
odbc.ini:
[myserver]
Driver = FreeTDS
Server = myserver.mydomain.com
Port = 1433
TDS_Version = 7.2
freetds.conf:
[myserver]
host = myserver.mydomain.com
port = 1433
tds version = 7.2
Also, you're going to want to use a higher TDS Version. TDS 4.2 was for Microsoft SQL Server 6.0! Despite the documentation, I have found it does not work with all products. To select an appropriate TDS version, see here:
http://www.freetds.org/userguide/choosingtdsprotocol.htm
You most likely want version 7.2 or 7.3. Good luck!

Resources