The answer: use a modified network sniffer to parse out the MySQL packets and decode them. You’ll have to do a little compiling, but it’ll be worth it. Note that this will not usually work for local connections, although you are welcome to try.

First, you need to install libpcap-dev, which is the development library that allows an application to sniff network packets.

Now let’s make a directory, download the source code and compile it

At this point, we have a shiny new executable named mysqlsniffer in our source directory. You can copy it wherever you like (somewhere in the path would be useful)

cd mysqlsniffer

wget hackmysql.com/code/mysqlsniffer.tgz

tar xvfz mysqlsniffer.tgz

gcc -O2 -lpcap -o mysqlsniffer mysqlsniffer.c packet_handlers.c misc.c

To run mysqlsniffer, you need to specify the network interface that MySQL is listening on. For me, it’s eth0.

Loads of stuff starts flying by… let’s filter it out a little more so we can just get the queries and not all the excess data.

Ah, now there we are… all sorts of query information, without having to restart MySQL.

192.168.73.1.2622 > server: COM_QUERY: SELECT @@sql_mode192.168.73.1.2622 > server: COM_QUERY: SET SESSION sql_mode=”192.168.73.1.2622 > server: COM_QUERY: SET NAMES utf8192.168.73.1.1636 > server: COM_QUERY: SELECT @@SQL_MODE192.168.73.1.1636 > server: COM_QUERY: SHOW FULL COLUMNS FROM db2842_howto.wp_users

Here are the full options for the command:

Usage: mysqlsniffer [OPTIONS] INTERFACE

OPTIONS:–port N Listen for MySQL on port number N (default 3306)–verbose Show extra packet information–tcp-ctrl Show TCP control packets (SYN, FIN, RST, ACK)–net-hdrs Show major IP and TCP header values–no-mysql-hdrs Do not show MySQL header (packet ID and length)–state Show state–v40 MySQL server is version 4.0–dump Dump all packets in hex–help Print this

Original source code and more information at:http://hackmysql.com/mysqlsniffer

If you are running on a development server, it would be easier to just turn on query logging.