Ruby脚本无法使用mysql2 gem连接到MySQL数据库

时间:2013-09-19 19:41:25

标签: mysql ruby-on-rails ruby gem mysql2

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

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

我已经安装了这些宝石      哪个红宝石      /Users/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 DB?

由于

1 个答案:

答案 0 :(得分:1)

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

尝试重新安装吗?