Ubuntu Server 64bits 如何安装 ruby-oci8

安装 Linux软件包:

sudo apt-get install libaio-dev unzip
 

转到Oracle网站下载适用于Linux64位的即时客户端基本和SDK。下载您已创建的Oracle文件夹中的文件,并将它们解压缩:

cd /opt/oracle
unzip oracle-basic-11.zip
unzip oracle-sdk-11.zip
cd instantclient_11_2/
sudo ln -s libclntsh.so.11.1 libclntsh.so

创建Oracle即时客户端系统变量

export LD_LIBRARY_PATH=/opt/oracle/instantclient_11_2
 

然后,安装ruby - oci8:

sudo env LD_LIBRARY_PATH=/opt/oracle/instantclient_11_2 /usr/bin/gem install ruby-oci8

测试连接到数据库,创建一个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 sysdate FROM dual')
while r = cursor.fetch()
puts r.join(',')
end
cursor.close
conn.logoff

 并运行它:

ruby sql.rb

常见错误信息:

sql:1:in `require’: no such file to load — oci8 (LoadError)

原因:require 'oci8' 前面需加上一行:

require ‘rubygems’
 

或创建一个系统变量,它会自动使用RubyGems:

RUBYOPT=”rubygems”export RUBYOPT

/var/lib/gems/1.8/gems/ruby-oci8-2.0.4/lib/oci8lib_18.so: libclntsh.so.11.1: cannot open shared object file: No such file or directory – /var/lib/gems/1.8/gems/ruby-oci8-2.0.4/lib/oci8lib_18.so (LoadError)

原因:没有配置LD_LIBRARY_PATH变量是。

相关推荐