Skip to content

Notes on science, software and the craft of support

Martin EvansNotes and resources

Blog post

Access Microsoft SQL Server from Unix in ten minutes

A short walkthrough for reaching Microsoft SQL Server from Linux with unixODBC and the Easysoft ODBC driver, ending with a working isql session.

In this example for Linux we install the unixODBC driver manager and an ODBC driver, then get data back from Microsoft SQL Server. We ignore access from any particular programming language or interface and use the isql utility that comes with unixODBC. For further information about access from PHP, Perl or Python, see the developer resources at Easysoft.

You will need to know the name or IP address of the machine where SQL Server is installed, and for SQL Server authentication you will need a valid user name and password.

Install the driver manager

  1. Open your package manager. On Ubuntu it is under System, then Administration, then Synaptic Package Manager.
  2. Find the main unixODBC packages. They are usually split into a binary package, GUI tools and a development package. You only need the binary package for this demonstration, but you will need the development package later if you use a language such as Perl.
  3. Install the unixODBC binary package. This usually gives you the unixODBC driver manager shared object and the command line tool isql.
  4. If you are not using Linux or a Unix system that packages unixODBC as a binary, either compile unixODBC yourself or install the copy that comes with the driver.

Install the SQL Server driver

  1. Go to the Easysoft SQL Server page and download the SQL Server ODBC driver for your platform.
  2. Extract the downloaded archive with the tar utility and change into the directory it creates.
  3. Run the installer with sudo, or become root and run it directly.
  4. You can accept most of the defaults, but note the following questions. It is advisable to install the driver in /usr/local/easysoft because extra work is needed if you do not.
  5. Since unixODBC is already installed, use your installed unixODBC instead of the copy included with the driver. The included copy is usually the better default on non Linux platforms, but on Linux the packaged version is easier to maintain.
  6. When the installer asks about a licence, accept the default and choose the SQL Server ODBC Driver licence. Enter the requested name, company, email and telephone details, leave the authorisation code empty for a trial licence and, if the machine has an internet connection, choose the option to obtain a trial licence automatically.
  7. The installer asks whether you have a SQL Server that can be reached. Answer the prompts and it will build a connection string and save it as a Data Source Name that you can use later. If you use SQL Server Express, append \SQLEXPRESS to the end of the host name or IP address.

Test the connection

You should now have a DSN in the /etc/odbc.ini file under the name you provided. If you installed unixODBC from an operating system package, isql and the driver shared objects should be on your path and registered with the dynamic linker. Otherwise, add /usr/local/easysoft/unixODBC/bin to your PATH and export it, and add /usr/local/easysoft/unixODBC/lib and /usr/local/easysoft/sqlserver/lib to LD_LIBRARY_PATH or the equivalent variable on your platform.

Run isql with the verbose option, followed by your DSN name and the user name and password for SQL Server:

isql mydsn sa password

Type help and press return and you should get a list of tables. For SQL Server there is a table called information_schema.tables, so you should be able to run:

select * from information_schema.tables

You should also be able to select data from any table, although isql is only a test program and does not display binary large objects. Once this works, the same DSN can be used from an application or a scripting language.