|
|||||||||||||||||||||||||||||||||||||||||||||||||||
|
Applications: MySQL > tech-resources > articlesproxy-gettingstarted.html (Request Expert MySQL Support)
Skip navigation links
About the author![]() Giuseppe Maxia is a QA developer in MySQL community team. A system analyst with 20 years of IT experience, he has worked as a database consultant and designer for several years. He is a frequent speaker at open source events and he's the author of many articles. He lives in
Sardinia (Italy).
We are always looking for interesting articles
about MySQL! Have you written something and would like to it published
here? Please contact us via feedback form » Learn about new MySQL releases, technical articles, events and more.
Getting started with MySQL ProxyThe launch of MySQL Proxy has caused quite a commotion in the community. And with reason. For feature hungry people, this is undeniably the most exciting addition to MySQL set of tools. Get ready for a wonderful trip to Proxyland. MySQL Proxy overviewmysql-proxy is a lightweight binary application standing between one or more MySQL clients and a server. The clients connect to the proxy with the usual credentials, instead of connecting to the server. The proxy acts as man-in-the-middle between client and server. In its basic form, the proxy is just a redirector. It gets an empty bucket from the client (a query), takes it to the server, fills the bucket with data, and passes it back to the client. If that was all, the proxy would just be useless overhead. There is a little more I haven't told you yet. The proxy ships with an embedded Lua interpreter. Using Lua, you can define what to do with a query or a result set before the proxy passes them along.
Figure 1. MySQL Proxy can modify queries and results. The power of the proxy is all in its flexibility, as allowed by the Lua engine.
In the same way, you can intercept the result set. Thus you can:
Key conceptsMySQL Proxy is built with an object oriented infrastructure. The main class exposes three member functions to the public. You can override them in a Lua script to modify the proxy behavior.
By combining these three back doors to the server you can achieve a high degree of maneuverability over the server. InstallationInstalling the proxy is quite easy. The distribution package contains just one binary (and as of 0.5.1, also some sample lua scripts). You can unpack that and copy it where you like. If your operating system is not included in the distribution, or if you want to try the bleeding edge features as soon as they leave the factory, you may get the source from the public Subversion tree and then build the proxy yourself. Simple query interceptionAs our first example, let's do a "I was there" kind of action, just to give you the feeling that you are standing where you want to be.
If you come back to the previous terminal window, you will see that the proxy has intercepted something for you. The first query was sent on connection by the mysql client. The second one is the one you sent.
Note on usageUntil version 0.5.0, to use a Lua script you need also to use the option --proxy-profiling, or else the read_query and read_query_result functions don't kick in. Query rewritingThe more interesting stuff starts with query rewriting. To demonstrate this feature, let's choose a practical task. We want to catch queries with a common typing error and replace it with the correct keyword. We will look for my most frequent finger twists Here is second_example.lua As before, start the server with the option --proxy-lua-script=second_example.lua Isn't it sweet? I made my usual mistakes, but the proxy was kind enough to fix them for me. The first two queries are stuff the client needs for its purpose. Then came my first mistake, This script is quite crude, but it gives you an idea of the possibilities. Query injectionNext, let's exploit one of the ad hoc features of MySQL Proxy. Query injection.
Figure 2. Query injection. In the image, the server receives three queries, and of course it sends back three result sets. When an injection has taken place, the result set gets processed by another function, read_query_result, where you can deal with the result sets according to their
Figure 3. Processing the injected queries. The result set of the query sent by the client is duly returned. It's transparent for the client, but in between you managed to collect statistical results which are displayed on the proxy console. For a full example, see the query injection tutorial in the Forge. MacrosMacros are just another way of using the query rewriting facility.
Creating result sets - shell commands from MySQLThe proxy receives a request from a client, and then it has to give back a result set. Most of the times, this is straightforward. Passing the query to the server, getting the result set, passing the result set to the client. But what happens if we need to return something that the server is not able to provide? Then we need to build a result set, which is composed of a set of column names, and a bi-dimensional array with the data. Dataset creation basicsFor example, if I wanted to return a warning about a deprecated feature, I could create a result set like this: The above structure, when received by the client, would be shown as That's to say that you can fabricate every result set that meet your needs. For more details, see Jan Kneschke example. Shell commands from MySQL clientAnd now for something completely different, let's see how to use our freshly acquired knowledge to execute shell commands through the proxy. We said already that the proxy behavior can be altered with Lua scripts. And Lua is a complete language, meaning that you can do almost everything with it, including executing shell commands.
Figure 4. Running shell commands through the Proxy. Let's step though it, using the tutorial from MySQL Forge.
The shell tutorial script implements a simple syntax to ask for shell commands: for example,
Make sure that it works as a normal proxy to the database server. Good. The normal operations work as expected. Now we test the enhanced features. Hello shell! This is really a treat for advanced users. Once you have a way of accessing external commands, you can become quite creative. I know that I could check the same with That's not bad. Now that we are content with the status of the server, what about some leisure? However, because the listing is so large, and nobody will remember that anyway, you should paste it into a shell script, and call it, for instance, last_planet.sh. And, here you are!
Shell access, and web contents from MySQL client! wow! A word of cautionHaving shown that you can access the shell from a MySQL connection does not imply automatically that you should always do it. Shell access is a security vulnerability, and if you want to use this feature in your server, do it for internal purposes only. Do not allow shell access to applications open to normal users. That would be asking for trouble (and finding it really fast). You can use the shell to view things, but you could also use it to erase items. Be very careful with shell access! Be aware that the shell access you get through the Proxy is referred to the host where the Proxy is running. If you install the Proxy on the same host, it will coincide with the database server, but don't take it for granted. Customized loggingI left this example for the end because, in my experience, this is the most interesting one and it has a practical, immediate use. Logs on demand are available in MySQL 5.1. But if you are stuck with MySQL 5.0, then the proxy can give you a hand. Simple loggingTo enable logging of queries into something that looks like a general log, the task is easy. Write this small portion of code into a simple_logs.lua file (or download the snippet from MySQL Forge). Then start the proxy with it, and connect to the proxy from some concurrent sessions.
The log contains date, time, connection The pleasant aspect is that you don't need to restart the server to activate the general log. All you need to do is to point your applications to the port 4040 instead of 3306, and you have enabled a simple but functional logging.
Figure 5. Redirecting traffic from port 3306 to 4040. Now you have logging enabled, and you don't have to restart the server or to touch your applications! More customized loggingThe simple and effective logging script from the previous section is tempting, but it's really basic. We have had a glimpse at the Proxy internals, and we have seen that we can get better information, and these logs can be much more interesting than a bare list of queries. We know all the elements to reach this goal. The script will be a bit longer, but not much. In the global part of the script, we check that we're using an appropriate version of the Proxy, since we are using features that are not available in version 0.5.0.
The first function does little work. It appends the query to the proxy queue, so that the next function will be triggered when the result is ready. In this function we can check if we are dealing with a data manipulation query or a select query. If there are rows, the function counts them, and the result is printed in braces to the log file. If there are affected rows, then this is the number that is reported. We also check if there was an error, in which case the information is returned in brackets, and finally all gets written to the log file. Here is an example: The first, second, and fourth line says that the queries returned respectively 5, 2, and 6 rows. The third one says that the query returned an error. The fifth row reports that 2 rows were affected by the Note on the examplesThe examples provided with this article have been tested with a few different operating systems. The code is still in alpha stage, though, so it may happen that data structures, options, and interfaces change, until the feature set is stabilized. What's nextAt the end of this long excursus, I feel that I have barely scratched the surface. MySQL Proxy is this, and much more. There are features that I have not touched, and that should require appropriate coverage, with some benchmarking. Also, I did not get into much detail with the architecture. Somebody will cover that as well. Expect more articles about MySQL Proxy, covering load balancing, replication specific features, benchmarks, and especially a MySQL Proxy cookbook, as soon as the community gathers enough recipes to justify the title. As a last item for this article, I would like to say Cartoon artwork by Richard Duszczak of Cartoon Studio Limited
Diagrams by Patrizio Tassone This article was originally published on O'Reilly network Comments?Read and post comments on this article in the MySQL Forums. There are currently 2 comments. © 1995-2008 MySQL AB. All rights reserved. |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||


