Satya's blog - 2010/
|
Getting Rails to work with Oracle is straight-forward. Get the instant client libraries from Oracle. I got the following, and unzipped them into /opt/oracle/instantclient-VERSION: instantclient-basic-linux32-10.2.0.3-20061115.zip instantclient-sdk-linux32-10.2.0.3-20061115.zip instantclient-sqlplus-linux32-10.2.0.3-20061115.zip for n in instantclient*.zip; do unzip $n; done Symlink the directory so that we don't have to refer to it by version number, and then symlink certain files inside it because the libraries are supposed to be generically-named: ln -s instantclient-VERSION instantclient cd instantclient ln -s libclntsh.so.11.1 libclntsh.so ln -s libocci.so.11.1 libocci.so In /etc/apache2/envvars add: export LD_LIBRARY_PATH=/opt/oracle/instantclient:$LD_LIBRARY_PATH And then install the ruby-oci8 driver, and the activerecord_oracle_adapter: sudo env LD_LIBRARY_PATH=$LD_LIBRARY_PATH /usr/bin/gem install ruby-oci8 --version "< 2.0.0" sudo gem install activerecord-oracle-adapter Here's an example config/database.yml file: production: adapter: oracle database: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=foo.example.com)(PORT=1234))(CONNECT_DATA=(SID=databse))) username: foo password: boo There, all done. There might be a way to do this using ODBC but I'm just happy this works pain-free (unlike sybase). I've been trying to get Sybase working with Rails, as detailed before in my blog. Here's what eventually worked for me, on Ubuntu 9.10 and Rails 2.3.5, using Phusion Passenger. Install the rubygems package. Install rails and stuff via gem, and some ruby libraries via apt-get. Note: We apt-get install rubygems, and all the other rails stuff is installed as a gem. Because we wanted latest stuff. *Ruby* libraries are apt-get installed where possible, otherwise via gem. freetds and sybct libraries are compiled on the box. Yuck. The following gems are installed (I've taken out ones, like calendar_date_select, that are obviously irrelevant for this purpose): activerecord (2.3.5, 2.3.4, 2.3.3, 2.3.2, 1.15.6) activerecord-jdbc-adapter (0.9.2, 0.9.1) activerecord-odbc-adapter (2.0) activerecord-oracle-adapter (1.0.0.9250) activerecord-oracle_enhanced-adapter (1.2.3) activerecord-sybase-adapter (1.0.0.9250) activeresource (2.3.5, 2.3.4, 2.3.3, 2.3.2) activesupport (2.3.5, 2.3.4, 2.3.3, 2.3.2, 1.4.4) dbd-odbc (0.2.5) dbi (0.4.3) deprecated (2.0.1) fastthread (1.0.7) rack (1.1.0, 1.0.1, 1.0.0) rails (2.3.5, 2.3.4, 2.3.3, 2.3.2) rake (0.8.7) ruby-oci8 (1.0.7) ruby-odbc (0.9999) rubygems-update (1.3.5) I'd gem install rails, dbd-odbc, and rubygems-update. That should pull in most of the others as dependencies. Don't install ruby-odbc and ruby-oci8 right off. For ruby-odbc, see below, and for ruby-oci8, See another article (to be written). The oci8 is for Oracle. Additional stuff to install (again, only relevant stuff listed):
sudo gem install activerecord-sybase-adapter -s http://gems.rubyonrails.org
sudo apt-get install libiodbc2 libodbcinstq1c2 odbcinst1debian1 freetds-common freetds-dev tdsodbc unixodbc unixodbc-dev libdbi-ruby At some point, I got sybase-ctlib ruby library (sybct-ruby) from here: http://enjoy1.bb-east.ne.jp/~tetsu/sybct-ruby-0.2.12.tar.gz (sybase-ctlib project page). Also download freetds-stable, ./configure and make, and then edit sybct's extconf.rb file:
sybase = "/usr/local/"
$CFLAGS = "-g -Wall -DFREETDS -I#{sybase}/include"
$LDFLAGS = " -L#{sybase}/lib -L/freetds-0.82/src/tds/.libs"
$LOCAL_LIBS = "-lct -lsybdb -ltds -rdynamic -ldl -lnsl -lm"
Note the ldflags line — you have to tell it where libtds is located. You can
ruby extconf.rb make sudo cp sybct.rb sybct.so sybsql.rb /usr/local/lib/site_ruby/1.8/i486-linux/ Fix this destination according to your system. The command "make install" tries to put it in the right place, but doesn't copy all three files. Your /etc/freetds/freetds.conf file:
[A]
host = somehost1
port = 4100
tds version = 5.0
[D]
host = somehost2
port = 4100
tds version = 5.0
At this point you should be able to use the native sybase adapter in Rails. Your config/database.xml: my_db: adapter: sybase host: a database: whatever username: heh password: youwish
For the ODBC connection, [d] Description = d server, name elided Driver = /usr/lib/odbc/libtdsodbc.so Server=fqdn.example.com Port=4100 TDS Version = 5.0 [a] Description = a server, name elided Driver = /usr/lib/odbc/libtdsodbc.so Server=fqdn2.example.com Port=4100 TDS Version = 5.0
Test with this command: For ODBC with Rails: Per instructions from RubyODBC maintainer (Christian Werner, whom I hereby thank again). First line removes libdbd-odbc-ruby libdbd-odbc-ruby1.8 libodbc-ruby1.8 if they were installed: sudo apt-get purge libodbc-ruby1.8 sudo gem install ruby-odbc -- --build-flags --disable-dlopen And that got Sybase over ODBC to work. disable-dlopen is kinda important. Important information that I was looking for but couldn't find: "-- --build-flags" is how you pass stuff to extconf.rb when the gem install command is going to compile the gem. Now you should be able to use ODBC in Rails with this config/database.yml file: my_db: adapter: odbc dsn: a database: whatever username: heh password: youwish Phew! Update: I don't think you need the sybse-ctlib library, nor the freetds source/compile, if you're only doing the ODBC thing. You can get by with the freetds package installed by apt-get. If you're using the activerecord sybase adapter, that's when you need sybase-ctlib and therefore the freetds source.
Summary:
# freetds and odbc packages (the last two may auto-install as dependencies): sudo apt-get install freetds-common tdsodbc unixodbc \ odbcinst1debian1 libodbcinstq1c2 # for ruby-odbc to compile correctly: sudo apt-get purge libodbc-ruby1.8 # to build ruby-odbc: sudo apt-get install unixodbc-dev sudo gem install ruby-odbc -- --build-flags --disable-dlopen Last updated: Feb 22 2010 14:14 James P. Hogan's Inherit the Stars is an excellent piece of Science Fiction. It's not long. It's about the investigation of a dead body on the moon. It's the ultimate cold case. The evidence has been cooling for fifty thousand years. I like the scientific tone of the book. It *could* be true. But I also like the back-drop which predicts, in a story published in 1977, laptops, the Internet, silicon chips, and warp drive. I've read it three times now, and every time it delights. It's available at the Baen Free library: Inherit the Stars. I also own a paperback copy. I was working on the nest of wiring behind my desk. I pulled a plug and everything went dark. It was just that room and a few other things like the front hallway. So naturally I checked the breaker. It hadn't tripped. I reset it anyway. No joy. Deciding it might be a bad breaker, I opened up the panel and tested with a multimeter. I got 30 volts across the switch and 100 volts, I forget which was open and which was closed. Heck, the meter (a decent Fluke, for gosh' sake) was reading some tens of volts with the leads not touching anything. So I figured, replace the breaker and see what happens, or at least have Lowe's (my new hangout) test it. Too bad I couldn't get the screw to move. So I put the breaker back in and decided to see if anyone on the internet had insight. (Alright, so the wife was telling me to go check the internet.) Now the room with no power is the study. That means I had no modem nor router. I simply ran an extension cord from another room and plugged in the master surge strip. I have a couple devices hanging off that, and the UPS. The modem hangs off the UPS's surge-only side, and the computers and monitor hang off the un-interruptable side. So I now had enough power for modem and router. If I felt like running two Amperes of current through this extension cord, I could run the computers. Another amp or two and I could have light! I declined. Getting on the wife's wireless computer, I looked for something like "power loss in one room" and found the first hit. That described a situation exactly like mine, and the first response said to check for a GFCI (Ground Fault Circuit Interruptor) in the downed circuit. This is often an outlet panel with a couple of pushbuttons labelled "Test" and "Reset" (that site didn't say this, I already knew what a GFCI panel looks like). That's when a light went off in my head. The breaker panel is in the garage, and the garage lights (and the rest of the house) were on. So I didn't even think of the outlets in the garage wall. The front hallway runs next to the garage, and is part of the downed circuit. The garage wall outlets are not on the garage circuit. They're on -- wait for it -- the hallway circuit. And those outlets include a GFCI. So I went and hit the reset button on it. Still no joy, because -- I had left the breaker off after the previous round. I flipped it, and. Last updated: Feb 08 2010 19:07
|
|