Pythian has openings for MySQL and MS SQL Server DBAs in each of our offices in Ottawa, Canada; Boston, USA; Dubai, UAE; and Hyderabad, India. If you are a MySQL and/or SQL Server DBA and would like to evaluate this opportunity, please send us your résumé with an introductory paragraph to hr@pythian.com.

Simple MySQL Proxy Failover

By Sheeri Cabral March 27th, 2008 at 3:15 pm
Posted in Group Blog PostsMySQL
Tags:

mysql-proxy defaults to round-robin load balancing. There are fancy tricks around to get mysql-proxy to balance connections based on how many idle connections there are in a proxy-based connection pool.

But there is no code that I found that would simply load balance based on “always go to one server, go to another server only when the first server is down.”

Well, I spent way too long figuring this out today, again running into the problem where the manual hasn’t been updated. I have indeed made a Forge snippet of this code, but it does not hurt to post it here.

This was in fact taken from http://jan.kneschke.de/projects/mysql/mysql-proxy-learns-r-w-splitting, which I referenced above as having the fancy trick above — but there is one very important difference — the internal structure proxy.servers has been changed to proxy.backends. Sadly, the manual page for the Internal Structures has not been updated to reflect this.

At least now there’s a comment on the documentation page that states that.

function connect_server()
  for i = 1, #proxy.backends do
    local s = proxy.backends[i]

    if s.state ~= proxy.BACKEND_STATE_DOWN then
      proxy.connection.backend_ndx = i
--      print ("connecting to " .. i)
      return
    end
  end
end

function read_query(packet)
  for i = 1, #proxy.backends do
    local s = proxy.backends[i]

    if s.state ~= proxy.BACKEND_STATE_DOWN then
      proxy.connection.backend_ndx = i
--      print ("connecting to " .. i)
      return
    end
  end
end
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Google
  • del.icio.us
  • Facebook
  • bodytext
  • Technorati
  • TwitThis
  • Reddit

3 Responses to “Simple MySQL Proxy Failover”

  1. YoungWoo Kim Says:

    Hi Sheeri .
    Thanks for your posting.
    I got a Problem. It does not work mysql-proxy 0.6.1 on RHEL4.
    Which version of mysql-proxy do you use for testing your lua script?

    Thanks.

    yw kim.

  2. Sheeri Cabral Says:

    YoungWoo — I’m using

    mysql-proxy-0.6.0-solaris10-x86_64

    Can you give more details on how it does not work? ie, are you getting a syntax error with Lua, or are you finding that it does not work with failover? If that’s the case, what’s the behavior you’re seeing — are you seeing it do round-robin load balancing? Have you set up 2 backend servers with the –proxy-addresses startup option to mysql-proxy?

  3. YoungWoo Kim Says:

    Sheeri,
    Thanks for your advice.

    So, I tested mysql-proxy 0.6.0.

    As a result, It works fine with 0.6.0. :-)

    In my case, I have two mysql servers with master-master replication scheme.

    I think, mysql-proxy(0.6.1) does not recognize backends status(UP or DOWN).

    Thanks.

    Regards,
    yw kim.

Leave a Reply

Filling out the following captcha not only allows us to cut down on automated blogspam but also helps digitize books. Please feel free to send comments on this approach directly to Paul at vallee@pythian.com.

NOTE: After submitting your comment, verify that it is added to the blog. New comments will be marked as "waiting for moderation" (we only moderate for spam). If the level of spam is as low as we hope, we will bypass this step.