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.

3
  • 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. Commented Jun 15, 2012 at 22:18
  • Let me guess: you're using Mac OS X Leopard (or Lion), right? ) If that's the story, check this article, it should help you. ) Commented Jun 15, 2012 at 22:22
  • I'm on Mountain Lion (10.7). Just tried that fix but unfortunately I am still getting that error :( Commented Jun 15, 2012 at 22:37

2 Answers 2

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!

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

3 Comments

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)'
Solved! Using mysql2, your code and this little tip. Thanks! rorguide.blogspot.co.uk/2011/07/…
Cool! Thanks for sharing that tip. I'm sure it will help someone else too.
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

5 Comments

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 `<main>'
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
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: rubydoc.info/gems/mysql2/0.2.3/frames
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)'
Solved! sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib from: rorguide.blogspot.co.uk/2011/07/…

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.