Ruby版本:1.9.1 Mysql版本:5.0
编写的代码是:
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
上面的代码打印到3,并在Source.find(1)中断,错误:
1 2 3 C:/Ruby/lib/ruby/1.9.1/mysql.rb:1019:in
read': Packets out of order: 0<> (RuntimeError)
read' 来自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' 来自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' 来自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' 来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:在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
块(2级)中C heckout” 来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco rd / connection_adapters / abstract / connection_pool.rb:186:在结账时loop' 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
块中' 来自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' 来自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' 来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco rd / connection_adapters / abstract / connection_specification.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' 来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco rd / base.rb:679:在columns' 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'中 来自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' 来自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&GT; C:\ WorkspaceGunjan \ TrafficTracker \ TrafficTracker \ TestProject \ RubyPrograms \ RubyPrograms&GT; C:\ WorkspaceGunjan \ TrafficTracker \ TrafficTracker \ TestProject \ RubyPrograms \ RubyPrograms&GT;
在分析了更多之后,我在\ Ruby \ lib \ ruby \ 1.9.1中发现了mysql.rb的问题 创建问题的方法是:
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
答案 0 :(得分:0)
最后,在使用MySql和Postgresql完成所有工作后,我能够成功连接MySql。
工作集涉及:
Ruby版本:1.87
MySql版本:5.5
要使用ActiveRecord,需要安装适配器。在命令提示符下,写:
gem install mysql2。
在ActiveRecord层下构建MySql数据库与Ruby的成功连接的代码:
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
注意:使用的适配器是mysql2而不是mysql