Using Tcpdump for MySQL query logging

If you are a privileged user (i.e. root), you can use tcpdump on a database server to take a peek into a network stream and filter for packets that go to MySQL. Those packets contain queries. Here’s a quick one-liner :
# tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | perl -e '
while(<>) { chomp; next if /^[^ ]+[ ]*$/;
if (defined $q) { print "$qn"; }
} else {
$_ =~ s/^[ t]+//; $q.=" $_";

Thanks to:

4 thoughts on “Using Tcpdump for MySQL query logging”

  1. Maatkit’s mk-query-digest now has built-in support for the MySQL protocol and can extract queries much more easily and accurately than this technique — and with timing information about the execution time, too.

Leave a Reply

Your email address will not be published. Required fields are marked *