mysql-proxy tutorial - simple query logging

Developed In: Lua — Contributed by: Giuseppe Maxia

This script shows how to use MySQL proxy as a basic replacement for the general log *without restarting the server*.

MySQL Proxy is a simple program that sits between your client and MySQL server that can monitor, analyze or transform their communication. See http://forge.mysql.com/wiki/MySQL_Proxy for more information.


Giuseppe Maxia
Lua
  1. --[[
  2.  
  3.   Copyright (C) 2007 MySQL AB
  4.  
  5.   This program is free software; you can redistribute it and/or modify
  6.   it under the terms of the GNU General Public License as published by
  7.   the Free Software Foundation; version 2 of the License.
  8.  
  9.   This program is distributed in the hope that it will be useful,
  10.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12.   GNU General Public License for more details.
  13.  
  14.   You should have received a copy of the GNU General Public License
  15.   along with this program; if not, write to the Free Software
  16.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17.  
  18. --]]
  19.  
  20. ---
  21. -- Uses MySQL-Proxy to log your queries
  22. --
  23. -- Written by Giuseppe Maxia, based on examples provided
  24. -- by Jan Knesckhe
  25. --
  26. -- This script will log the current date and time, the connection id
  27. -- and the query to a file named "mysql.log" in the current directory
  28. --
  29. local log_file = 'mysql.log'
  30.  
  31. local fh = io.open(log_file, "a+")
  32.  
  33. function read_query( packet )
  34. if string.byte(packet) == proxy.COM_QUERY then
  35. local query = string.sub(packet, 2)
  36. fh:write( string.format("%s %6d -- %s \n",
  37. os.date('%Y-%m-%d %H:%M:%S'),
  38. proxy.connection["thread_id"],
  39. query))
  40. fh:flush()
  41. end
  42. end

Current Tags

You must be logged in to tag this tool

Using MySQL Proxy 0.7.2 you get an error message

2009-08-12 20:41:48: (critical) (read_query) [string "/home/rbradfor/mysql-proxy-0.7.2-linux-rhel..."]:39: proxy.connection.thread_id is deprecated, use proxy.connection.server.thread_id instead

You need to change the line proxy.connection["thread_id"] to proxy.connection.server["thread_id"]

to get the script to work correctly.

On version 0.6.1 (maybe earlier versions as well)

you need to modify the line

  1. proxy.connection["thread_id"]
for
  1. proxy.connection.server["thread_id"]

Votes

  • Rated 4.67 out of 5
Rated 4.67 out of 5 with 3 votes cast.
You must be logged in to vote.

Watches

1 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment