4 Replies - 934 Views - Last Post: 11 April 2013 - 03:44 PM Rate Topic: -----

#1 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Connect to remote MySQL -- Am I using the gem properly?

Posted 10 April 2013 - 03:47 AM

This is for Ruby without rails.

I am trying to connect to a database on a remote machine. I've installed the mysql2 gem and do somehting like this to connect:

client = Mysql2::Client.new(:host => "localhost", :username => "cfoley")


I have no control over the machine. I've been told that to connect I need to use an SSH tunnel which is why the host is localhost. Anyway, I'm relatively sure the problem is with my installation.

I get this:

NameError: uninitialized constant Mysql2
        from (irb):1
        from C:/Ruby193/bin/irb:12:in `<main>'


So, I wonder if I have to require the gem:

irb(main):001:0> require "mysql2"
LoadError: 126: The specified module could not be found.   - C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/1.9/mysql2.so
        from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/mysql2.rb:2:in `<top (required)>'
        from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2.rb:9:in `<top (required)>'
        from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
        from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
        from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
        from (irb):1
        from C:/Ruby193/bin/irb:12:in `<main>'


I've checked that the mysql2.so file is at that path so I'm at a loss over what to do. A couple of thoughts:
The mysql2 gem installation says this:

Quote

You may have to specify --with-mysql-config=/some/random/path/bin/mysql_config


but MySQL is on a remote machine.

Also this says to copy a dll to a Ruby directory. Again, I can't because it is on another machine.
http://stackoverflow...ws-rubymine-ide

If anyone could help that would be fantastic. I'm not limited to mysql2 so if a different gem might work better that's fine too. :-)

Is This A Good Question/Topic? 0
  • +

Replies To: Connect to remote MySQL -- Am I using the gem properly?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: Connect to remote MySQL -- Am I using the gem properly?

Posted 10 April 2013 - 04:39 AM

The MySQL2 is a wrapper around libmysqlclient. You need to have that installed in order to successfully build and use the gem. Just because the server is running on a different machine, that doesn't mean that you don't have to have a MySQL client installed in order to connect to it.
Was This Post Helpful? 1
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: Connect to remote MySQL -- Am I using the gem properly?

Posted 10 April 2013 - 05:30 AM

Thanks. It's working perfectly now!
Was This Post Helpful? 0
  • +
  • -

#4 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon



Reputation: 1335
  • View blog
  • Posts: 3,398
  • Joined: 28-November 09

Re: Connect to remote MySQL -- Am I using the gem properly?

Posted 11 April 2013 - 03:31 PM

You might also look into Sequel, it's a great gem for sql-agnostic operations: http://sequel.rubyforge.org/
Was This Post Helpful? 1
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: Connect to remote MySQL -- Am I using the gem properly?

Posted 11 April 2013 - 03:44 PM

That does look pretty tasty. I'll keep it in mind for future projects!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1