ubuntu下rails如何连接sqlserver?

RAILS连接SQL SERVER是一个比较复杂的事情.因为不同平台的处理方法不一样.

就拿linux来说,不同发行版的处理方法也有不同.

这里记录ubuntu下rails连接sqlserver的步骤.
参考: http://wiki.rubyonrails.org/rails/pages/HowtoConnectToMicrosoftSQLServerFromRailsOnLinux

1 编辑~/.bashrc文件,添加:

export ODBCINI=/etc/odbc.ini
export ODBCSYSINI=/etc
export FREETDSCONF=/etc/freetds/freetds.conf


2 安装unixodbc

sudo apt-get install unixodbc unixodbc-dev tdsodbc


3 安装FreeTDS

sudo apt-get install freetds-dev sqsh


配置FreeTDS: 编辑/etc/freetds/freetds.conf,添加:

[YOUR_DB_DEFINITION_NAME]
host = 192.168.1.101 (change this to the right one for you)
port = 1433
tds version = 8.0
charset = gb2312 # 数据库端编码
client charset = UTF-8 # 客户端编码

如果遇到乱码问题,需要根据具体情况设置编码.

 

测试FreeTDS:

sqsh -S YOUR_DB_DEFINITION_NAME -U USERNAME -P PASSWORD


4 定义DB
编辑/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


编辑/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


测试:

isql YOUR_DB_DEFINITION_NAME USERNAME PASSWORD


5 安装RUBY ODBC

sudo apt-get install libodbc-ruby1.8


6 安装RBI ODBC DRIVER

sudo gem install activerecord-odbc-adapter


到此安装结束.

databse.yml中这样定义:

development:
adapter: odbc
dsn: YOUR_DB_DEFINITION_NAME
username: USERNAME
password: PASSWORD

 


以上步骤在ubuntu8.04, ruby1.8.6, rails 2.1下测试通过.

阅读更多

没有更多推荐了,返回首页