Ruby version: 1.9.1 Mysql version: 5.0
code written was:
require "rubygems"
require "active_record"
puts "1"
class Source < ActiveRecord::Base
puts "2"
establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "root",
:password => "root@123",
:database => "TrafficTracker"
)
end
puts "3"
traffic_source = Source.find(1)
puts "4"
puts traffic_source.source_name
Above code prints till 3 and breaks at Source.find(1) with error:
1 2 3 C:/Ruby/lib/ruby/1.9.1/mysql.rb:1019:in
read': Packets out of order: 0<> (RuntimeError)
from C:/Ruby/lib/ruby/1.9.1/mysql.rb:444:inread': Packets out of order: 0<> (RuntimeError)
from C:/Ruby/lib/ruby/1.9.1/mysql.rb:444:inread': Packets out of order: 0<> (RuntimeError)
read' from C:/Ruby/lib/ruby/1.9.1/mysql.rb:110:in
from C:/Ruby/lib/ruby/1.9.1/mysql.rb:444:inreal_connect' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:600:in
connect' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:164:ininitialize' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:36:in
new' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:36:inmysql_connection' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:228:in
new_connection' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:incheckout_new_connection' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in
checkout_new_connection' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in
block (2 levels) in c heckout' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco rd/connection_adapters/abstract/connection_pool.rb:186:inloop' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco rd/connection_adapters/abstract/connection_pool.rb:186:in
block in checkout' from C:/Ruby/lib/ruby/1.9.1/monitor.rb:201:inmon_synchronize' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco rd/connection_adapters/abstract/connection_pool.rb:185:in
checkout' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco rd/connection_adapters/abstract/connection_pool.rb:93:inconnection' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco rd/connection_adapters/abstract/connection_pool.rb:316:in
retrieve_connection' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco rd/connection_adapters/abstract/connection_specificati on.rb:97:inretrieve_conn ection' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco rd/connection_adapters/abstract/connection_specification.rb:89:in
connection' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco rd/base.rb:679:incolumns' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/persistence.rb:284:in
attributes_from_column_definition' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/locking/optimistic.rb:62:inattributes_from_column_definition' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/base.rb:1394:in
initialize' from C:/WorkspaceGunjan/TrafficTracker/TrafficTracker/TestProject/RubyPrograms/RubyPrograms/AccessTrafficTracker.rb:22:innew' from C:/WorkspaceGunjan/TrafficTracker/TrafficTracker/TestProject/RubyPrograms/RubyPrograms/AccessTrafficTracker.rb:22:in
' C:\\WorkspaceGunjan\\TrafficTracker\\TrafficTracker\\TestProject\\RubyPrograms\\RubyPrograms> C:\\ WorkspaceGunjan\\TrafficTracker\\TrafficTracker\\TestProject\\RubyPrograms\\RubyPrograms> C:\\WorkspaceGunjan\\TrafficTracker\\TrafficTracker\\TestProject\\RubyPrograms\\RubyPrograms>
after analyzing more, i found problem with mysql.rb in \\Ruby\\lib\\ruby\\1.9.1 Method creating problem is:
def read()
buf = []
len = nil
@sock.sync = false
while len == nil or len == MAX_PACKET_LENGTH do
a = @sock.read(4)
len = a[0]+a[1]*256+a[2]*256*256
pkt_nr = a[3]
if @pkt_nr != pkt_nr then
raise "Packets out of order: #{@pkt_nr}<>#{pkt_nr}" #Exception raised at line number 1019
end
@pkt_nr = @pkt_nr + 1 & 0xff
buf << @sock.read(len)
end
@sock.sync = true
buf.join
end
Finally, after putting all my efforts with MySql and Postgresql, I am able to make successful connection with MySql.
Working set involves:
Ruby Version: 1.87
MySql Version: 5.5
To work with ActiveRecord, one need to have the adapter installed. On command prompt, write:
gem install mysql2.
Code to build successful connection of MySql database with Ruby under the layer of ActiveRecord:
require "rubygems"
require "active_record"
class Source < ActiveRecord::Base
set_table_name "sources"
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:username => "test",
:password => "test",
:port => 3306,
:database => "TestDB",
:socket => "mysql"
)
end
source = Source.find(:first, :conditions => [ "source_id = ?", 2 ])
puts source.source_name
Note: Adapter used is mysql2 and not mysql
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.