0

Im trying to connect my ruby script to the local mysql database located here:

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

Ive got these gems installed which ruby /Users/AM/.rvm/rubies/ruby-2.0.0-p247/bin/ruby

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

When I run this script:

 #!/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

I get this error:

 /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>'

WHat am I doing wrong? How can I connect to the MySQL DB successfully?

Thanks

banditKing
  • 9,405
  • 28
  • 100
  • 157

1 Answers1

1

It looks like you haven't compiled the C extension part of that gem properly. It needs to link to the mysql dynamic library.

Try reinstalling it?

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    I am accepting your answer because your keyword "dynamic" tipped me off to the solution: which is adding this line in the .bash_profile: <> After that I restarted terminal and reinstalled the mysql2 gem and everything worked – banditKing Sep 19 '13 at 20:24
  • Ah, so it was compiled but the MySQL library wasn't in the proper loader path. Good catch finding that. – tadman Sep 19 '13 at 20:51