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

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

I am accepting your answer because your keyword "dynamic" tipped me off to the solution: which is adding this line in the .bash_profile: <<export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH>> After that I restarted terminal and reinstalled the mysql2 gem and everything worked
Ah, so it was compiled but the MySQL library wasn't in the proper loader path. Good catch finding that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.