Wednesday, December 19, 2012

SQL server connection for perl from Ubuntu

I had to connect to a remote Microsoft SQL server from a perl module on Linux recently, and it turned out to be not as straight forward as MySql database connection.  There are a couple ways to do it:

Use DBI + Sybase driver
It sounds like a hack, because it is.  We're using sybase driver to connect to MS SQL server.  Apparently, it works, and that's all that it matters.   The connection string for sybase looks like this: DBI:Sybase:server=server.hostname.com

Use DBI + ODBC + FreeTDS driver
This setup basically creates a Datasource name (DSN) for the connection in the driver configuration file.  Thanks to this blog, which provided step to step guide to set it up.  Though it is intended for php script, I found it working for perl module as well.

sudo cpan DBD::ODBC
sudo apt-get install unixodbc-dev tdsodbc

Install FreeTDS by downloading here if it's not already installed.
Edit the TDS config file to add a DSN /etc/freetds.conf
[MSSQL]
host = server host name or IP
port = 1433
tds version = 8.0

Edit  /etc/odbcinst.ini
[FreeTDS]
Description = FreeTDS driver
Driver = /usr//lib/odbc/libtdsodbc.so
Setup=/usr/lib/odbc/libtdsS.so
FileUsage = 1
UsageCount = 1

Edit /etc/odbc.ini
[MSSQL]
Description = MS SQL Server
Driver = /usr/lib/libtdsodbc.so
Server = server name or ip
UID = devuser
PWD = devpass
ReadOnly = No
Port = 1433

Test configuration with isql: isql -v MSSQL devuser 'devpass'

The connection string for perl module would look like this: dbi:ODBC:MSSQL

I'll probably add some sample code later.


No comments:

Post a Comment