1

I have install the mysql gem but am falling at the first hurdle:

 initialize': wrong number of arguments(4 for 0) (ArgumentError)
 from open.rb:14:in `new' 
 from open.rb:14:in `<main>'

Is the result from this code:

 require 'mysql'

 db = Mysql.new('localhost','root','','test')
 puts db

I was following the code from this tutorial:

http://rubylearning.com/satishtalim/ruby_mysql_tutorial.html

It just looks like the new method is not going to accept 4 arguments. I have no idea why. Mysql.new creates a new object just fine.

Richard Burton
  • 2,230
  • 6
  • 34
  • 49
  • Sorry yeh just removed that #. That wasn't there when I got the error. It just seems the new method is not happy with 4 arguments. – Richard Burton Jun 15 '12 at 22:18
  • Let me guess: you're using Mac OS X Leopard (or Lion), right? ) If that's the story, check [this article](http://thinlight.org/2010/05/12/how-to-install-ruby-mysql-on-mac-os-x-10-6-snow-leopard/), it should help you. ) – raina77ow Jun 15 '12 at 22:22
  • I'm on Mountain Lion (10.7). Just tried that fix but unfortunately I am still getting that error :( – Richard Burton Jun 15 '12 at 22:37

2 Answers2

2

Use the Mysql2 gem:

gem install mysql2

Then:

require 'mysql2'
client = Mysql2::Client.new(:host => "localhost", :username => "root")
results = client.query("show databases")
results.each do |row|
  puts row["Database"]
end

You can find more information in the gem documentation

HTH!

alf
  • 18,372
  • 10
  • 61
  • 92
  • Just been trying mysql2 but now I get: '`require': dlopen(/Users/richardjburton/.rvm/gems/ruby-1.9.3-p0/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)' – Richard Burton Jun 15 '12 at 22:48
  • 1
    Solved! Using mysql2, your code and this little tip. Thanks! http://rorguide.blogspot.co.uk/2011/07/getting-error-library-not-loaded.html – Richard Burton Jun 15 '12 at 22:52
  • Cool! Thanks for sharing that tip. I'm sure it will help someone else too. – alf Jun 15 '12 at 22:55
0

Is line 14 the line where you declare db = Mysql.new('localhost','root','','test')?

I've just opened irb, installed the gem, copied your line and it worked just fine - it returned me a Mysql object as expected.

Can you try do the same via irb? Btw, I recommend you to install the mysql2 gem instead. Here is an explanation about the why: What the difference between mysql and mysql2 gem

Community
  • 1
  • 1
Nicholas Pufal
  • 2,175
  • 20
  • 23
  • 1.9.3-p0 :002 > require 'mysql' => true 1.9.3-p0 :003 > Mysql.new('localhost', 'user', 'pass', 'db') ArgumentError: wrong number of arguments(4 for 0) from (irb):3:in `initialize' from (irb):3:in `new' from (irb):3 from /Users/richardjburton/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `
    '
    – Richard Burton Jun 15 '12 at 22:37
  • Sadly I get the same error in irb. It just won't accept the 4 arguments. Thanks for taking the time to try! What's your setup? I am running OS X Mountain Lion – Richard Burton Jun 15 '12 at 22:38
  • I'm also on Lion. However, my Ruby version is 1.9.2. Can you try to use gem `mysql2` instead? You can find it's docs here: http://rubydoc.info/gems/mysql2/0.2.3/frames – Nicholas Pufal Jun 15 '12 at 22:47
  • Yep I'm now trying the mysql2 gem but getting a new error: `require': dlopen(/Users/richardjburton/.rvm/gems/ruby-1.9.3-p0/gems/mysql2-0.3.11/lib/mysq‌​l2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)' – Richard Burton Jun 15 '12 at 22:51
  • Solved! sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib from: http://rorguide.blogspot.co.uk/2011/07/getting-error-library-not-loaded.html – Richard Burton Jun 15 '12 at 22:52