gem install ruby-oci8安装步骤

1、下载oracle 相关的安装包

下载地址:http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html
版本选择:version 11.2.0.0.3.0(64-bit)

instantclient-basic-macos.x64-11.2.0.3.0.zip
instantclient-basiclite-macos.x64-11.2.0.3.0.zip
instantclient-sdk-macos.x64-11.2.0.3.0.zip
instantclient-sqlplus-macos.x64-11.2.0.3.0.zip

2、解压缩以及创建oracle相关的文件

解压缩cd instantclient_11_2

mkdir -p /usr/local/oracle/product/instantclient_64/11.2.0.3.0/bin
mkdir -p /usr/local/oracle/product/instantclient_64/11.2.0.3.0/lib
mkdir -p /usr/local/oracle/product/instantclient_64/11.2.0.3.0/jdbc/lib
mkdir -p /usr/local/oracle/product/instantclient_64/11.2.0.3.0/rdbms/jlib
mkdir -p /usr/local/oracle/product/instantclient_64/11.2.0.3.0/sqlplus/admin

创建oracle相关的文件,并将解压缩的文件迁移

mv ojdbc* /usr/local/oracle/product/instantclient_64/11.2.0.3.0/jdbc/lib/
    mv x*.jar /usr/local/oracle/product/instantclient_64/11.2.0.3.0/rdbms/jlib/
    mv glogin.sql /usr/local/oracle/product/instantclient_64/11.2.0.3.0/sqlplus/admin/login.sql
    mv *dylib* /usr/local/oracle/product/instantclient_64/11.2.0.3.0/lib/
    mv sdk /usr/local/oracle/product/instantclient_64/11.2.0.3.0/lib/sdk
    mv *README /usr/local/oracle/product/instantclient_64/11.2.0.3.0/
    mv * /usr/local/oracle/product/instantclient_64/11.2.0.3.0/bin/

3、编辑oracle的连接信息(可省略此步骤,用于测试,测试过程详见步骤9,测试完继续安装步骤4执行)

mkdir -p /usr/local/oracle/admin/network
    touch /usr/local/oracle/admin/network/tnsnames.ora

编辑tnsnames.ora,将oracle的连接信息写进去,例子如下:

ORADEMO= (description=(address_list=
                (address = (protocol = TCP)(host = 127.0.0.1)(port = 1521)))
                (connect_data =(service_name=orademo))  )

4、编辑~/.bash_profile文件(如果使用zshrc,则编辑~/.zshrc)

将下面的代码写到文件中

DYLD_LIBRARY_PATH="/usr/local/opt/oracle/instantclient_11_2"
export DYLD_LIBRARY_PATH
export ORACLE_HOME=$DYLD_LIBRARY_PATH
TNS_ADMIN=/usr/local/oracle/admin/network
export TNS_ADMIN
export NLS_LANG="AMERICAN_AMERICA.UTF8"

写好保存之后,重新导入~/.bash_profile文件(或~/.zshrc文件)

source ~/.bash_profile

创建链接(很重要,否则安装gem包会出错)

cd /usr/local/opt/oracle/instantclient_11_2
ln -s libclntsh.dylib.11.1 libclntsh.dylib

5、如果是其他系统现在就可以安装oracle官方提供的ruby-oci8 gem包了,但是Mac OS X EI Capitan 系统限制了普通软件使用root权限启动,所以还需要做一些操作(以下2种方案二选一即可)

方案A:

cd /usr/local/opt/oracle/instantclient_11_2
curl -O https://raw.githubusercontent.com/kubo/fix_oralib_osx/master/fix_oralib.rb
ruby fix_oralib.rb –a
export OCI_DIR=/usr/local/opt/oracle/instantclient_11_2
git clone https://github.com/kubo/ruby-oci8.git #获取ruby-oci8的source
git checkout ruby-oci8-2.2.1 #检查需要的版本,我用的是2.2.1的版本

修改ruby-oci8/ext/oci8/oraconf.rb文件,找到如下代码

when /darwin/ 
    @@ld_envs = %w[DYLD_LIBRARY_PATH] 
    so_ext = 'dylib'

改为如下显示:(即添加OCI_DIR)

when /darwin/ 
    @@ld_envs = %w[DYLD_LIBRARY_PATH OCI_DIR]  
    so_ext = 'dylib'
cd /usr/local/opt/oracle
gem build ruby-oci8.gemspec

安装ruby-oci8

gem install ruby-oci8-2.2.1.gem #注意版本号

看到如下显示就说明gem安装成功了

Building native extensions. This could take a while... Successfully installed ruby-oci8-2.1.2 1 gem installed

方案B:

如果想要在EI Capitan系统中安装ruby-oci8,需要禁用SIP,EI Capitan系统默认开启SIP

a、重启 Mac,按住 Command+R 键直到 Apple logo 出现,进入 Recovery Mode
b、点击 Utilities > Terminal
c、在 Terminal 中输入 csrutil disable,之后回车
d、重启 Mac

注:同样的方法输入:csrutil enable,就可以启动SIP,不推荐Mac新手使用此方法。

6、在Gemfile中添加:(根据ruby和rails的版本选择’activerecord-oracle_enhanced-adapter’的版本,我用的是ruby 1.9.3 和rails 3.1.1的,所以选择1.4.0版本)

gem 'ruby-oci8'
gem 'activerecord-oracle_enhanced-adapter', '~> 1.4.0'

7、设置database.yml

development:
        adapter: oracle_enhanced
        host: localhost #根据实际情况修改
        encoding: utf8
        database: test #这个参数为oracle的SID值
        username:      #oracle登录的用户名
        password:      #oracle登录密码
        port: 1521 #oracle默认端口为1521,根据实际情况修改

8、项目根目录下执行:

bundle install
rails s

9、测试oracle client是否安装成功

测试 连接到 数据库 ,创建 一个 Ruby文件 例如 sql.rb , 并复制 / 粘贴以下代码 :

require 'rubygems'  
require 'oci8'  
tnsnames = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = host_name_or_ip)(PORT = 1521)) (CONNECT_DATA = (SID = SID)))'  
conn = OCI8.new('user', 'password', tnsnames)  
cursor = conn.exec('SELECT * FROM users')  #括号中为oracle SQL的语句,自行修改
while r = cursor.fetch()  
puts r.join(',')
end  
cursor.close  
conn.logoff

保存并运行

ruby sql.rb