Mysql Proxy en
Mysql Proxy en
Abstract
The MySQL Proxy is an application that communicates over the network using the MySQL client/server protocol and
provides communication between one or more MySQL servers and one or more MySQL clients. Because MySQL
Proxy uses the MySQL client/server protocol, it can be used without modification with any MySQL-compatible client
that uses the protocol. This includes the mysql command-line client, any clients that uses the MySQL client libraries,
and any connector that supports the MySQL network protocol.
For notes detailing the changes in each release of MySQL Proxy, see MySQL Proxy Release Notes.
For help with using MySQL, please visit either the MySQL Forums or MySQL Mailing Lists, where you can discuss
your issues with other MySQL users.
For additional documentation on MySQL products, including translations of the documentation into other languages,
and downloadable versions in variety of formats, including HTML and PDF formats, see the MySQL Documentation
Library.
Licensing information. This product may include third-party software, used under license. See this document
for licensing information, including licensing information relating to third-party software that may be included in this
release.
iii
iv
Preface and Legal Notices
This is the MySQL Proxy manual. This document covers MySQL Proxy 0.8.5.
Legal Notices
Copyright © 2012, 2016, Oracle and/or its affiliates. All rights reserved.
This software and related documentation are provided under a license agreement containing restrictions
on use and disclosure and are protected by intellectual property laws. Except as expressly permitted
in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast,
modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any
means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for
interoperability, is prohibited.
The information contained herein is subject to change without notice and is not warranted to be error-free.
If you find any errors, please report them to us in writing.
If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it
on behalf of the U.S. Government, then the following notice is applicable:
U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software,
any programs installed on the hardware, and/or documentation, delivered to U.S. Government end users
are "commercial computer software" pursuant to the applicable Federal Acquisition Regulation and agency-
specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the
programs, including any operating system, integrated software, any programs installed on the hardware,
and/or documentation, shall be subject to license terms and license restrictions applicable to the programs.
No other rights are granted to the U.S. Government.
This software or hardware is developed for general use in a variety of information management
applications. It is not developed or intended for use in any inherently dangerous applications, including
applications that may create a risk of personal injury. If you use this software or hardware in dangerous
applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other
measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages
caused by use of this software or hardware in dangerous applications.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks
of their respective owners.
Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks
are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD,
Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced
Micro Devices. UNIX is a registered trademark of The Open Group.
This software or hardware and documentation may provide access to or information about content,
products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and
expressly disclaim all warranties of any kind with respect to third-party content, products, and services
unless otherwise set forth in an applicable agreement between you and Oracle. Oracle Corporation and its
affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of
third-party content, products, or services, except as set forth in an applicable agreement between you and
Oracle.
Documentation Accessibility
For information about Oracle's commitment to accessibility, visit the Oracle Accessibility Program website
at
v
Legal Notices
http://www.oracle.com/pls/topic/lookup?ctx=acc&id=docacc.
Oracle customers that have purchased support have access to electronic support through My Oracle
Support. For information, visit
http://www.oracle.com/pls/topic/lookup?ctx=acc&id=info or visit http://www.oracle.com/pls/topic/lookup?
ctx=acc&id=trs if you are hearing impaired.
This documentation is NOT distributed under a GPL license. Use of this documentation is subject to the
following terms:
You may create a printed copy of this documentation solely for your own personal use. Conversion to other
formats is allowed as long as the actual content is not altered or edited in any way. You shall not publish
or distribute this documentation in any form or on any media, except if you distribute the documentation in
a manner similar to how Oracle disseminates it (that is, electronically for download on a Web site with the
software) or on a CD-ROM or similar medium, provided however that the documentation is disseminated
together with the software on the same medium. Any other use, such as any dissemination of printed
copies or use of this documentation, in whole or in part, in another publication, requires the prior written
consent from an authorized representative of Oracle. Oracle and/or its affiliates reserve any and all rights
to this documentation not expressly granted above.
vi
Chapter 1 Introduction to MySQL Proxy
The MySQL Proxy is an application that communicates over the network using the MySQL client/server
protocol and provides communication between one or more MySQL servers and one or more MySQL
clients. Because MySQL Proxy uses the MySQL network protocol, it can be used without modification with
any MySQL-compatible client that uses the protocol. This includes the mysql command-line client, any
clients that uses the MySQL client libraries, and any connector that supports the MySQL network protocol.
In the most basic configuration, MySQL Proxy simply interposes itself between the server and clients,
passing queries from the clients to the MySQL Server and returning the responses from the MySQL Server
to the appropriate client. In more advanced configurations, the MySQL Proxy can also monitor and alter
the communication between the client and the server. Query interception enables you to add profiling, and
interception of the exchanges is scriptable using the Lua scripting language.
By intercepting the queries from the client, the proxy can insert additional queries into the list of queries
sent to the server, and remove the additional results when they are returned by the server. Using this
functionality you can return the results from the original query to the client while adding informational
statements to each query, for example, to monitor their execution time or progress, and separately log the
results.
The proxy enables you to perform additional monitoring, filtering, or manipulation of queries without
requiring you to make any modifications to the client and without the client even being aware that it is
communicating with anything but a genuine MySQL server.
Warning
MySQL Proxy is currently an Alpha release and should not be used within
production environments.
Important
MySQL Proxy is compatible with MySQL 5.0 or later. Testing has not been
performed with Version 4.1. Please provide feedback on your experiences using the
MySQL Proxy Forum.
For notes detailing the changes in each release of MySQL Proxy, see MySQL Proxy Release Notes.
1
2
Chapter 2 MySQL Proxy Supported Platforms
MySQL Proxy is currently available as a precompiled binary for the following platforms:
• OS X
• FreeBSD
• IBM AIX
• Sun Solaris
• Microsoft Windows (including Microsoft Windows XP, Microsoft Windows Vista, Microsoft Windows
Server 2003, Microsoft Windows Server 2008)
Other Unix/Linux platforms not listed should be compatible by using the source package and building
MySQL Proxy locally.
System requirements for the MySQL Proxy application are the same as the main MySQL server. Currently
MySQL Proxy is compatible only with MySQL 5.0.1 and later. MySQL Proxy is provided as a standalone
binary. It is not necessary to have MySQL or Lua installed.
3
4
Chapter 3 Installing MySQL Proxy
Table of Contents
3.1 Installing MySQL Proxy from a Binary Distribution .......................................................................... 5
3.2 Installing MySQL Proxy from a Source Distribution ........................................................................ 6
3.3 Installing MySQL Proxy from the Bazaar Repository ...................................................................... 6
3.4 Setting Up MySQL Proxy as a Windows Service ........................................................................... 7
• Precompiled binaries are available for a number of different platforms. See Section 3.1, “Installing
MySQL Proxy from a Binary Distribution”.
• You can install from the source code to build on an environment not supported by the binary
distributions. See Section 3.2, “Installing MySQL Proxy from a Source Distribution”.
• The latest version of the MySQL Proxy source code is available through a development repository. This
is the best way to stay up to date with the latest fixes and revisions. See Section 3.3, “Installing MySQL
Proxy from the Bazaar Repository”.
To install, unpack the archive into the desired directory, then modify your PATH environment variable so
that you can use the mysql-proxy command directly:
shell> cd /usr/local
shell> tar zxf mysql-proxy-0.8.5-platform.tar.gz
shell> PATH=$PATH:/usr/local/mysql-proxy-0.8.5-platform/bin
To update the path globally on a system, you might need administrator privileges to modify the appropriate
/etc/profile, /etc/bashrc, or other system configuration file.
On Windows, you can update the PATH environment variable using this procedure:
1. On the Windows desktop, right-click the My Computer icon, and select Properties.
2. Next select the Advanced tab from the System Properties menu that appears, and click the
Environment Variables button.
3. Under System Variables, select Path, then click the Edit button. The Edit System Variable dialogue
should appear.
The Microsoft Visual C++ runtime libraries are a requirement for running MySQL Proxy as of version 0.8.2.
Users that do not have these libraries must download and install the Microsoft Visual C++ 2008 Service
Pack 1 Redistributable Package MFC Security Update. Use the following link to obtain the package:
Note
This is not required on Windows 8 or higher as the Visual C++ runtime libraries are
bundled with that version of Windows.
5
Installing MySQL Proxy from a Source Distribution
http://www.microsoft.com/download/en/details.aspx?id=26368
• pkg-config.
• automake 1.10 or higher, up to 1.12.x. It is not possible to use version 1.13 or higher.
• flex 2.5.37.
• gtk-doc 1.18.
Note
On some operating systems, you might need to manually build the required
components to get the latest version. If you have trouble compiling MySQL Proxy,
consider using a binary distributions instead.
After verifying that the prerequisite components are installed, configure and build MySQL Proxy:
6
Setting Up MySQL Proxy as a Windows Service
A build from the Bazaar repository requires that the following prerequisite components be installed:
• pkg-config.
• flex 2.5.37.
• gtk-doc 1.18.
The mysql-proxy source is hosted on Launchpad. To check out a local copy of the Bazaar repository,
use bzr:
The preceding command downloads a complete version of the Bazaar repository for mysql-proxy. The
main source files are located within the trunk subdirectory. The configuration scripts must be generated
before you can configure and build mysql-proxy. The autogen.sh script generates the required
configuration scripts for you:
shell> sh ./autogen.sh
The autogen.sh script creates the standard configure script, which you then use to configure and build
with make:
shell> ./configure
shell> make
shell> make install
To create a standalone source distribution, identical to the source distribution available for download, use
this command:
The preceding command creates the file mysql-proxy-0.8.5.tar.gz (with the corresponding current
version) within the current directory.
7
Setting Up MySQL Proxy as a Windows Service
the service, including automatically starting and stopping it during boot, reboot and shutdown, without
separately running the MySQL Proxy application.
To set up a MySQL Proxy service, use the sc command to create a new service using the MySQL Proxy
service command. Specify the MySQL Proxy options on the sc command line, and identify the service with
a unique name. For example, to configure a new MySQL Proxy instance that will automatically start when
your system boots, redirecting queries to the local MySQL server:
Note
The space following the equal sign after each property is required; failure to include
it results in an error.
The preceding command creates a new service called Proxy. You can start and stop the service using
the net start|stop command with the service name. The service is not automatically started after it is
created. To start the service:
You can specify additional command-line options to the sc command. You can also set up multiple MySQL
Proxy services on the same machine (providing they are configured to listen on different ports and/or IP
addresses.
For more information on creating services using sc, see How to create a Windows service by using
Sc.exe.
8
Chapter 4 MySQL Proxy Command Options
To start MySQL Proxy, you can run it directly from the command line:
shell> mysql-proxy
For most situations, you specify at least the host name or address and the port number of the backend
MySQL server to which the MySQL Proxy should pass queries.
You can specify options to mysql-proxy either on the command line, or by using a configuration file and
the --defaults-file command-line option to specify the file location.
[mysql-proxy]
admin-address = host:port
• Specify all configuration options in the form of a configuration name and the value to set.
• For options that are a simple toggle on the command line (for example, --proxy-skip-profiling),
use true or false. For example, the following is invalid:
[mysql-proxy]
proxy-skip-profiling
[mysql-proxy]
proxy-skip-profiling = true
• Give the configuration file Unix permissions of 0660 (readable and writable by user and group, no
access for others).
Failure to adhere to any of these requirements causes mysql-proxy to generate an error during startup.
The following tables list the supported configuration file and command-line options.
Format Description
--help Show help options
--help-admin Show admin module options
--help-all Show all help options
--help-proxy Show proxy module options
Format Description
--admin-address The admin module listening host and port
--admin-lua-script Script to execute by the admin module
9
Format Description
--admin-password Authentication password for admin module
--admin-username Authentication user name for admin module
--proxy-address The listening proxy server host and port
Format Description
--basedir The base directory prefix for paths in the configuration
--daemon Start in daemon mode
--defaults-file Read only named option file
--event-threads Number of event-handling threads
--keepalive Try to restart the proxy if a crash occurs
--log-backtrace-on-crash Try to invoke the debugger and generate a backtrace on crash
--log-file The file where error messages are logged
--log-level The logging level
--log-use-syslog Log errors to syslog
--lua-cpath Set the LUA_CPATH
--lua-path Set the LUA_PATH
--max-open-files The maximum number of open files to support
--pid-file File in which to store process ID
--plugin-dir Directory containing plugin files
--plugins List of plugins to load
--user The user to use when running mysql-proxy
--version Show version information
Except as noted in the following details, all of the options can be used within the configuration file by
supplying the option and the corresponding value. For example:
10
[mysql-proxy]
log-file = /var/log/mysql-proxy.log
log-level = message
• --help, -h
• --help-admin
• --help-all
• --help-proxy
• --admin-address=host:port
The host name (or IP address) and port for the administration port. The default is localhost:4041.
• --admin-lua-script=file_name
• --admin-password=password
The password to use to authenticate users wanting to connect to the MySQL Proxy administration
module. This module uses the MySQL protocol to request a user name and password for connections.
• --admin-username=user_name
11
Command-Line Format --admin-username=user_name
Permitted Values Type string
Default root
The user name to use to authenticate users wanting to connect to the MySQL Proxy administration
module. This module uses the MySQL protocol to request a user name and password for connections.
The default user name is root.
• --basedir=dir_name
The base directory to use as a prefix for all other file name configuration options. The base name should
be an absolute (not relative) directory. If you specify a relative directory, mysql-proxy generates an
error during startup.
• --daemon
• --defaults-file=file_name
The file to read for configuration options. If not specified, MySQL Proxy takes options only from the
command line.
• --event-threads=count
• --keepalive
Create a process surrounding the main mysql-proxy process that attempts to restart the main mysql-
proxy process in the event of a crash or other failure.
Note
• --log-backtrace-on-crash
12
Command-Line Format --log-backtrace-on-crash
Log a backtrace to the error log and try to initialize the debugger in the event of a failure.
• --log-file=file_name
The file to use to record log information. If this option is not given, mysql-proxy logs to the standard
error output.
• --log-level=level
warning
info
message
debug
The log level to use when outputting error messages. Messages with that level (or lower) are output. For
example, message level also outputs message with info, warning, and error levels.
• --log-use-syslog
• --lua-cpath=dir_name
The LUA_CPATH to use when loading compiled modules or libraries for Lua scripts.
• --lua-path=dir_name
• --max-open-files=count
13
Permitted Values Type numeric
The maximum number of open files and sockets supported by the mysql-proxy process. Certain
scripts might require a higher value.
• --no-proxy
• --plugin-dir=dir_name
• --plugins=plugin
Loads a plugin.
When using this option on the command line, you can specify the option multiple times to specify
multiple plugins. For example:
When using the option within the configuration file, you should separate multiple plugins by commas.
The equivalent of the preceding example would be:
...
plugins=proxy,admin
• --proxy-address=host:port, -P host:port
The listening host name (or IP address) and port of the proxy server. The default is :4040 (all IPs on
port 4040).
• --proxy-read-only-backend-addresses=host:port, -r host:port
Setting this value only configures the servers within the corresponding internal
structure (see proxy.global.backends). You can determine the backend type
by checking the type field for each connection.
You should therefore only use this option in combination with a script designed to
make use of the different backend types.
When using this option on the command line, you can specify the option and the server multiple times to
specify multiple backends. For example:
When using the option within the configuration file, you should separate multiple servers by commas.
The equivalent of the preceding example would be:
...
proxy-read-only-backend-addresses = 192.168.0.1:3306,192.168.0.2:3306
• --proxy-backend-addresses=host:port, -b host:port
The host name (or IP address) and port of the MySQL server to connect to. You can specify multiple
backend servers by supplying multiple options. Clients are connected to each backend server in round-
robin fashion. For example, if you specify two servers A and B, the first client connection will go to server
A; the second client connection to server B and the third client connection to server A.
When using this option on the command line, you can specify the option and the server multiple times to
specify multiple backends. For example:
When using the option within the configuration file, you should separate multiple servers by commas.
The equivalent of the preceding example would be:
...
proxy-backend-addresses = 192.168.0.1:3306,192.168.0.2:3306
• --proxy-pool-no-change-user
Disable use of the MySQL protocol CHANGE_USER command when reusing a connection from the pool of
connections specified by the proxy-backend-addresses list.
• --proxy-skip-profiling
Command-Line Format 15
--proxy-skip-profiling
Disable query profiling (statistics time tracking). The default is for tracking to be enabled.
• --proxy-fix-bug-25371
Removed 0.8.1
Command-Line Format --proxy-fix-bug-25371
Enable a workaround for an issue when connecting to a MySQL server later than 5.1.12 when using a
MySQL client library of any earlier version.
This option was removed in mysql-proxy 0.8.1. Now, mysql-proxy returns an error message at the
protocol level if it sees a COM_CHANGE_USER being sent to a server that has a version from 5.1.14 to
5.1.17.
• --proxy-lua-script=file_name, -s file_name
The Lua script file to be loaded. Note that the script file is not physically loaded and parsed until a
connection is made. Also note that the specified Lua script is reloaded for each connection; if the content
of the Lua script changes while mysql-proxy is running, the updated content is automatically used
when a new connection is made.
• --pid-file=file_name
• --user=user_name
• --version, -V
The most common usage is as a simple proxy service (that is, without additional scripting). For basic proxy
operation, you must specify at least one proxy-backend-addresses option to specify the MySQL
server to connect to by default:
The default proxy port is 4040, so you can connect to your MySQL server through the proxy by specifying
the host name and port details: 16
shell> mysql --host=localhost --port=4040
If your server requires authentication information, this will be passed through natively without alteration by
mysql-proxy, so you must also specify the required authentication information:
You can also connect to a read-only port (which filters out UPDATE and INSERT queries) by connecting to
the read-only port. By default the host name is the default, and the port is 4042, but you can alter the host/
port information by using the --proxy-read-only-backend-addresses command-line option.
For more detailed information on how to use these command-line options, and mysql-proxy in general in
combination with Lua scripts, see Chapter 6, Using MySQL Proxy.
17
18
Chapter 5 MySQL Proxy Scripting
Table of Contents
5.1 Proxy Scripting Sequence During Query Injection ........................................................................ 21
5.2 Internal Structures ...................................................................................................................... 23
5.3 Capturing a Connection with connect_server() ...................................................................... 30
5.4 Examining the Handshake with read_handshake() ................................................................... 30
5.5 Examining the Authentication Credentials with read_auth() ...................................................... 31
5.6 Accessing Authentication Information with read_auth_result() ............................................... 31
5.7 Manipulating Queries with read_query() .................................................................................. 32
5.8 Manipulating Results with read_query_result() ..................................................................... 33
You can control how MySQL Proxy manipulates and works with the queries and results that are passed
on to the MySQL server through the use of the embedded Lua scripting language. You can find out more
about the Lua programming language from the Lua Web site.
The following diagram shows an overview of the classes exposed by MySQL Proxy.
19
20
Proxy Scripting Sequence During Query Injection
The primary interaction between MySQL Proxy and the server is provided by defining one or more
functions through an Lua script. A number of functions are supported, according to different events and
operations in the communication sequence between a client and one or more backend MySQL servers:
• connect_server(): This function is called each time a connection is made to MySQL Proxy from a
client. You can use this function during load-balancing to intercept the original connection and decide
which server the client should ultimately be attached to. If you do not define a special solution, a simple
round-robin style distribution is used by default.
• read_handshake(): This function is called when the initial handshake information is returned by the
server. You can capture the handshake information returned and provide additional checks before the
authorization exchange takes place.
• read_auth(): This function is called when the authorization packet (user name, password, default
database) are submitted by the client to the server for authentication.
• read_auth_result(): This function is called when the server returns an authorization packet to the
client indicating whether the authorization succeeded.
• read_query(): This function is called each time a query is sent by the client to the server. You can use
this to edit and manipulate the original query, including adding new queries before and after the original
statement. You can also use this function to return information directly to the client, bypassing the server,
which can be useful to filter unwanted queries or queries that exceed known limits.
• read_query_result(): This function is called each time a result is returned from the server, providing
you have manually injected queries into the query queue. If you have not explicitly injected queries within
the read_query() function, this function is not triggered. You can use this to edit the result set, or to
remove or filter the result sets generated from additional queries you injected into the queue when using
read_query().
The following table lists MySQL proxy and server communication functions, the supplied information, and
the direction of information flow when the function is triggered.
Function Supplied Information Direction
connect_server() None Client to Server
read_handshake() None Server to Client
read_auth() None Client to Server
read_auth_result() None Server to Client
read_query() Query Client to Server
read_query_result() Query result Server to Client
By default, all functions return a result that indicates whether the data should be passed on to the client
or server (depending on the direction of the information being transferred). This return value can be
overridden by explicitly returning a constant indicating that a particular response should be sent. For
example, it is possible to construct result set information by hand within read_query() and to return the
result set directly to the client without ever sending the original query to the server.
In addition to these functions, a number of built-in structures provide control over how MySQL Proxy
forwards queries and returns the results by providing a simplified interface to elements such as the list of
queries and the groups of result sets that are returned.
21
Proxy Scripting Sequence During Query Injection
server, and the information that the proxy ultimately returns to the client, need not match or correlate.
Once the client has connected to the proxy, the sequence shown in the following diagram occurs for each
individual query sent by the client.
1. When the client submits one query to the proxy, the read_query() function within the proxy is
triggered. The function adds the query to the query queue.
2. Once manipulation by read_query() has completed, the queries are submitted, sequentially, to the
MySQL server.
3. The MySQL server returns the results from each query, one result set for each query submitted. The
read_query_result() function is triggered for each result set, and each invocation can decide
which result set to return to the client
For example, you can queue additional queries into the global query queue to be processed by the server.
This can be used to add statistical information by adding queries before and after the original query,
changing the original query:
22
Internal Structures
SELECT NOW();
SELECT * FROM City;
SELECT NOW();
You can also modify the original statement; for example, to add EXPLAIN to each statement executed
to get information on how the statement was processed, again altering our original SQL statement into a
number of statements:
In both of these examples, the client would have received more result sets than expected. Regardless of
how you manipulate the incoming query and the returned result, the number of queries returned by the
proxy must match the number of original queries sent by the client.
You could adjust the client to handle the multiple result sets sent by the proxy, but in most cases you will
want the existence of the proxy to remain transparent. To ensure that the number of queries and result
sets match, you can use the MySQL Proxy read_query_result() to extract the additional result set
information and return only the result set the client originally requested back to the client. You can achieve
this by giving each query that you add to the query queue a unique ID, then filter out queries that do not
match the original query ID when processing them with read_query_result().
The following table describes common attributes of the MySQL proxy scripting element.
Attribute Description
connection A structure containing the active client connections. For a list of attributes,
see proxy.connection.
servers A structure containing the list of configured backend servers. For a list of
attributes, see proxy.global.backends.
queries A structure containing the queue of queries that will be sent to the server
during a single client query. For a list of attributes, see proxy.queries.
PROXY_VERSION The version number of MySQL Proxy, encoded in hex. You can use this
to check that the version number supports a particular option from within
the Lua script. The value is encoded as a hex value, so to check the
version is at least 0.5.1 you compare against 0x00501.
proxy.connection
The proxy.connection object is read only, and provides information about the current connection,
and is split into a client and server tables. This enables you to examine information about both the
incoming client connections to the proxy (client), and to the backend servers (server).
The following table describes the client and server attributes of the proxy.connection object.
23
Internal Structures
Attribute Description
client.default_db Default database requested by the client
client.username User name used to authenticate
The scrambled version of the password used to authenticate
client.scrambled_password
client.dst.name The combined address:port of the Proxy port used by this client
(should match the --proxy-address configuration parameter)
client.dst.address The IP address of the of the Proxy port used by this client
client.dst.port The port number of the of the Proxy port used by this client
client.src.name The combined address:port of the client (originating) TCP/IP endpoint
client.src.address The IP address of the client (originating) TCP/IP port
client.src.port The port of the client (originating) TCP/IP endpoint
server.scramble_buffer The scramble buffer used to scramble the password
server.mysqld_version The MySQL version number of the server
server.thread_id The ID of the thread handling the connection to the current server
server.dst.name The combined address:port for the backend server for the current
connection (i.e. the connection to the MySQL server)
server.dst.address The address for the backend server
server.dst.port The port for the backend server
server.src.name The combined address:port for the TCP/IP endpoint used by the
Proxy to connect to the backend server
server.src.address The address of the endpoint for the proxy-side connection to the MySQL
server
server.src.port The port of the endpoint for the proxy-side connection to the MySQL
server
proxy.global.backends
The proxy.global.backends table is partially writable and contains an array of all the configured
backend servers and the server metadata (IP address, status, etc.). You can determine the array index of
the current connection using proxy.connection["backend_ndx"] which is the index into this table of
the backend server being used by the active connection.
The attributes for each entry within the proxy.global.backends table are shown in the following table.
Attribute Description
dst.name The combined address:port of the backend server.
dst.address The IP address of the backend server.
dst.port The port of the backend server.
connected_clients The number of clients currently connected.
state The status of the backend server. See Backend State/Type
Constants [27].
type The type of the backend server. You can use this to identify whether the
backed was configured as a standard read/write backend, or a read-only
backend. You can compare this value to the proxy.BACKEND_TYPE_RW
and proxy.BACKEND_TYPE_RO.
24
Internal Structures
proxy.queries
The proxy.queries object is a queue representing the list of queries to be sent to the server. The
queue is not populated automatically, but if you do not explicitly populate the queue, queries are
passed on to the backend server verbatim. Also, if you do not populate the query queue by hand, the
read_query_result() function is not triggered.
The following functions are supported for populating the proxy.queries object.
Function Description
append(id,packet, Appends a query to the end of the query queue. The id is an integer
[options]) identifier that you can use to recognize the query results when they are
returned by the server. The packet should be a properly formatted query
packet. The optional options should be a table containing the options
specific to this packet.
prepend(id,packet) Prepends a query to the query queue. The id is an identifier that you can
use to recognize the query results when they are returned by the server.
The packet should be a properly formatted query packet.
reset() Empties the query queue.
len() Returns the number of query packets in the queue.
For example, you could append a query packet to the proxy.queries queue by using the append():
proxy.queries:append(1,packet)
The optional third argument to append() should contain the options for the packet. To have access to the
result set through the read_query_result() function, set the resultset_is_needed flag to true:
• Reduce memory usage (because the result set is not stored internally for processing)
The default mode is therefore quicker and useful if you only want to monitor the queries sent, and the basic
statistics.
To perform any kind of manipulation on the returned data, you must set the flag to true, which will:
• Enable you to discard the result set instead of returning it to the client.
proxy.response
The proxy.response structure is used when you want to return your own MySQL response, instead
of forwarding a packet that you have received a backend server. The structure holds the response type
information, an optional error message, and the result set (rows/columns) to return.
25
Internal Structures
Attribute Description
type The type of the response. The type must be either MYSQLD_PACKET_OK
or MYSQLD_PACKET_ERR. If the MYSQLD_PACKET_ERR, you should
set the value of the mysql.response.errmsg with a suitable error
message.
errmsg A string containing the error message that will be returned to the client.
resultset A structure containing the result set information (columns and rows),
identical to what would be returned when returning a results from a
SELECT query.
An example of this can be seen in the tutorial-resultset.lua script within the MySQL Proxy
package:
return proxy.PROXY_SEND_RESULT
proxy.response.resultset
The proxy.response.resultset structure should be populated with the rows and columns of data to
return. The structure contains the information about the entire result set, with the individual elements of the
data shown in the following table.
26
Internal Structures
Attribute Description
fields The definition of the columns being returned. This should be a dictionary
structure with the type specifying the MySQL data type, and the name
specifying the column name. Columns should be listed in the order of the
column data that will be returned.
flags A number of flags related to the result set. Valid flags include
auto_commit (whether an automatic commit was triggered),
no_good_index_used (the query executed without using an
appropriate index), and no_index_used (the query executed without
using any index).
rows The actual row data. The information should be returned as an array of
arrays. Each inner array should contain the column data, with the outer
array making up the entire result set.
warning_count The number of warnings for this result set.
affected_rows The number of rows affected by the original statement.
insert_id The last insert ID for an auto-incremented column in a table.
query_status The status of the query operation. You can use the MYSQLD_PACKET_OK
or MYSQLD_PACKET_ERR constants to populate this parameter.
The following constants are used internally by the proxy to specify the response to send to the client or
server. All constants are exposed as values within the main proxy table.
Constant Description
PROXY_SEND_QUERY Causes the proxy to send the current contents of the queries queue to the
server.
PROXY_SEND_RESULT Causes the proxy to send a result set back to the client.
PROXY_IGNORE_RESULT Causes the proxy to drop the result set (nothing is returned to the client).
As constants, these entities are available without qualification in the Lua scripts. For example, at the end of
the read_query_result() you might return PROXY_IGNORE_RESULT:
return proxy.PROXY_IGNORE_RESULT
The following states describe the status of a network packet. These items are entries within the main
proxy table.
Constant Description
MYSQLD_PACKET_OK The packet is OK
MYSQLD_PACKET_ERR The packet contains error information
MYSQLD_PACKET_RAW The packet contains raw data
27
Internal Structures
The following constants are used either to define the status or type of the backend MySQL server to which
the proxy is connected. These items are entries within the main proxy table.
Constant Description
BACKEND_STATE_UNKNOWN The current status is unknown
BACKEND_STATE_UP The backend is known to be up (available)
BACKEND_STATE_DOWN The backend is known to be down (unavailable)
BACKEND_TYPE_UNKNOWN Backend type is unknown
BACKEND_TYPE_RW Backend is available for read/write
BACKEND_TYPE_RO Backend is available only for read-only use
The values described in the table below are used in the packets exchanged between the client and server
to identify the information in the rest of the packet. These items are entries within the main proxy table.
The packet type is defined as the first character in the sent packet. For example, when intercepting packets
from the client to edit or monitor a query, you would check that the first byte of the packet was of type
proxy.COM_QUERY.
Constant Description
COM_SLEEP Sleep
COM_QUIT Quit
COM_INIT_DB Initialize database
COM_QUERY Query
COM_FIELD_LIST Field List
COM_CREATE_DB Create database
COM_DROP_DB Drop database
COM_REFRESH Refresh
COM_SHUTDOWN Shutdown
COM_STATISTICS Statistics
COM_PROCESS_INFO Process List
COM_CONNECT Connect
COM_PROCESS_KILL Kill
COM_DEBUG Debug
COM_PING Ping
COM_TIME Time
COM_DELAYED_INSERT Delayed insert
COM_CHANGE_USER Change user
COM_BINLOG_DUMP Binlog dump
COM_TABLE_DUMP Table dump
COM_CONNECT_OUT Connect out
COM_REGISTER_SLAVE Register slave
COM_STMT_PREPARE Prepare server-side statement
28
Internal Structures
Constant Description
COM_STMT_EXECUTE Execute server-side statement
COM_STMT_SEND_LONG_DATA Long data
COM_STMT_CLOSE Close server-side statement
COM_STMT_RESET Reset statement
COM_SET_OPTION Set option
COM_STMT_FETCH Fetch statement
COM_DAEMON Daemon (MySQL 5.1 only)
COM_ERROR Error
These constants are used to identify the field types in the query result data returned to clients from the
result of a query. These items are entries within the main proxy table.
29
Capturing a Connection with connect_server()
There are no arguments to the function, but you can use and if necessary manipulate the information in the
proxy.connection table, which is unique to each client session.
For example, if you have multiple backend servers, you can specify which server that connection should
use by setting the value of proxy.connection.backend_ndx to a valid server number. The following
code chooses between two servers based on whether the current time in minutes is odd or even:
function connect_server()
print("--> a client really wants to talk to a server")
if (tonumber(os.date("%M")) % 2 == 0) then
proxy.connection.backend_ndx = 2
print("Choosing backend 2")
else
proxy.connection.backend_ndx = 1
print("Choosing backend 1")
end
print("Using " .. proxy.global.backends[proxy.connection.backend_ndx].dst.name)
end
This example also displays the IP address/port combination by accessing the information from the internal
proxy.global.backends table.
For example, you can print out the handshake data and refuse clients by IP address with the following
function:
function read_handshake()
print("<-- let's send him some information about us")
print(" mysqld-version: " .. proxy.connection.server.mysqld_version)
print(" thread-id : " .. proxy.connection.server.thread_id)
print(" scramble-buf : " .. string.format("%q",proxy.connection.server.scramble_buffer))
30
Examining the Authentication Credentials with read_auth()
return proxy.PROXY_SEND_RESULT
end
end
You can obtain the authentication information by examining the proxy.connection.client structure.
For more information, see proxy.connection.
For example, you can print the user name and password supplied during authorization using:
function read_auth()
print(" username : " .. proxy.connection.client.username)
print(" password : " .. string.format("%q", proxy.connection.client.scrambled_password))
end
You can interrupt the authentication process within this function and return an error packet back to the
client by constructing a new packet and returning proxy.PROXY_SEND_RESULT:
proxy.response.type = proxy.MYSQLD_PACKET_ERR
proxy.response.errmsg = "Logins are not allowed"
return proxy.PROXY_SEND_RESULT
function read_auth_result(auth)
local state = auth.packet:byte()
31
Manipulating Queries with read_query()
end
end
If a long-password capable client tries to authenticate to a server that supports long passwords, but the
user password provided is actually short, read_auth_result() will be called twice. The first time,
auth.packet:byte() will equal 254, indicating that the client should try again using the old password
protocol. The second time read_auth_result()/ is called, auth.packet:byte() will indicate
whether the authentication actually succeeded.
For example, you can intercept a query packet and print out the contents using the following function
definition:
This example checks the first byte of the packet to determine the type. If the type is COM_QUERY (see
Server Command Constants [28]), we extract the query from the packet and print it. The structure of
the packet type supplied is important. In the case of a COM_QUERY packet, the remaining contents of the
packet are the text of the query string. In this example, no changes have been made to the query or the list
of queries that will ultimately be sent to the MySQL server.
To modify a query, or add new queries, you must populate the query queue (proxy.queries), then
execute the queries that you have placed into the queue. If you do not modify the original query or the
queue, the query received from the client is sent to the MySQL server verbatim.
When adding queries to the queue, you should follow these guidelines:
• The packets inserted into the queue must be valid query packets. For each packet, you must set the
initial byte to the packet type. If you are appending a query, you can append the query statement to the
rest of the packet.
• Once you add a query to the queue, the queue is used as the source for queries sent to the server. If
you add a query to the queue to add more information, you must also add the original query to the queue
or it will not be executed.
• Once the queue has been populated, you must set the return value from read_query() to indicate
whether the query queue should be sent to the server.
• When you add queries to the queue, you should add an ID. The ID you specify is returned with the result
set so that you identify each query and corresponding result set. The ID has no other purpose than as an
identifier for correlating the query and result set. When operating in a passive mode, during profiling for
example, you identify the original query and the corresponding result set so that the results expected by
the client can be returned correctly.
• Unless your client is designed to cope with more result sets than queries, you should ensure that the
number of queries from the client match the number of results sets returned to the client. Using the
unique ID and removing result sets you inserted will help.
32
Manipulating Results with read_query_result()
Normally, the read_query() and read_query_result() function are used in conjunction with each
other to inject additional queries and remove the additional result sets. However, read_query_result()
is only called if you populate the query queue within read_query().
• id: The ID of the result set, which corresponds to the ID that was set when the query packet was
submitted to the server when using append(id) on the query queue. You must have set the
resultset_is_needed flag to append to intercept the result set before it is returned to the client. See
proxy.queries [25].
• query_time: The number of microseconds required to receive the first row of a result set since the
query was sent to the server.
• response_time: The number of microseconds required to receive the last row of the result set since
the query was sent to the server.
By accessing the result information from the MySQL server, you can extract the results that match the
queries that you injected, return different result sets (for example, from a modified query), and even create
your own result sets.
The following Lua script, for example, will output the query, followed by the query time and response time
(that is, the time to execute the query and the time to return the data for the query) for each query sent to
the server:
proxy.queries:append(1, packet )
return proxy.PROXY_SEND_QUERY
end
end
function read_query_result(inj)
print("query-time: " .. (inj.query_time / 1000) .. "ms")
print("response-time: " .. (inj.response_time / 1000) .. "ms")
end
You can access the rows of returned results from the result set by accessing the rows property of the
resultset property of the result that is exposed through read_query_result(). For example, you can
iterate over the results showing the first column from each row using this Lua fragment:
Just like read_query(), read_query_result() can return different values for each result according
to the result returned. If you have injected additional queries into the query queue, for example, remove
33
Manipulating Results with read_query_result()
the results returned from those additional queries and return only the results from the query originally
submitted by the client.
The following example injects additional SELECT NOW() statements into the query queue, giving them
a different ID to the ID of the original query. Within read_query_result(), if the ID for the injected
queries is identified, we display the result row, and return the proxy.PROXY_IGNORE_RESULT from the
function so that the result is not returned to the client. If the result is from any other query, we print out the
query time information for the query and return the default, which passes on the result set unchanged. We
could also have explicitly returned proxy.PROXY_IGNORE_RESULT to the MySQL client.
return proxy.PROXY_SEND_QUERY
end
end
function read_query_result(inj)
if inj.id == 2 then
for row in inj.resultset.rows do
print("injected query returned: " .. row[1])
end
return proxy.PROXY_IGNORE_RESULT
else
print("query-time: " .. (inj.query_time / 1000) .. "ms")
print("response-time: " .. (inj.response_time / 1000) .. "ms")
end
end
34
Chapter 6 Using MySQL Proxy
Table of Contents
6.1 Using the Administration Interface ............................................................................................... 35
There are a number of different ways to use MySQL Proxy. At the most basic level, you can allow MySQL
Proxy to pass queries from clients to a single server. To use MySQL Proxy in this mode, you just have to
specify on the command line the backend server to which the proxy should connect:
If you specify multiple backend MySQL servers, the proxy connects each client to each server in a round-
robin fashion. Suppose that you have two MySQL servers, A and B. The first client to connect is connected
to server A, the second to server B, the third to server A. For example:
shell> mysql-proxy \
--proxy-backend-addresses=narcissus:3306 \
--proxy-backend-addresses=nostromo:3306
When you specify multiple servers in this way, the proxy automatically identifies when a MySQL server has
become unavailable and marks it accordingly. New connections are automatically attached to a server that
is available, and a warning is reported to the standard output from mysql-proxy:
Lua scripts enable a finer level of control, both over the connections and their distribution and how queries
and result sets are processed. When using an Lua script, you must specify the name of the script on the
command line using the --proxy-lua-script option:
When you specify a script, the script is not executed until a connection is made. This means that faults with
the script are not raised until the script is executed. Script faults will not affect the distribution of queries to
backend MySQL servers.
Note
Because a script is not read until the connection is made, you can modify the
contents of the Lua script file while the proxy is still running and the modified script
is automatically used for the next connection. This ensures that MySQL Proxy
remains available because it need not be restarted for the changes to take effect.
In mysql-proxy 0.8.0 and earlier, a rudimentary interface was built into the proxy. In later versions this
was replaced so that you must specify an administration script to be used when users connect to the
administration interface.
35
Using the Administration Interface
To use the administration interface, specify the user name and password required to connect to the admin
service, using the --admin-username and --admin-password options. You must also specify the Lua
script to be used as the interface to the administration service by using the admin-lua-script script
option to point to a Lua script.
For example, you can create a basic interface to the internal components of the mysql-proxy system
using the following script, written by Diego Medina:
--[[
Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
--]]
-- admin.lua
--[[
See http://www.chriscalender.com/?p=41
(Thanks to Chris Calender)
See http://datacharmer.blogspot.com/2009/01/mysql-proxy-is-back.html
(Thanks Giuseppe Maxia)
--]]
function set_error(errmsg)
proxy.response = {
type = proxy.MYSQLD_PACKET_ERR,
errmsg = errmsg or "error"
}
end
function read_query(packet)
if packet:byte() ~= proxy.COM_QUERY then
set_error("[admin] we only handle text-based queries (COM_QUERY)")
return proxy.PROXY_SEND_RESULT
end
if f_e then
-- if that match, take the next sub-string as option
f_s, f_e, option = string.find(packet, "^%s+(%w+)", f_e + 1)
end
36
Using the Administration Interface
return proxy.PROXY_SEND_RESULT
{ name = "address",
type = proxy.MYSQL_TYPE_STRING },
{ name = "state",
type = proxy.MYSQL_TYPE_STRING },
{ name = "type",
type = proxy.MYSQL_TYPE_STRING },
}
for i = 1, #proxy.global.backends do
local b = proxy.global.backends[i]
rows[#rows + 1] = {
i, b.dst.name, b.state, b.type
}
end
else
set_error()
return proxy.PROXY_SEND_RESULT
end
proxy.response = {
type = proxy.MYSQLD_PACKET_OK,
resultset = {
fields = fields,
37
Using the Administration Interface
rows = rows
}
}
return proxy.PROXY_SEND_RESULT
end
proxy.response.resultset = {
fields = {},
rows = {}
}
for i,v in pairs (header) do
table.insert(proxy.response.resultset.fields, {type = proxy.MYSQL_TYPE_STRING, name = v})
end
for i,v in pairs (dataset) do
table.insert(proxy.response.resultset.rows, v )
end
return proxy.PROXY_SEND_RESULT
end
function show_process_table()
local dataset = {}
local header = { 'Id', 'IP Address', 'Time' }
local rows = {}
for t_i, t_v in pairs (proxy.global.process) do
for s_i, s_v in pairs ( t_v ) do
table.insert(rows, { t_i, s_v.ip, os.date('%c',s_v.ts) })
end
end
return make_dataset(header,rows)
end
function show_process_help()
local dataset = {}
local header = { 'command', 'description' }
local rows = {
{'SELECT HELP', 'This command.'},
{'SHOW PROXY PROCESSLIST', 'Show all connections and their true IP Address.'},
}
return make_dataset(header,rows)
end
function dump_process_table()
proxy.global.initialize_process_table()
print('current contents of process table')
for t_i, t_v in pairs (proxy.global.process) do
print ('session id: ', t_i)
for s_i, s_v in pairs ( t_v ) do
print ( '\t', s_i, s_v.ip, s_v.ts )
end
end
print ('---END PROCESS TABLE---')
end
--[[ Help
is split into
command = "show"
option = "querycounter"
38
Using the Administration Interface
returns a error-packet
--]]
--[[
Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
--]]
-- reporter.lua
--[[
See http://www.chriscalender.com/?p=41
(Thanks to Chris Calender)
See http://datacharmer.blogspot.com/2009/01/mysql-proxy-is-back.html
(Thanks Giuseppe Maxia)
--]]
proxy.global.query_counter = proxy.global.query_counter or 0
function proxy.global.initialize_process_table()
if proxy.global.process == nil then
proxy.global.process = {}
end
if proxy.global.process[proxy.connection.server.thread_id] == nil then
proxy.global.process[proxy.connection.server.thread_id] = {}
end
end
function disconnect_client()
local connection_id = proxy.connection.server.thread_id
if connection_id then
-- client has disconnected, set this to nil
proxy.global.process[connection_id] = nil
end
39
Using the Administration Interface
end
---
-- read_query() can return a resultset
--
-- You can use read_query() to return a result-set.
--
-- @param packet the mysql-packet sent by the client
--
-- @return
-- * nothing to pass on the packet as is,
-- * proxy.PROXY_SEND_QUERY to send the queries from the proxy.queries queue
-- * proxy.PROXY_SEND_RESULT to send your own result-set
--
function read_query( packet )
-- a new query came in this connection
-- using proxy.global.* to make it available to the admin plugin
proxy.global.query_counter = proxy.global.query_counter + 1
end
To use the script, save the first script to a file (admin.lua in the following example) and the other to
reporter.lua, then run mysql-proxy specifying the admin script and a backend MySQL server:
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
In another different window, connect to the mysql-proxy admin service using the specified user name
and password:
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
To monitor the status of the proxy, ask for a list of the current active processes:
mysql>
40
Using the Administration Interface
For more information on the example, see MySQL Proxy Admin Example.
41
42
Appendix A MySQL Proxy FAQ
Questions
• A.2: How do I use a socket with MySQL Proxy? Proxy change logs mention that support for UNIX
sockets has been added.
• A.5: Do proxy applications run on a separate server? If not, what is the overhead incurred by Proxy on
the DB server side?
• A.6: With load balancing, what happens to transactions? Are all queries sent to the same server?
• A.7: Is it possible to use MySQL Proxy with updating a Lucene index (or Solr) by making TCP calls to
that server to update?
• A.8: Is the system context switch expensive, how much overhead does the Lua script add?
• A.10: Do you have to make one large script and call it at proxy startup, can I change scripts without
stopping and restarting (interrupting) the proxy?
• A.11: If MySQL Proxy has to live on same machine as MySQL, are there any tuning considerations to
ensure both perform optimally?
• A.12: I currently use SQL Relay for efficient connection pooling with a number of Apache processes
connecting to a MySQL server. Can MySQL Proxy currently accomplish this? My goal is to minimize
connection latency while keeping temporary tables available.
• A.13: Are these reserved function names (for example, error_result()) that get automatically
called?
• A.14: As the script is re-read by MySQL Proxy, does it cache this or is it looking at the file system with
each request?
• A.15: Given that there is a connect_server() function, can a Lua script link up with multiple servers?
• A.17: The global namespace variable example with quotas does not persist after a reboot, is that
correct?
• A.20: Are there tools for isolating problems? How can someone figure out whether a problem is in the
client, the database, or the proxy?
• A.22: So authentication with connection pooling has to be done at every connection? What is the
authentication latency?
43
• A.23: If you have multiple databases on the same box, can you use proxy to connect to databases on
default port 3306?
• A.24: What about caching the authorization information so clients connecting are given back-end
connections that were established with identical authorization information, thus saving a few more round
trips?
• A.25: Is there any big web site using MySQL Proxy? For what purpose and what transaction rate have
they achieved?
• A.27: I tried using MySQL Proxy without any Lua script to try a round-robin type load balancing. In this
case, if the first database in the list is down, MySQL Proxy would not connect the client to the second
database in the list.
• A.29: How different is MySQL Proxy from DBCP (Database connection pooling) for Apache in terms of
connection pooling?
• A.30: MySQL Proxy can handle about 5000 connections, what is the limit on a MySQL server?
• A.31: Would the Java-only connection pooling solution work for multiple web servers? With this, I would
assume that you can pool across many web servers at once?
There is no automatic separation of queries that perform reads or writes to the different backend servers.
However, you can specify to mysql-proxy that one or more of the “backend” MySQL servers are read
only.
shell> mysql-proxy \
--proxy-backend-addresses=10.0.1.2:3306 \
--proxy-read-only-backend-addresses=10.0.1.3:3306 &
A.2: How do I use a socket with MySQL Proxy? Proxy change logs mention that support for UNIX
sockets has been added.
--proxy-backend-addresses=/path/to/socket
MySQL Proxy is designed to work with MySQL 5.0 or higher, and supports the MySQL network protocol for
5.0 and higher.
Use the --daemon option. To keep track of the process ID, the daemon can be started with the --pid-
file=file option to save the PID to a known file name. On version 0.5.x, the Proxy cannot be started
natively as a daemon.
A.5: Do proxy applications run on a separate server? If not, what is the overhead incurred by Proxy
on the DB server side?
44
You can run the proxy on the application server, on its own box, or on the DB-server depending on the use
case.
A.6: With load balancing, what happens to transactions? Are all queries sent to the same server?
Without any special customization the whole connection is sent to the same server. That keeps the whole
connection state intact.
A.7: Is it possible to use MySQL Proxy with updating a Lucene index (or Solr) by making TCP calls
to that server to update?
A.8: Is the system context switch expensive, how much overhead does the Lua script add?
Lua is fast and the overhead should be small enough for most applications. The raw packet overhead is
around 400 microseconds.
A.10: Do you have to make one large script and call it at proxy startup, can I change scripts
without stopping and restarting (interrupting) the proxy?
You can just change the script and the proxy will reload it when a client connects.
A.11: If MySQL Proxy has to live on same machine as MySQL, are there any tuning considerations
to ensure both perform optimally?
MySQL Proxy can live on any box: application, database, or its own box. MySQL Proxy uses comparatively
little CPU or RAM, with negligible additional requirements or overhead.
A.12: I currently use SQL Relay for efficient connection pooling with a number of Apache
processes connecting to a MySQL server. Can MySQL Proxy currently accomplish this? My goal is
to minimize connection latency while keeping temporary tables available.
Yes.
A.13: Are these reserved function names (for example, error_result()) that get automatically
called?
Only functions and values starting with proxy.* are provided by the proxy. All others are user provided.
A.14: As the script is re-read by MySQL Proxy, does it cache this or is it looking at the file system
with each request?
It looks for the script at client-connect and reads it if it has changed, otherwise it uses the cached version.
A.15: Given that there is a connect_server() function, can a Lua script link up with multiple
servers?
MySQL Proxy provides some tutorials in the source package; one is examples/tutorial-
keepalive.lua.
45
No, MySQL Proxy is an application that forwards packets from a client to a server using the MySQL
network protocol. The MySQL Proxy provides a API allowing you to change its behavior.
A.17: The global namespace variable example with quotas does not persist after a reboot, is that
correct?
Yes. If you restart the proxy, you lose the results, unless you save them in a file.
No, being the man-in-the-middle, Proxy cannot handle encrypted sessions because it cannot share the
SSL information.
The MySQL network protocol does not allow passwords to be sent in cleartext, all you could capture is the
encrypted version.
A.20: Are there tools for isolating problems? How can someone figure out whether a problem is in
the client, the database, or the proxy?
You can set a debug script in the proxy, which is an exceptionally good tool for this purpose. You can see
very clearly which component is causing the problem, if you set the right breakpoints.
Yes and no. Java connection pools are specific to Java applications, MySQL Proxy works with any client
API that talks the MySQL network protocol. Also, connection pools do not provide any functionality for
intelligently examining the network packets and modifying the contents.
A.22: So authentication with connection pooling has to be done at every connection? What is the
authentication latency?
You can skip the round-trip and use the connection as it was added to the pool. As long as the application
cleans up the temporary tables it used. The overhead is (as always) around 400 microseconds.
A.23: If you have multiple databases on the same box, can you use proxy to connect to databases
on default port 3306?
Yes, MySQL Proxy can listen on any port, provided that none of the MySQL servers are listening on the
same port.
A.24: What about caching the authorization information so clients connecting are given back-end
connections that were established with identical authorization information, thus saving a few more
round trips?
A.25: Is there any big web site using MySQL Proxy? For what purpose and what transaction rate
have they achieved?
Yes, gaiaonline. They have tested MySQL Proxy and seen it handle 2400 queries per second through the
proxy.
DBSlayer is a REST->MySQL tool, MySQL Proxy is transparent to your application. No change to the
application is needed.
46
A.27: I tried using MySQL Proxy without any Lua script to try a round-robin type load balancing. In
this case, if the first database in the list is down, MySQL Proxy would not connect the client to the
second database in the list.
A.29: How different is MySQL Proxy from DBCP (Database connection pooling) for Apache in
terms of connection pooling?
Connection Pooling is just one use case of the MySQL Proxy. You can use it for a lot more and it works in
cases where you cannot use DBCP (for example, if you do not have Java).
A.30: MySQL Proxy can handle about 5000 connections, what is the limit on a MySQL server?
The server limit is given by the value of the max_connections system variable. The default value is
version dependent.
A.31: Would the Java-only connection pooling solution work for multiple web servers? With this, I
would assume that you can pool across many web servers at once?
Yes. But you can also start one proxy on each application server to get a similar behavior as you have it
already.
47
48