Failing to connect to oracle database - oracle

I'm trying to write a jruby script that connects to an oracle database using jdbc.
Thusfar I've got:
require 'rubygems'
require 'jdbc_adapter'
require 'active_record'
require 'active_record/version'
ActiveRecord::Base.establish_connection(
:adapter => 'jdbc',
:driver => 'oracle.jdbc.driver.OracleDriver',
:url => 'jdbc:oracle:thin:#mydatabase:1521:mydb',
:user => "user",
:password => "password"
)
ActiveRecord::Base.connection.execute("SELECT * FROM MYTABLE")
The error I'm getting:
C:/Program Files/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-jdbc-adapter-0.9.2/lib/active_record/connection_adapters/jdbc_adapter.rb:326:in `initialize': The driver encountered an error: java.sql.SQLException: invalid arguments in call (RuntimeError)
Suggestions?

The post below implies that you should use :username instead of :user in your connection call:
http://www.ruby-forum.com/topic/143105
as well as the thread of this posting:
http://osdir.com/ml/lang.jruby.user/2007-05/msg00182.html

I think Dougman is correct. My JRuby code is Rails-based, so I can't verify your 'requires', but my database.yml uses "username":
test:
adapter: jdbc
driver: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:#mydatabase:1521:mydb
username: login_name
password: password

Related

bundle exec rake snorby:setup - error rake aborted! invalid hash

While executing below command to setup the snorby on ruby on rails, i get the error: rake aborted! invalid hash. Pls help
#bundle exec rake snorby:setup
#Jammit Warning: Asset compression disabled -- Java unavailable.
No time_zone specified in snorby_config.yml; detected time_zone: Asia/Kolkata
750d6d911891ab576bdbc6b1c4dc5ecf73cb95fd145ab7b16194636094daa3b4ba42b01c05d991c8c174919162e00152a129c645b1e0508850042cf224f165af
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1007 (HY000) at line 1: Can't create database 'snorby'; database exists
[datamapper] Finished auto_upgrade! for :default repository 'snorby'
rake aborted!
invalid hash
Tasks: TOP => db:seed
(See full trace by running task with --trace)
Below are the versions currently used:
#ruby 1.9.3p551 (2014-11-13 revision 48406) [x86_64-linux]
#Bundler version 1.9.3
#Ubuntu 20.04 LTS (focal)
Modifying the entries in seeds.rb file as given below, the error has been resolved. In the seeds.rb changed the line for Default user setup from
User.create(:name => 'Administrator', :email => 'snorby#snorby.org', :password => 'snorby', :password_confirmation => 'snorby', :admin => true) if User.all.blank?
To
User.create(:name => 'Administrator', :email => 'snorby#snorby.org', :encrypted_password => 'snorby', :password_confirmation => 'snorby', :admin => true) if User.all.blank?
i.e the :password was changed to :encrypted_password

Can't connect to local MySQL server through socket. I want to connect to REMOTE db

This is my code.
I want to connect to remote database
require 'bundler/setup'
#require "mysql"
#require 'mysql2'
#require "active_record"
Bundler.require
#db_host = ENV["HOST"]
#db_user = ENV["USER"]
#db_pass = ENV['PASSWORD']
#db_name = "db_name"
require "active_record"
ActiveRecord::Base.establish_connection(
:adapter => 'mysql2',
:database => #db_name,
:username => #db_user,
:password => #db_pass,
:host => #db_host)
class ConversionRate < ActiveRecord::Base
end
class ConversionRateMonthly < ActiveRecord::Base
self.table_name = "conversion_rates_monthly"
end
class KdpReport < ActiveRecord::Base
end
class SalesCalculator
def run
p KdpReport.count
end
end
calculator = SalesCalculator.new
calculator.run
But I get this error:
/home/jonsdirewolf/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.9/lib/mysql2/client.rb:89:in `connect': Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2) (Mysql2::Error)
It is strange but yesterday my code worked. what may be wrong? I want to connect to remote db, not local. And btw I use ruby without rails.
So, the problem is in empty environment var ENV["HOST"].
When host, passed to ActiveRecord is nil or equl to string localhost - AR will try to connect to db using socket.

ssh wrong number of arguments

Referencing the net-ssh API, what's the "best" way to pass connection parameters which come from a yaml?
error:
thufir#dur:~/ruby$
thufir#dur:~/ruby$ ruby ssh.rb
{:host=>"localhost", :user=>"me", :port=>123, :password=>"mypassword"}
/home/thufir/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.6.8/lib/net/ssh.rb:165:in `start': wrong number of arguments (1 for 2..3) (ArgumentError)
from ssh.rb:14:in `<main>'
thufir#dur:~/ruby$
code:
#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'
require 'yaml'
require 'pry'
conn = YAML.load_file('conn.yml')
params = {:host => conn['host'], :user => conn['user'],
:port => conn['port'],:password => conn['password']}
puts params
Net::SSH.start(params) do |ssh|
puts 'hi'
end
yaml:
user: me
password: mypassword
port: 123
host: localhost
is it possible to just pass one monolithic object? Or, do you have to break it up into host, user, et. al.?
The host and user arguments must be specified as positional arguments, not as part of the options hash. Thus:
Net::SSH.start(conn['host'], conn['user'], port: conn['port'], password: conn['password']) do |ssh|
# ...
end
Or you can just pass through all the YAML key-value pairs directly:
Net::SSH.start(conn.delete('host'), conn.delete('user'), conn) do |ssh|
# ...
end

using redis and ruby to implement a tiny short url app

