This post describes the steps carried out to test my connection to Microsoft Sql Server from Ubuntu using ODBC.
This is part of my “How to connect to Sql Server from Ubuntu” series of blog post.
- The version of Ubuntu used in this guide is Ubuntu 12.04.2 LTS (Precise Pangoline) 32-bit.
- You are trying to connect to Microsoft Sql Server 2008 or later.
- You know how to use a terminal session in Ubuntu.
- You have installed unixodbc.
- You have installed freetds.
Note: Some of configuration files may be sitting in folders where your default user account may not have permission to edit them.
It is assume that that you will use “sudo” to gain the necessary elevated permission to edit the file if this happens.
- Location of odbcinst.ini file.
This file is where you will be placing configuration details of any ODBC drivers that you will be using.
If you have followed the steps in the previous blog post, this file should be at /usr/local/etc/odbcinst.ini
- Location of the freetds drivers.
If you have followed the steps in the previous blog post, this file should be at /usr/local/lib/libtdsodbc.ini
- Add driver configuration information for freetds
- Test freetds
- Configure freetds.conf for Sql Server connectivity testing
- Test connection
- Troubleshooting notes
Add driver configuration for freetds
1) Edit your /usr/local/etc/odbcinst.ini.
It should be blank. Add the following lines to the file and save the file.
Description = FreeTDS unixODBC Driver
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
After you have configure the driver, you can test the connection to your Sql Server.
We will use the tsql command to test the connection.
1) Start a terminal session. Type tsql –C on the command line.
This command displays some of freetds configuration.
You should see results like the below.
Of note is the freetds.conf directory. This file is used to define Sql Servers aliases as well as freetds driver default settings.
2) Assuming you manage to display the configuration information, this means that your freetds setup correctly.
The next thing to try to do is probe for your Sql Server.
You do thing by typing the command tql –LH <machine_hostname>
<machine_hostname> can be hostname or an IP address.
In the screen below, the machine that host my Sql Server is SARA, so I typed tsql –LH SARA
From the above results, I can see that I have 2 instances of Sql Server installed on the host SARA.
It is important to note the TCP port number used by the server instance.
For the remainder of this post, we will try to connect to the SQLEXPRESS instance.
But prior to that, we will need to add a configuration setting in order to use the tsql command to connect to Sql Server.
1) Remember the freetds.conf file that you noted in step 1 of under test freetds? The one located at /usr/local/etc/freetds.conf ?
There are content in this file. But you don’t really need them at this stage. What you really need are just the following lines:
tds version = 8.0
Save the file.
The reason this step is required is because the tsql command utility do not have any parameters that allow you to specify the TDS version that it should use to connect to Sql Server. This is a problem when trying to connect to Sql Servers 2005 or later.
Hence, we set it as a global setting so that the default TDS version that driver will revert to is version 8.0
1) Now you can use the tsql command to connect to your Sql Server.
Assuming my database user account is dbadmin, I would connect to the server by typing the following at the command line:
tsql –S SARA\\SQLEXPRESS –U dbadmin
If you get a screen like the below, it means you have successfully connected to your Sql Server.
You can run sql statements in this shell.
Type quit in the shell to exit.
If you get a message like “There was a problem connecting to the server” as per the screen below, check your freetds.conf file and make sure tds version is set to 8.0 under global settings section.
Another problem you might encounter when test is the following.
You appear to have logged in successfully, but you get a running numeric prompt that starts from 1, 2, 3, …until such a time it timed out.
Essentially, it means tsql was not able to locate the server.
Checkpoints for this problem are:
- Is this correct server?
If you are trying to connect to a database instance, you should specify tsql –S SARA\\SQLEXPRESS –U dbadmin
- Is the server running on default port?
Note the TCP used by Sql Server by using the tsql –LH <machine_hostname> command. Then try the tsql command again by specify the port number with –p parameter. That is to say:
tsql –S SARA\\SQLEXPRESS –U dbadmin –p 1433
Hope this helps anyone who had trouble setting up connection to Sql Server from their Ubuntu boxes.