Setting Up An Erlang and SQL Server Test Rig

We are in the beginnings of a project in which we will be building an Erlang service. The first step in being able to develop anything is to have a test rig that will allow you to mimic the final production environment.

Our Erlang system will be connecting to both a SQL Server 2012 database and also to a Riak database. In it’s final form, the Erlang service and the Riak database will be running on a Linux server and, obviously, the SQL Server will be running on a windows server. For our test rig, however, I will be running the whole set-up out of my laptop using a VM Player and an Ubuntu Linux virtual machine.

The majority of the configuration effort will be done in the Linux machine but there are some minor configuration changes that must be done on SQL Server so that it will accept the connection from the Linux machine. At a high level these are the steps:

  1. Set up the VM
  2. Install Ubuntu Linux
  3. Install Erlang in Linux
  4. Install IntelliJ IDE and Erlang plug-in in Linux
  5. Install ODBC drivers in Linux
  6. Configure MS SQL Server to accept external connections
  7. Test connectivity

Let’s begin the adventure…

Installing the Ubuntu

General steps are:

  1. Download the ISO image from the Ubuntu website
  2. Create a VM
  3. Configure (2 GB ram, 2 cores, 40 GB hd, split files)
  4. Boot the VM from the ISO image

Installing Erlang

Go to erlang solutions and select the Ubuntu “tab”:


At the time I installed it I was using the “Trusty” version of Ubuntu, which is 14.04. You can find out which version of Ubuntu you are running by using the following command:

$ lsb_release -c

In my case the result was:

[email protected]:/usr/bin/idea-IC-139.659.2/bin$ lsb_release -c
Codename: trusty

In the website look at the available sources definitions and add the following line to the /etc/apt/sources.list using your favorite editor. For example for me it was (Ubuntu 14.04):

deb trusty contrib

Next, add the Erlang Solutions public key for apt-secure using following commands:

$ wget
$ sudo apt-key add erlang_solutions.asc</code></pre>

All the previous steps get Ubuntu ready to download the packages directly from Erlang solutions. Now we can do the actual install with these commands:

$ sudo apt-get update
$ sudo apt-get install erlang

If all is well you should be able to run in the command line erl command and get the Erlang VM. To run the shell:

$ erl

That’s it for this step.

Installing git

You will need Git to download the latest version of rebar (it’s like a make for erlang). To do that issue the following command:

$ sudo apt-get install git

That’s it for this step.

Installing Rebar

I installed rebar in the /usr/lib directory to be consistent with all the other libraries. To do that first go to the directory and then download the rebar files:

$ cd /usr/lib
$ sudo git clone git://
$ cd rebar
$ sudo ./bootstrap #compiles rebar
$ cd /usr/bin
$ sudo ln -s /usr/lib/rebar/rebar #creates a link to the resulting rebar executable

With this Rebar should be complete.

That’s it for this step.

Installing IntelliJ and the Erlang IDE plug-in

Go to the IntelliJ site and see the instructions for downloading the install files. I put my files under the /usr/bin directory. In this example the actual install file was sitting in my home directory so:

$ cd /usr/bin
$ sudo tar xvzf /home/ilhadad/ideaIC-14.0.2.tar.gz

These commands unpack the file in the /usr/bin directory. Now we need to make it easier to start the program by creating a link in the /usr/bin directory. Like this:

$ sudo ln -s idea-IC-139.659.2/bin/

To run the IDE we just have to run in the command line from anywhere:

To install the Erlang plug-in go to this site and follow the directions

That’s it for this step.

Setting up SQL Server and Windows

Enable the firewall connection by:

  • Start->Run->firewall.cpl
  • Right click on Inbound Rules and select “New Rule”
  • Select “Port” on the radio button and click on “Next”
  • Select “TCP” on the radio button, specify port 1433 and click on “Next”
  • Select “Allow the connection” and click on “Next”
  • Make sure all checkboxes are selected e.g. “Domain”, “Private”, and “Public”
  • Give the Rule a meaninful name. e.g. “SQL Server 2012” and click on “Finish”

