One of the most frequently needed functionality in the MySQL Proxy is the need to know which server you are on. This is not given, on purpose, by the proxy, because the proxy is supposed to be transparent. It is not supposed to matter which back-end server you are on.
However, for testing purposes we often want to know which back-end server we’re on. Thus I developed functionality for
SHOW PROXY BACKEND [INDEX ADDRESS OTHER].
SHOW PROXY BACKEND INDEX — gives the index of the server you’re on (backend_ndx, ie 1)
SHOW PROXY BACKEND ADDRESS — gives the address of the server you’re on (ie, foo.bar.com:3306)
SHOW PROXY BACKEND OTHER — gives the address of all the other servers except those you’re not on, in multiline format.
Note that I was pretty lazy and the commands are case-sensitive. But I figured that since this is supposed to be used mostly in testing circumstances, it did not really matter.
The code is on the MySQL Forge Wiki at http://forge.mysql.com/tools/tool.php?id=139
Interestingly enough, this script is actually being used in production — a site has a primary and failover server, and wants to check that when the primary server is in use, there are no connections on the failover. I wrote that check as well, but as the logic is somewhat particular, I am not sure it would be useful to many. The logic is:
SHOW PROXY BACKEND INDEX.
- if it fails, I can’t connect to the proxy, log an error, exit.
- if it !=1, I’m on the failover server, log a warning, exit.
- if it =1, I’m on the primary server.
SHOW PROXY BACKEND OTHER.
- if it is empty, either I can’t connect to the proxy or there are no failovers defined, log a warning, exit
- For each
OTHERaddress, connect to them and find what that connection’s host looks like (sometimes it looks like
foo.bar.com:4325, other times it looks like
126.96.36.199:4573). Get the value of “my host” by stripping off the port.
- Connect to the
OTHERaddress again, killing off connections from “my host” except for the “system user” and a few other special accounts (replication slave being one of them). Log each kill with the thread number and a warning.
- If there are no connections to be killed, log OK.
Let me know if you’d like to see that…it’s a shell script, and it requires the mysql client and bintools like