Using MySQL Proxy’s Configuration File
I write this tutorial, simply because it is undocumented.
MySQL Proxy has only long options. Therefore, it is a little bit inconvenient to type the long command line. For example, suppose we are going to setup a MySQL Proxy server forwarding connections to two MySQL backends. We also want to 1) use a Lua script, 2) start the proxy in daemon mode, 3) log all messages of level debug and higher to a specific file, and 4) write the proxy’s pid to its pid file. What would the command line look like?
$ mysql-proxy --proxy-address=192.168.0.189:3307 \ --proxy-backend-addresses=192.168.0.189:3306 \ --proxy-backend-addresses=192.168.0.192:3306 \ --proxy-lua-script=/home/josh/mysql-proxy/dispatch-by-client-address.lua \ --daemon \ --log-file=/home/josh/mysql-proxy/mysql-proxy.log --log-level=debug \ --pid-file=/home/josh/mysql-proxy/mysql-proxy.pid
As you can see, it’s quite long. Not to mention, MySQL Proxy can be plugged into many plugins, which could import more options.
Is there any way to make it less repetitive? Yes. By taking advantage of MySQL Proxy’s configuration file, we no longer need to type such long command line.
MySQL Proxy uses GLib’s key file (GKeyFile) as its configuration file format. A key file is very similar to a .ini file. It consists of groups of key value pairs which can be strings, booleans, integers and lists of these. Each key value pair must be contained in a group, where the name appears between enclosed square brackets.
Here goes the proxy’s configuration file of the example above:
# MySQL Proxy's configuration file (mysql-proxy.cnf) [mysql-proxy] daemon = true pid-file = /home/josh/mysql-proxy/mysql-proxy.pid log-file = /home/josh/mysql-proxy/mysql-proxy.log log-level = debug proxy-address = 192.168.0.189:3307 proxy-backend-addresses = 192.168.0.192:3306,192.168.0.189:3306 proxy-lua-script = /home/josh/mysql-proxy/dispatch-by-client-address.lua
And the command line:
$ mysql-proxy --defaults-file=mysql-proxy.cnf
It is apparently much shorter. We now just need to specify the name of the configuration file!
Some notes:
1) GLib uses the ‘;’ character as the default list separator, while MySQL Proxy uses ‘,’.
2) Ungrouped keys are not allowed in key files.
3) The –version and –defaults-file options can not appear in MySQL Proxy’s configuration file.
4) A handy feature: the values in MySQL Proxy’s configuration file can be overridden by the command line options.
To learn more about GKeyFile, please visit:
http://www.gtkbook.com/tutorial.php?page=keyfile
http://library.gnome.org/devel/glib/2.18/glib-Key-value-file-parser.html
http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.0.html
yufeng said,
February 20, 2009 @ 11:43 am
写的真清晰 建议收入mysql-proxy INSTALL 文档中去哦
Erwan Ben Souiden said,
June 8, 2009 @ 8:16 pm
Thank you for this howto, I like this approach !
tom3k said,
March 31, 2010 @ 1:22 pm
excellent!
for those that care,
this is a great way to overcome mysqld’s ‘one ip, or all’ bind-address limitation.
Ramesh said,
August 31, 2011 @ 5:02 pm
Really good post
Abigail22Reed said,
November 7, 2011 @ 3:15 am
One understands that life is expensive, but different people need cash for various issues and not every man gets enough money. Thence to receive some home loans and just term loan would be good solution.
nike free shoes said,
November 10, 2011 @ 9:31 am
nike free shoes
nike free run+