Once the firewall is enabled then we we need to enable SQL Server(2012) so that it will accept external connections. To do that:

  • Run “SQL Server Configuration Manager”.
  • In the interface on the right hand side select “SQL Server Network Configuration” -> “Protocols for SQLSERVER2012”.
  • On the left pane make sure “TCP/IP” is “Enabled”.
  • Right click on “TCP/IP” and in the context menu select properties.
  • In the “TCP/IP Properties” dialog scroll down to find the “IP2” and ensure the IP address of the host is there.
  • Scroll down to IPAll.
  • Make sure that TCP Dynamic Ports is blank. (Mine was set to some 5-digit port number.)
  • Make sure that TCP Port is set to 1433. (Mine was blank.)
  • Then “Apply” the changed settings.
  • Run->services.msc
  • Stop and Start the Microsoft SQl Server so the settings take.

That’s it for this step.

Installing ODBC

I got the information to get it up an running from these sites: and

This will be the ODBC system in Ubuntu.

$ sudo apt-get install unixodbc

Then you will need the MS SQL Server drivers. To get them follow these instructions to access SQL Server database install the packages:

$ sudo apt-get install freetds-bin freetds-common tdsodbc odbcinst1

This provides the libraries you need. Copy the contents of /usr/share/doc/freetds-common/examples/odbcinst.ini into /etc/odbcinst.ini. This registers the FreeTDS driver with the ODBC layer.

Restart your webserver to load the ODBC module into PHP.

Test the connection by going back to the linux terminal and typing:

$ tsql -H  -U usernamehere -P pwdhere -p 1433


locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"

If you get a response as shown above then you know all is well.


The following instructions came from

Here two parameters are of primary concern to us for configuration

  1. freetds.conf directory
  2. TDS Version

As shown above freetds.conf directory is /etc/freetds and our TDS Version is 4.2

Now we need to edit /etc/odbcinst.ini but before that just find out the path of the library It is generally installed in /usr/lib/x86_64-linux-gnu/odbc/ directory but still you can verify by issuing the follwing command on the linux terminal

find / -name

Now open /etc/odbcinst.ini and create a new section as show below

Description = FreeTDS Driver v0.91
Driver = /usr/lib/x86_64-linux-gnu/odbc/
Setup = /usr/lib/x86_64-linux-gnu/odbc/

Now open /etc/odbc.ini and create a new connection identifier

Driver = FreeTDS
Description = Development server
Trace = No
ServerName = mssql
Port = 1433
Database = SSA_DB
TDS_Version = 4.2

Now open /etc/freetds/freetds.conf and create a new section:

# Added this connection for SSA development
        host =
        port = 1433
        tds version = 4.2

Note that the host name should be the IP or DNS resolveable name of the host which has SQL server installed. OK! we are done. Now you can test the connection by using the isql utility

isql -v SSA_DB usernamehere pwdhere

Where usernamehere is a valid sql server user and pwdhere is the corresponding password for that user.

It should connect successfully to the MSSQL server

Getting Erlang to connect to SQL Server via ODBC

First you need to start the ERLANG ODBC Server. Here’s how:

Run Erlang VM first:

$ erl
Erlang/OTP 17 [erts-6.3] [source] [64-bit] [smp:2:2] [async-threads:10] [kernel-poll:false]

Eshell V6.3  (abort with ^G)

In the Erlang shell start the ODBC Server and establish a connection:

1> odbc:start().
2> {ok,DbRef}=odbc:connect("DSN=SSA_DB;UID=SQL2005_309367_ey_user;PWD=dilly07",[]).

Now to do some queries:

3> odbc:sql_query(DbRef, "SELECT * FROM eyProject").
          [{1,33,"Test 2",null,null,null,
           {2,19,"new test",null,null,null,

 {11,38,"8+4 V2 - Data From Vinay",null,null,null,
           {17,41,"0+12 Jan 6",null,null,null,
           {18,41,"0+12 Jan 9",null,null,null,{{...},...},{...},...}]}


Erlang should now be ready to work with SQL Server via ODBC. Enjoy!

Keep coming back for more details on how to use Erlang with SQL Server.