热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HowtoconnecttoMicrosoftsqlserver2005fromrailsonlinux(centos5)

为什么80%的码农都做不了架构师?Assumed:ruby1.8.6rails:2.2.2centos564bitsqlserver2005Required:u

为什么80%的码农都做不了架构师?>>>   hot3.png

Assumed:

  ruby 1.8.6

  rails : 2.2.2

  centos5  64bit

  sqlserver2005

Required:

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

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

  Ruby ODBC >= 0.996 (http://www.ch-werner.de/rubyodbc/)

  Ruby DBI >= 0.0.23 (http://ruby-dbi.sourceforge.net/)

First:

 Use ~/.bashrc for your local profile or /etc/profile to set this for all users.

    export ODBCINI=/etc/odbc.ini

    export ODBCSYSINI=/etc

    export FREETDSCONF=/etc/freetds/freetds.conf

install unixODBC:

  apt-get install unixODBC or yum install unixODBC

  but I am use rpm,like this:   rpm -ivh  unixODBC-2.2.11-7.1.x86_64.rpm

  you can install unixODBC manually,like this:

    # cd  /usr/local/src

    # wget http://www.unixodbc.org/unixODBC-2.2.11.tar.gz

    # tar -xzvf unixODBC*.tar.gz

    #cd unixODBC*

    # ./config --prefix=/usr/local -sysconfdir=/etc --disable-gui

    #make

    #make install

install FreeTDS:

   you can install freetds by apt-get or yum.

    rpm -ivh freetds-0.64-11.el5.centos.x86_64.rpm

  or install freetds manually,like this:

    wget ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz

    tar -xzvf freetds-stable*.tgz

    cd freetds-stable*

    ./configure —with-unixodbc=/usr/local

    make

    make install

    configure FreeTDS:

    add a section to /etc/freetds/freetds.conf or /etc/freetds.conf:

    [your_db_name]

         host = 192.168.1.101 (change this to the right one for you)

     port = 1433

     tds version = 8.0 #(might be different, check "here":http://www.freetds.org/userguide/choosingtdsprotocol.htm.) #8.0 is the minimum for some important features of SQL server

Test FreeTDS:

   tsql -S  your_db_name  -U  name -P pw

  you should see and do the following to make sure all went well:

   locale is "zh_CN"
   locale charset is "GB2312"
1>use  your_actual_db

2> go

1> select * from user    (do this for a table that exists in your db)

2> go

id   login

1    user1

2    user2

(make sure you get correct output)

1>quit

create db definition

 edit: /etc/odbc.ini:

[your_db_name]

Driver            = FreeTDS

Description   = ODBC connection via FreeTDS

Trace             = No

Servername    = your_db_name

Database        = your_actual_db

edit: /etc/odbcinst.ini:

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

Test unixODBC:

isql -v your_db_name username pw 

You should see:

  +---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> select * from user

+---------------------------------------+
| id | login |
+---------------------------------------+
| 1 | user1 |
| 3 | user2 |
+---------------------------------------+
2 rows affected
2 rows returned

SQL> quit

ruby odbc:

gem install dbi

gem install dbd-odbc

Test everything:

 # irb1.8 irb(main):001:0> require "dbi"

=> true

irb(main):004:0> dbh = DBI.connect('dbi:ODBC:your_db_name', 'username', 'pw')

=> #, @trace_mode=2, @handle=#>> irb(main):005:0> quit

Setup rails:

 database.yml

  development:

      adapter: sqlserver

      mode: odbc

      dsn: your_db_name

      username: USERNAME

      password: PASSWORD

Last:

 gem install activerecord-odbc-adapter

 

Everything is ok,now test the rails:

 [root@b03 ruby]# ruby script/console
 Loading development environment (Rails 2.2.2)
Tue Feb 23 14:59:18 +0800 2010 [Info] [Module] Copy plugin app_frame resources to Rails.public
Tue Feb 23 14:59:18 +0800 2010 [Info] [Module] Copy plugin endless_page resources to Rails.public
Tue Feb 23 14:59:18 +0800 2010 [Info] [Module] Copy plugin sfp_skin resources to Rails.public
>> ActiveRecord::Base.connection.execute('select * from message')
=> #
>>

 

参考:http://oldwiki.rubyonrails.org/rails/pages/HowtoConnectToMicrosoftSQLServerFromRailsOnLinux/versions/140


 

 

 


转:https://my.oschina.net/tomsu/blog/2862



推荐阅读
author-avatar
勇士8853
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有