Friday, April 10, 2009

Reading files using the MySQL Proxy

After I wrote about a new feature on MySQL Proxy that helps you read files, I thought that showing an example could come handy.

You can find the complete file on the MySQL Forge and once I have a test case for this script, it will be available on Launchpad.

Explaining the code.
You can see at the top I have
local proto = assert(require("mysql.proto"))
This is important, as it makes the from_masterinfo_string() function available for use.

I also included a function called get_command(), which is a modification of code found here.
It basically does a simple parsing of the statement you send through the proxy and returns two variables.

Here, I look for the query "read masterinfo;" and convert it into
I do this so that I could get the location of the file. You can just hardcode it, but that's not fun :)

Here, I read the value for datadir, and use it to load the content of into a lua variable, convert the content into a mysql result set and send it back to the client.

How to use.
You need to connect to a mysql proxy that is in front of a slave mysql server; (and tell the proxy to use this lua script)(*). Then execute

mysql> read masterinfo;

And you would get something like this:

mysql> read masterinfo;
| Variable_name | Value |
| master_user | slave |
| master_ssl | 0 |
| master_port | 43309 |
| master_log_file | dv1-bin.000005 |
| master_connect_retry | 60 |
| master_password | slave |
| master_log_pos | 272 |
| master_host | |
8 rows in set (0.00 sec)

And you are done.

(*)You can find more information about the MySQL Proxy on the MySQL Forge site.

No comments:

Post a Comment

Vote on Planet MySQL