Showing posts with label ODBC. Show all posts
Showing posts with label ODBC. Show all posts

Wednesday, June 16, 2010

How to connect to MSSQL from Linux using ODBC

Following steps has been tested in CentOS 5 and Redhat Enterprise Server

To connect to MSSQL 2005 server from linux machine,unixODBC and freeTDS packages has to be installed.

[$] yum list unixODBC*
Loaded plugins: downloadonly, rhnplugin, security
Excluding Packages in global exclude list
Finished
Available Packages
unixODBC.i386 2.2.11-7.1
unixODBC-devel.i386 2.2.11-7.1
unixODBC-kde.i386 2.2.11-7.1

Install following packages

[$] yum install unixODBC.i386
[$] yum install unixODBC-devel.i386

Once ODBC has been installed,install FreeTDS

[$] yum list freetds*
Loaded plugins: downloadonly, rhnplugin, security
Excluding Packages in global exclude list
Finished
Available Packages
freetds.i386 0.64-1.1.rs
freetds-unixodbc.i386 0.64-1.1.rs
freetds-devel.i386 0.64-1.1.rs
freetds-doc.i386 0.64-1.1.rs
[$] yum install freetds.i386
[$] yum install reetds-unixodbc.i386

Once you have completed installation

Edit /etc/odbc.ini file by adding a DNS configuration as shown below(Create a new file if it doesn't exists).

[MSSQLDB]
Driver = FreeTDS
Description = MS SQL DNS
Trace = Yes
TraceFile = /var/log/mstest.log
Servername = MSSQLDB_SERVER
Database =
Port = 1433

Open /etc/odbcinst.ini file and confirm that driver configuration for FreeTDS exists otherwise add following entry

[FreeTDS]
Description = FreeTDS unixODBC Driver
Driver = /usr/lib/libtdsodbc.so.0
Setup = /usr/lib/libtdsodbc.so.0
UsageCount = 2

Please not that the name FreeTDS has been refered in /etc/odbc.ini file as Driver

Now we have to configure Free TDS with host and port number of the database.To do that edit /etc/freetds.conf

[MSSQLDB_SERVER]
host =
port = 1433
tds version = 8.0

Please not that the name MSSQLDB_SERVER has been refered in /etc/odbc.ini file as Servername.

Now we are done with configuration.To check the connection try

[$] isql MSSQLDB  

above command should give following result if you could connect to MSSQL server successfully

+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>

References

FreeTDS

UnixODBC