Translations of this page?:

Connect To MicrosoftSQLServer From Rails On Linux Box

Install the following application(s) on your Linux box

unixODBC >= 2.2.11 (http://www.unixodbc.org/)

unixodbc-dev

FreeTDS >= 0.63 (http://www.freetds.org/)

freetds-dev

Note that you need to install unixODBC before FreeTDS: The ./configure step of FreeTDS is dependent on unixODBC being installed: ./configure —with-unixodbc=/usr/local

Ruby ODBC binding (http://www.ch-werner.de/rubyodbc/ruby-odbc-0.9997.tar.gz). Extract and in the extracted folder run:

 ruby extconf.rb
 make
 sudo make install

Install the following gems

 sudo gem install dbd-odbc
 sudo gem install dbi

If you are using SQL Server 2000, 2005, or 2008:

 sudo gem install activerecord-sqlserver-adapter

For more information, please see the rails adapterproject page

DSN-less connection

You don't have to mess with all the FreeTDS configuration files in the rest of this article if you use a DSN-less connection in your database.yml like the following:

 database_name:
   adapter: sqlserver
   mode: ODBC
   dsn: 'DRIVER=/usr/local/lib/libtdsodbc.so;TDS_Version=8.0;SERVER=Your_Server_Name;DATABASE=Your_Database_Name;Port=1433;uid=Your_UID;pwd=Your_PWD;'

The obvious problem with this approach is having a different freetds installation location on development and production machines.

Make sure you set the Driver to the actual path of the your driver.

configure FreeTDS

Add a section to /etc/freetds/freetds.conf

  [YOUR_DB_DEFINITION_NAME]
      host = <HOST NAME/IP ADDRESS>
      port = <PORT NUMBER>
      tds version = 8.0 
      client charset = UTF-8

Create DB Definition

Edit /etc/odbc.ini:

  [YOUR_DB_DEFINITION_NAME]
      Driver          = FreeTDS
      Description     = ODBC connection via FreeTDS
      Trace           = No
      Servername      = YOUR_DB_DEFINITION_NAME
      Database        = YOUR_ACTUAL_DB_NAME

Edit /etc/odbcinst.ini:

  [FreeTDS]
     Description     = TDS driver (Sybase/MS SQL)
     Driver          = /usr/lib/odbc/libtdsodbc.so
     Setup           = /usr/lib/odbc/libtdsS.so
     CPTimeout       =
     CPReuse         =
     FileUsage       = 1

Try these in your Linux Box

tsql -S YOUR_DB_DEFINITION_NAME -U USERNAME -P PASSWORD

or

sqsh -S YOUR_DB_DEFINITION_NAME -U USERNAME -P PASSWORD

database.yml

  development:
    adapter: sqlserver
    mode: ODBC
    dsn: YOUR_DB_DEFINITION_NAME
    username:YOUR_DB_USERNAME
    password:YOUR_DB_PASSWORD

  test:
    adapter: sqlserver
    mode: ODBC
    dsn: YOUR_DB_DEFINITION_NAME
    username:YOUR_DB_USERNAME
    password:YOUR_DB_PASSWORD

  production:
    adapter: sqlserver
    mode: ODBC
    dsn: YOUR_DB_DEFINITION_NAME
    username:YOUR_DB_USERNAME
    password:YOUR_DB_PASSWORD

Discussion

Chad Johnson, 2009/03/09 14:58

Here are some additional notes on getting MS SQL connections (via FreeTDS) with DSN-less OSBC connections:

http://lists.ibiblio.org/pipermail/freetds/2009q1/024482.html

Roberto Lopez, 2009/03/12 03:29

I did not find activerecord-sqlserver-adapter. Instead I installed akitaonrails-activerecord-sqlserver-adapter-1.1.1.

Chad Johnson, 2009/03/13 14:44

Robert, run this command first :

gem sources -a http://gems.rubyonrails.org

That should solve your problem.

Jens-Harald Johansen, 2009/03/25 09:10

I'm located behind a firewall with no way out. Where can I download the official file ?

Found a host with direct HTTP connection to the internet so I just ran the following and copied the file to the other server:

sudo gem sources -a http://gems.rubyonrails.org

sudo gem fetch activerecord-sqlserver-adapter

mauricio, 2009/03/27 19:25

Hi, I'm running an app in a FedoraC6, Apache (using fcgi) and a local MySql primary database, also a secondary SqlServer database at other box

I've followed this tutorial step by step and now the connection to sqlServer its working through command line tsql and performing well, also working through ruby but not so well.

1. If I ask the database for a big amount of data I'm getting an Application error Rails application failed to start properly message

2. If ask for just a couple of rows it do the job and retrieve the data

I use unix “top” command to see how proccesses consume resources CPU & MEM and for case (1) I can see up to three or four dispatch.fcgi proccess “eating” a lot of resources

Does somebody know if this is this normal? what can be happening?, how to solve?

thanks in advance

George Roman, 2009/09/15 17:16

I just finished wrestling with the following problem and thought I could help others avoid it as well.

If you get this error running isql:

[IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified

make sure the properties in all three conf files do not have any indentation. The examples above were indented and it took me a while to figure out why things weren't working.

Ken Collins, 2009/10/27 15:35

I would defer to the adapter's wiki page for upto date info. For instance, if you just install the latest DBI/DBD-ODBC, things are not going to work. There are specific blessed versions and the README and wiki for the adapter project stay current. http://wiki.github.com/rails-sqlserver/2000-2005-adapter/platform-installation

 
database-support/ms-sql.txt · Last modified: 2009/07/14 00:58 by rps
 
Recent changes RSS feed Creative Commons License