Ruby DBI MySQL: how to specify protocol=tcp

Discussion in 'Ruby' started by Kyle, Apr 14, 2008.

  1. Kyle

    Kyle Guest

    Hello! I'm using a ruby script to synchronize a remote (web) MySQL
    database with a local database. I had been remotely accessing the
    database through a non-standard port, but I would much rather use an
    SSH tunnel for security. I can set up the tunnel to point port 3307
    on the local machine to 3306 (MySQL) on the remote host. When using
    mysql from the command line, I'm able to connect if I specify --
    protocol=tcp. Otherwise, MySQL tries to use a socket, and it fails.

    Using Ruby DBI::MySQL, I can't seem to specify --protocol=tcp. I've
    put this in both /etc/mysql/my.cnf and ~/.my.cnf so that I no longer
    have to explicitly state it from the command line. Still, my script
    is unable to connect. Here is the connection setup in my code:

    mysql = DBI.connect('dbi:Mysql:database=dbname;port=3307', 'user',

    Here's the error:

    /usr/lib/ruby/1.8/DBD/Mysql/Mysql.rb:115:in `error': Can't connect to
    local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    from /usr/lib/ruby/1.8/DBD/Mysql/Mysql.rb:171:in `connect'
    from /usr/lib/ruby/1.8/dbi.rb:424:in `connect'
    from /usr/lib/ruby/1.8/dbi.rb:215:in `connect'
    from slice-inventory-sync.rb:9

    (Note: specifying host=localhost does the same and host=remotehost

    Any ideas on how I can get --protocol=tcp specified in my script?


    Kyle, Apr 14, 2008
    1. Advertisements

  2. Roger Pack

    Roger Pack Guest

    Roger Pack, Apr 15, 2008
    1. Advertisements

  3. Kyle

    Kyle Guest


    Thanks for the tips! I had tried specifying host=localhost, but I
    hadn't tried host= It works! Bizarre...

    Thanks for the link, too. It's definitely more robust than what I
    have, but I'm happy with my solution for now. If I build another sync
    script some time, I'll take some good ideas from that program.


    On Apr 15, 12:15 am, Roger Pack <> wrote:
    > > mysql = DBI.connect('dbi:Mysql:database=dbname;port=3307', 'user',
    > > 'pass')

    > yeah try specifying the host as either '' or 'localhost' -- one
    > of the two ignores the sock.
    > You can also take a look at
    > for some nice references on how to build a solid version of what you're
    > building.
    > :)
    > --
    > Posted via
    Kyle, Apr 15, 2008
  4. Roger Pack

    Roger Pack Guest

    Kyle wrote:
    > Roger,
    > Thanks for the tips! I had tried specifying host=localhost, but I
    > hadn't tried host= It works! Bizarre...

    yeah it's a mysql thing. I have no idea why they did that.

    The one I was most happy to discover was how to use compression. That
    rocked :)
    Posted via
    Roger Pack, Apr 15, 2008
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeffrey Spoon

    Custom Protocol over TCP

    Jeffrey Spoon, Dec 31, 2004, in forum: Java
    Jeffrey Spoon
    Jan 1, 2005
  2. Michael campbell
    Michael campbell
    Oct 22, 2003
  3. Paul Vudmaska

    dbi:mysql mysql has gone away

    Paul Vudmaska, Apr 21, 2004, in forum: Ruby
    Paul Vudmaska
    Apr 21, 2004
  4. Jerome Hauss
    Jerome Hauss
    Oct 13, 2004
  5. Grant Edwards
    Antoine Pitrou
    Apr 10, 2014

Share This Page