我试图将我的ruby脚本连接到位于此处的本地mysql数据库:

  which mysql
  /usr/local/mysql/bin/mysql


我已经安装了这些宝石
     哪个红宝石
     /用户/AM/.rvm/rubies/ruby-2.0.0-p247/bin/ruby

 gem list
 ......
 mysql2 (0.3.13)
 ......


当我运行此脚本时:

 #!/usr/bin/env ruby
 require 'mysql2'

 begin
     client = Mysql2::Client.new(:host => 'localhost', :database => '0828FromSQL', :username => "root", :password => "")

 rescue Mysql2::Error => e
     puts e.errno
     puts e.error
 ensure
     con.close if con
 end


我收到此错误:

 /Users/AM/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require': cannot load such file -- mysql2/mysql2 (LoadError)
 from /Users/AM/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
 from /Users/AM/.rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.13/lib/mysql2.rb:8:in `<top (required)>'
 from /Users/AM/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:116:in `require'
 from /Users/AM/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:116:in `rescue in require'
 from /Users/AM/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:122:in `require'
 from mysqltester.rb:3:in `<main>'


我究竟做错了什么?如何成功连接到MySQL数据库?

谢谢

最佳答案

看来您尚未正确编译该gem的C扩展部分。它需要链接到mysql动态库。

尝试重新安装吗?

关于mysql - Ruby脚本无法使用mysql2 gem连接到MySQL数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18903539/

10-09 20:32