March 27th, 2008 Posted in Linux | 5 Comments »
As much as I wish I could have a pure Linux machine room, this just is not to be. I have to watch over several windows machines in addition to the Linux bits I love so much. Recently I’ve had a need to talk to our Microsoft SQL servers and pull data from them to our Linux servers for statistics gathering. In order to get CentOS to talk to Microsoft’s SQL server, you’re going to need FreeTDS.
To get FreeTDS, you will need to use the RPMForge repository. With RPMForge enabled, run the command yum install freetds. Once this command finishes it’s time to configure freetds.
To configure FreeTDS, there really isn’t that much you need to do. Simply open /etc/freetds.conf, scroll to the bottom of the file, and add a line for your MSSQL server similar to the sample listings in the file already. Basically you should have a section looking like the one below:
[mymssql]
host = my.mssql.server
port = 1433
tds version = 8.0
Now, at this point we need to test the connection between FreeTDS and MSSQL, so I’ll assume that you have a user who can connect to MSSQL via a network connection. If not, you need to make one. To test FreeTDS, open up a terminal window and type in tsql -S mymssql -U username If all goes well, you should be prompted for a password, and then get a numbered prompt. See below for the expected results:
[root@statbox etc]# tsql -S mymssql -U statuser
locale is “en_US.UTF-8″
locale charset is “UTF-8″
Password:
1>
If you get the numbered prompt like the one above, then so far so good. At this point, you can actually run sql queries directly from FreeTDS’s tsql client, but it can be incredibly cumbersome to do so. A better method to extract data from MSSQL is to use the unixODBC package to communicate with FreeTDS for you. The unixODBC client will clean up and clarify the responses you get from FreeTDS so that you can actually make sense of things. It should already be installed on your system, but you can verify by running the command rpm -q unixODBC. If rpm tells you that it’s not installed, simply use yum to get it from the base CentOS repository.
To use the unixODBC package, we again have to configure a few things, because by default unixODBC comes configured only for postgresql. Most of the guides you’ll find reference the gui utilities for configuring things, but there’s really no need for all of this. The easiest way is to simply create a couple template files and import them.
The first template file that we need to create is the driver, which tells unixODBC how to talk to FreeTDS. For this, open up your favorite text editor (it had better be vim) and create a file called driver.tpl with the contents listed below:
[FreeTDS]
Description = version 0.64
Driver = /usr/lib/libtdsodbc.so.0
Now save this file, and import it (as root) by running the command odbcinst -i -d -f driver.tpl. You should get some output similar to the following:
odbcinst: Driver installed. Usage count increased to 1.
Target directory is /etc
You can verify that everything was added properly by checking the contents of /etc/odbcinst.ini. Now what we need to do is set up the data portion for ODBC. This is done on a per-user basis and creates a DSN, or Data Source Name.
As your normal user (or whoever will be connecting to the database), create a text file called datasource.tpl with the following format, substituting your own information where appropriate:
[mymssql]
Driver = FreeTDS
Description = Sample Database
Trace = No
Server = my.mssql.server
Port = 1433
Database = puppies
Once you’ve got this file all set, we need to create the DSN. To do this run the command: idbcinst -i -s -f datasource.tpl. We should be able to test this now by using isql. You should see a prompt like the one below:
[user@statbox ~]$ isql -v mymssqll statuser statpass
+—————————————+
| Connected!
|
| sql-statement
| help [tablename]
| quit
|
+—————————————+
SQL>
That’s pretty much it. From here, you can simply use isql to run your mssql queries and grab the information that you need, just like you would with mysql or postgresql.