Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect my rails app to Oracle

I have a server running Oracle, IP is 192.168.1.50.

On my linux box, I need to connect to this Oracle server. I then installed Oracle Instant client and set the environment variables accordingly:

OCI_INCLUDE_DIR=/home/luc/instantclient_11_2/sdk/include
LD_LIBRARY_PATH=/home/luc/instantclient_11_2
DYLD_LIBRARY_PATH=/home/luc/instantclient_11_2/
OCI_LIB_DIR=/home/luc/instantclient_11_2
ORACLE_HOME=/home/luc/instantclient_11_2

I have also installed the appropriate gem:

ruby-oci8 (2.1.0)

Once I've defined my models and ran rake db:migrate I got the following error message:

rake aborted!
ORA-12154: TNS:could not resolve the connect identifier specified
oci8.c:360:in oci8lib_191.so
/home/luc/.rvm/gems/ruby-1.9.3-p125/gems/ruby-oci8-2.1.0/lib/oci8/oci8.rb:123:in `initialize'
/home/luc/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-oracle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb:319:in `new'
/home/luc/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-oracle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb:319:in `new_connection'
/home/luc/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-oracle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb:429:in `initialize'
/home/luc/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-oracle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb:24:in `new'
/home/luc/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-oracle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb:24:in `initialize'
/home/luc/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-oracle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanced_connection.rb:9:in `new'
....

my database.yml is:

development:
  adapter: oracle_enhanced
  host:  192.168.1.50:1521/orcl
  username: USER
  password: PASS

sqlplus connection works perfectly though:

sqlplus USER/[email protected]:1521/orcl

SQL*Plus: Release 11.2.0.3.0 Production on Wed Mar 21 17:34:26 2012

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> 

Is there something missing in this conf ?

UPDATE

I have tested from command line and the connection is working fine:

ruby -rubygems -e "require 'oci8'; OCI8.new('USER','PASS','192.168.1.50/orcl').exec('select * from users') do |r| puts r.join(','); end"

=> OK

Same thing from irb:

ActiveRecord::Base.establish_connection(:adapter => "oracle_enhanced", :database => "//192.168.1.50/orcl",:username => "USER",:password => "PASS")

=> OK

but still not working from my rails app.

UPDATE 2

Using database instead of host fixed the thing:

development:
  adapter: oracle_enhanced
  database:  //192.168.1.50:1521/orcl
  username: USER
  password: PASS
like image 321
Luc Avatar asked Mar 21 '12 16:03

Luc


2 Answers

Using database instead of host fixed the thing:

development:
  adapter: oracle_enhanced
  database:  //192.168.1.50:1521/orcl
  username: USER
  password: PASS
like image 163
Luc Avatar answered Oct 07 '22 06:10

Luc


If you want to use the EZConnect syntax to connect to Oracle, the host in your database.yml file would need to include the leading slashes, i.e.

host: //192.168.1.50:1521/orcl

There are examples of other ways to configure Rails to access an Oracle database in this OTN article on Connecting to Oracle in Ruby on Rails.

like image 32
Justin Cave Avatar answered Oct 07 '22 08:10

Justin Cave