I'm making a short URL app, using Ruby, Sinatra, and Redis. Currently it's under 15 lines:
require 'rubygems'
require 'sinatra'
require 'redis'
require 'uri'
configure do
REDISTOGO_URL = "redis://localhost:6379/"
uri = URI.parse(REDISTOGO_URL)
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
get '/' do
haml :index
end
post '/shorten' do
a = rand(9999)
REDIS.set(a.to_s, params[:long])
"<pre>http://199.19.118.186/get/#{a}</pre>"
#haml :shorten
end
get '/get/:url' do
redirect REDIS.get(params[:url])
end
Where index.haml is a form that POSTs long to /shorten. I've no problem with that.
Right now, however, when I try to use Redis (with the server running, yes), I get this error:
What am I doing wrong?
EDIT: Copy/paste from Emacs... facepalm
EDIT: When trying to access redis alone from ruby (code below), I get this:
/var/lib/gems/1.8/gems/redis-2.2.2/lib/redis/client.rb:47:in `call': ERR unknown command (RuntimeError)
from /var/lib/gems/1.8/gems/redis-2.2.2/lib/redis.rb:841:in `set'
from /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from /var/lib/gems/1.8/gems/redis-2.2.2/lib/redis.rb:840:in `set'
from test_redis.rb:9
With this code:
require 'rubygems'
require 'redis'
require 'uri'
REDISTOGO_URL = "redis://localhost:6379/"
uri = URI.parse(REDISTOGO_URL)
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
REDIS.set("test", "blah")
puts REDIS.get("test")
Ruby being case sensitive, I would try to replace REDIS.SET by REDIS.set and REDIS.GET by REDIS.get. You can find the documentation of the Redis client here:
https://github.com/ezmobius/redis-rb
I have tested your example with ruby 1.8.7. (default on my Linux box).
After installing sinatra, haml, redis and hiredis gems, I have modified the code as follows:
require 'rubygems'
require 'sinatra'
require 'redis'
require 'uri'
configure do
REDISTOGO_URL = "redis://localhost:6379/"
uri = URI.parse(REDISTOGO_URL)
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
get '/' do
"Hello"
haml :index
end
post '/shorten' do
a = rand(9999)
REDIS.set(a.to_s, "http://"+params[:long])
"<pre>http://localhost:4567/get/#{a}</pre>"
end
get '/get/:url' do
redirect REDIS.get(params[:url])
end
I have added the following template in views/index.haml.
!!!
%html
%head
%title My Sinatra Website
%body
%h1 Welcome
%p
Welcome to my website made with Sinatra and HAML
%form{ :action => "/shorten", :method=>"POST" }
%fieldset
%input{ :type =>"text", :name=>"long" }
%input{ :type =>"submit" }
Once Redis is started on port 6379 and sinatra on port 4567, it works like a charm.
I suggest you check your ruby installation and try to access Redis from ruby with a simple non sinatra script.
UPDATE:
The error message is peculiar because normally, when an unknown command is sent to the server, the faulty command is provided:
ERR unknown command 'dummy'
while you just have:
ERR unknown command
Actually, this specific fix was introduced in Redis server more than 2 years ago (in December 2009) - an eternity for Redis.
https://github.com/antirez/redis/commit/2c14807b2dd5c15f1471bec32a7c6dbb077720ee
In other words, you are trying to use a very old (i.e. pre 1-3) version of Redis server with the last version of the Redis client ruby gem, which probably does not support anymore the initial protocol. You may want to compile and install a recent version of Redis server (it is easy), it should work better.

net-ssh and ActiveRecord 3: bringing it all together

I'm working on a small Ruby program that will connect to a remote MySQL Bugzilla database, perform a query of records, and email details of those records to a group on a daily basis.
So far, I've been able to SSH to the db server and execute a command using net-ssh. Here's an example:
require 'net/ssh'
Net::SSH.start("db.example.com", "sroach", :password => "secret") do |ssh|
result = ssh.exec!("ls -l")
puts result
end
That outputs just fine.
Using ActiveRecord 3.0.3, I wanted to test the establish_connection method so I established a connection to my local MySQL database and was able to execute commands using ActiveRecord. Example:
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:database => "list_tool_development",
:username => "my_username",
:password => "secretpassword"
)
class MailingList < ActiveRecord::Base
end
MailingList.first #=> Successfully retrieves first record from the table
So, where I'm having trouble is bringing it all together and applying it to my remote MySQL db. Here's my best try thus far:
require 'net/ssh'
Net::SSH.start("db.example.com", "sroach", :password => "secret") do |ssh|
ssh.forward.local(3307, "127.0.0.1", 3306)
ssh.loop { true }
end
But all that does is make my IRB session hang (which could be completely normal...don't know). Incase that hang was normal, I opened a new IRB session and tried to establish a connection to the remote database like so:
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "127.0.0.1",
:port => 3307,
:reconnect => false,
:database => "bugs",
:pool => 5,
:username => "my_username",
:password => "secret",
:socket => "/tmp/mysql.sock"
)
class Bug < ActiveRecord::Base #=> the table name in the "bugs" db is "bugs"
end #=> so I made the model singular
Bug.first #=> the IRB session hangs at this point
So, I have no idea what's going wrong or how to degub it. Any and all suggestions would be helpful.
I'm on Mac OSX. The db that I'm trying to connect to is on FreeBSD 7.0 and is MySQL version Ver 14.12 Distrib 5.0.67.
Rather than try to tunnel the ActiveRecord connection inside SSH, have you tried connecting directly from ActiveRecord to the DB server? That is the normal way to connect across a network and is directly supported by ActiveRecord.
Replace the host ID with the server host IP or DNS entry, the port can probably be allowed to default to the MySQL driver's default of 3306, and the socket isn't needed since the DB is on the remote host.
If the DB host isn't on the same network as yours, and you're crossing firewalls, you might need to have that port opened to allow the connection. If it is on the same network it should work without needing ssh at all.

Resources