rails 连接oracle

原以为rails连接oralce就像mysql那么简单,没想到还是费了一番周折,记录下来备忘
Windows
1)安装oralce 客户端
2下载 ruby-oci8-1.0.1-mswin32.rb
然后dos下rubyruby-oci8-1.0.1-mswin32.rb
D:\>ruby ruby-oci8-1.0.1-mswin32.rb
Copy OCI8.rb to D:/dev/ruby/lib/ruby/site_ruby/1.8/DBD/OCI8
Copy oci8.rb to D:/dev/ruby/lib/ruby/site_ruby/1.8
Copy oci8lib.so to D:/dev/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt
OK?
Enter Yes/No: yes
Copying OCI8.rb to D:/dev/ruby/lib/ruby/site_ruby/1.8/DBD/OCI8 ... done
Copying oci8.rb to D:/dev/ruby/lib/ruby/site_ruby/1.8 ... done
Copying oci8lib.so to D:/dev/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt ... done
OK
3)拷贝apdater文件
The oracle adapter does not exist on Rails 2 by default so:
  1. Check out the oracle adapter
    svn co
    http://svn.rubyonrails.org/rails/adapters/oracle/lib/active_record/connection_adapters/
  2. And move oracle_adapter.rb to your ActiveRecord adapters path
    [your ruby path]/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters
  3. Try again, it should work
4)
database.yml
development:
adapter: oracle
database: [your database as it appears on TNSNAMES.ora]
username: [your username]
password: [your password]
Linux
1)下载和安装oralce 10g 客户端
下载oracle-instantclient-basic-10.2.0.3-1.i386.rpm
oracle-instantclient-devel-11.1.0.1-1.i386.rpm
rpm -ivh oracle-instantclient-basic-10.2.0.3-1.i386.rpm
rpm -ivh oracle-instantclient-devel-11.1.0.1-1.i386.rpm
设置环境变量
LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.3/client/lib
export LD_LIBRARY_PATH
最好放在/etc/profile,这个不仅仅是安装时用到,程序运行时也需要用到
3)安装客户端api
cd /usr/local/src
tar xzvf ruby-oci8-1.0.1.tar.gz
cd ruby-oci8-1.0.1
make && make install
4) 拷贝apdater文件
The oracle adapter does not exist on Rails 2 by default so:
4.2) And move oracle_adapter.rb to your ActiveRecord adapters path
[your ruby path]/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters
5)
database.yml
development:
adapter: oracle
database: [your database as it appears on TNSNAMES.ora]或者[//主机名:端口/服务名]
username: [your username]
password: [your password]

注意上面的database有两种写法,如果用第一种,必须系统存在tnsnames.ora,
在一台没有安装oracle server的linux机器上是不存在这个文件到,但客户端会默认
寻找/etc/tnsnames.ora,如果没有这个文件会报错:
env.c:257:in oci8lib.so: ORA-12154: TNS:could not resolve the connect identifier specified (OCIError)
如果本来就在oralce server上,那么这么写是可以的
第二种写法通用性很强,完全无需tnsnames.ora
阅读更多
想对作者说点什么?

博主推荐

换一批

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