Have you ever wanted a handy reference of all of the various variables available in MySQL Proxy?
I’ve written a Lua script that outputs many of the available variables, which is really useful for coding Lua scripts for MySQL Proxy. In addition to the variables, it also generates output during each main function it enters in the Proxy, so you can follow the execution trace, and then see the value of each variable when it is within a particular function.
I’ve tested this on MySQL Proxy 0.7.2 and the newly available Proxy 0.8.0.
Here is a sample of the output, where I just connect, and then issue a simple query (SELECT * FROM world.city LIMIT 1):
C:\Program Files\MySQL\mysql-proxy-0.8.0\bin>mysql-proxy.exe --proxy-lua-script=show-vars.lua /* Entering function connect_server() */ When the proxy accepts a connection from a MySQL client, the connect_server() function is called. 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. /* Entering function read_handshake() */ Handshake information is sent by the server to the client after the initial connection (through connect_server()) has been made. read_handshake() doesn't receive a parameter anymore. Instead all the data is available in the connection tables. /* Entering function read_auth() */ The read_auth() function is triggered when an authentication handshake is initiated by the client. read_auth() doesn't receive a parameter anymore. Instead all the data is available in the connection tables. /* Entering function read_auth_result() */ The return packet from the server during authentication is captured by read_auth_result(). ... /* Entering function read_query( packet ) */ | o = [backends] userdata: 01AD9298 (type=userdata) [config] table: 01AD5890 (type=table) | Query = select * from world.city limit 1 | Time Stamp = 2010-01-27 16:14:05 | Proxy Version = 00800 | Lua Version = Lua 5.1 /* Entering function read_query_result( inj ) */ | os.date = 2010-01-27 16:14:05 | inj.query = ♥select * from world.city limit 1 | proxy.connection.server.scramble_buffer = "]CvKTD8,r|9B#&3oU>tO" | proxy.connection.server.mysqld_version = 50089 | proxy.connection.server.thread_id = 21 | proxy.connection.client.default_db = world | proxy.connection.client.username = root | proxy.connection.client.scrambled_password = "+δí!ò→g~\000h[BNòr∩+╦═" | proxy.connection.client.dst.name = 127.0.0.1:4040 | proxy.connection.client.dst.address = 127.0.0.1 | proxy.connection.client.dst.port = 4040 | proxy.connection.server.dst.name = 127.0.0.1:3306 | proxy.connection.server.dst.address = 127.0.0.1 | proxy.connection.server.dst.port = 3306 | proxy.connection.client.src.name = 127.0.0.1:58468 | proxy.connection.client.src.address = 127.0.0.1 | proxy.connection.client.src.port = 58468 | proxy.connection.server.src.name = 127.0.0.1:58469 | proxy.connection.server.src.address = 127.0.0.1 | proxy.connection.server.src.port = 58469 | inj.query-time = 4.6ms | inj.response-time = 4.613ms | proxy.connection.backend_ndx = 1 | #proxy.global.backends = 1 | proxy.global.backends[ndx].dst.name = 127.0.0.1:3306 | proxy.global.backends[ndx].dst.address = 127.0.0.1 | proxy.global.backends[ndx].dst.port = 3306 | proxy.global.backends[ndx].connected_clients = 1 | proxy.global.backends[ndx].state = 1 | proxy.global.backends[ndx].pool.max_idle_connections = 0 | proxy.global.backends[ndx].pool.min_idle_connections = 0 | proxy.global.backends[ndx].pool.users[1] = 0 | result.len = 1 | result.flags.in_trans = false | result.flags.auto_commit = true | result.flags.no_good_index_used = false | result.flags.no_index_used = true | result.warning_count = 0 | result.query_status = 0 | result.fields[1].type = 3 | result.fields[1].name = ID | query returned = 1 /* Entering function read_query( packet ) */ | o = [backends] userdata: 01AD9298 (type=userdata) [config] table: 01AD5890 (type=table) /* Entering function disconnect_client() */ Client 21 has disconnected. C:\Program Files\MySQL\mysql-proxy-0.8.0\bin>
And, here is the full script (named “show-vars.lua” in the above invocation):
function connect_server()
print("/* Entering function connect_server() */")
print(" When the proxy accepts a connection from a MySQL client, the")
print(" connect_server() function is called. There are no arguments")
print(" to the function, but you can use and if necessary manipulate")
print(" the information in the proxy.connection table, which is unique")
print(" to each client session.\n")
end
function read_handshake()
print("/* Entering function read_handshake() */")
print(" Handshake information is sent by the server to the client")
print(" after the initial connection (through connect_server()) has")
print(" been made. read_handshake() doesn't receive a parameter")
print(" anymore. Instead all the data is available in the connection")
print(" tables.\n")
end
function read_auth()
print("/* Entering function read_auth() */")
print(" The read_auth() function is triggered when an authentication")
print(" handshake is initiated by the client. read_auth() doesn't")
print(" receive a parameter anymore. Instead all the data is")
print(" available in the connection tables.\n")
end
function read_auth_result()
print("/* Entering function read_auth_result() */")
print(" The return packet from the server during authentication is")
print(" captured by read_auth_result().\n")
end
function read_query( packet )
print("/* Entering function read_query( packet ) */")
o = ""
for k, v in pairs(proxy.global) do
o = o .. (("[%s] %s (type=%s)\n"):format(k, tostring(v), type(v)))
end
print("| o = " .. o)
if string.byte(packet) == proxy.COM_QUERY then
proxy.queries:append(1, packet, {resultset_is_needed = true} )
print("| Query = "
.. string.sub(packet, 2))
print("| Time Stamp = "
.. os.date('%Y-%m-%d %H:%M:%S'))
print("| Proxy Version = "
.. string.format("%05x", proxy.PROXY_VERSION))
print("| Lua Version = "
.. _VERSION)
return proxy.PROXY_SEND_QUERY
end
end
function read_query_result( inj )
print("\n/* Entering function read_query_result( inj ) */")
local res = assert(inj.resultset)
local raw_len = assert(res.raw):len()
local packet = assert(inj.query)
local flags = res.flags
local backend_ndx = proxy.connection.backend_ndx
print("| os.date = "
.. os.date('%Y-%m-%d %H:%M:%S'))
print("| inj.query = "
.. inj.query)
print("| proxy.connection.server.scramble_buffer = "
.. string.format("%q",proxy.connection.server.scramble_buffer))
print("| proxy.connection.server.mysqld_version = "
.. proxy.connection.server.mysqld_version)
print("| proxy.connection.server.thread_id = "
.. proxy.connection.server.thread_id)
print("| proxy.connection.client.default_db = "
.. proxy.connection.client.default_db)
print("| proxy.connection.client.username = "
.. proxy.connection.client.username)
print("| proxy.connection.client.scrambled_password = "
.. string.format("%q", proxy.connection.client.scrambled_password))
print("| proxy.connection.client.dst.name = "
.. proxy.connection.client.dst.name)
print("| proxy.connection.client.dst.address = "
.. proxy.connection.client.dst.address)
print("| proxy.connection.client.dst.port = "
.. proxy.connection.client.dst.port)
print("| proxy.connection.server.dst.name = "
.. proxy.connection.server.dst.name)
print("| proxy.connection.server.dst.address = "
.. proxy.connection.server.dst.address)
print("| proxy.connection.server.dst.port = "
.. proxy.connection.server.dst.port)
print("| proxy.connection.client.src.name = "
.. proxy.connection.client.src.name)
print("| proxy.connection.client.src.address = "
.. proxy.connection.client.src.address)
print("| proxy.connection.client.src.port = "
.. proxy.connection.client.src.port)
print("| proxy.connection.server.src.name = "
.. proxy.connection.server.src.name)
print("| proxy.connection.server.src.address = "
.. proxy.connection.server.src.address)
print("| proxy.connection.server.src.port = "
.. proxy.connection.server.src.port)
print("| inj.query-time = "
.. (inj.query_time / 1000) .. "ms")
print("| inj.response-time = "
.. (inj.response_time / 1000) .. "ms")
print("| proxy.connection.backend_ndx = "
.. proxy.connection.backend_ndx)
print("| #proxy.global.backends = "
.. #proxy.global.backends)
print("| proxy.global.backends[ndx].dst.name = "
.. proxy.global.backends[backend_ndx].dst.name)
print("| proxy.global.backends[ndx].dst.address = "
.. proxy.global.backends[backend_ndx].dst.address)
print("| proxy.global.backends[ndx].dst.port = "
.. proxy.global.backends[backend_ndx].dst.port)
print("| proxy.global.backends[ndx].connected_clients = "
.. proxy.global.backends[backend_ndx].connected_clients)
print("| proxy.global.backends[ndx].state = "
.. proxy.global.backends[backend_ndx].state)
print("| proxy.global.backends[ndx].pool.max_idle_connections = "
.. proxy.global.backends[backend_ndx].pool.max_idle_connections)
print("| proxy.global.backends[ndx].pool.min_idle_connections = "
.. proxy.global.backends[backend_ndx].pool.min_idle_connections)
print("| proxy.global.backends[ndx].pool.users[1] = "
.. proxy.global.backends[backend_ndx].pool.users[1].cur_idle_connections)
print("| result.len = "
.. raw_len)
print("| result.flags.in_trans = "
.. tostring(res.flags.in_trans))
print("| result.flags.auto_commit = "
.. tostring(res.flags.auto_commit))
print("| result.flags.no_good_index_used = "
.. tostring(res.flags.no_good_index_used))
print("| result.flags.no_index_used = "
.. tostring(res.flags.no_index_used))
print("| result.warning_count = "
.. res.warning_count)
print("| result.query_status = "
.. res.query_status)
if res.affected_rows then
print("| result.affected_rows = "
.. res.affected_rows)
print("| result.insert_id = "
.. res.insert_id)
end
if res.query_status == proxy.MYSQLD_PACKET_ERR then
print("| result.err.code = "
.. res.raw:byte(2) + (res.raw:byte(3) * 256))
print("| result.err.sql_state = "
.. string.format("%q", res.raw:sub(5, 9)))
print("| result.err.msg = "
.. string.format("%q", res.raw:sub(10)))
end
if res.query_status == proxy.MYSQLD_PACKET_OK then
print("| result.fields[1].type = "
.. res.fields[1].type)
print("| result.fields[1].name = "
.. res.fields[1].name)
for row in inj.resultset.rows do
print("| query returned = "
.. row[1])
end
end
print("\n")
end
function disconnect_client()
print("/* Entering function disconnect_client() */")
if proxy.connection.server.thread_id then
print("Client " .. proxy.connection.server.thread_id
.. " has disconnected.")
end
end