<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jetpack Flight Log &#187; tcpdump</title>
	<atom:link href="http://jetpackweb.com/blog/tags/tcpdump/feed/" rel="self" type="application/rss+xml" />
	<link>http://jetpackweb.com/blog</link>
	<description>Rock{et}ing the interweb</description>
	<lastBuildDate>Wed, 19 May 2010 22:21:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Unobtrusive viewing of MySQL queries with tcpdump</title>
		<link>http://jetpackweb.com/blog/2009/09/16/unobstrusive-viewing-of-mysql-queries-with-tcpdump/</link>
		<comments>http://jetpackweb.com/blog/2009/09/16/unobstrusive-viewing-of-mysql-queries-with-tcpdump/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 17:33:32 +0000</pubDate>
		<dc:creator>Brian Racer</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[tcpdump]]></category>

		<guid isPermaLink="false">http://jetpackweb.com/blog/?p=381</guid>
		<description><![CDATA[There are times when you need to monitor the queries coming in to MySQL, but turning on query logging would create too much of a disk I/O hit, or you can&#8217;t restart the server to setup MySQL Proxy. Instead we can just monitor the network traffic and extract data that might be interesting using tcpdump [...]]]></description>
			<content:encoded><![CDATA[<p>There are times when you need to monitor the queries coming in to MySQL, but turning on query logging would create too much of a disk I/O hit, or you can&#8217;t restart the server to setup <a href="http://forge.mysql.com/wiki/MySQL_Proxy" target="_blank">MySQL Proxy</a>. Instead we can just monitor the network traffic and extract data that might be interesting using <strong>tcpdump</strong> and an inline perl script:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family: Monaco, monospace;"><span class="kw2">sudo</span> tcpdump <span class="re5">-i</span> lo <span class="re5">-s</span> <span class="nu0">0</span> <span class="re5">-l</span> <span class="re5">-w</span> - dst port <span class="nu0">3306</span> <span class="sy0">|</span> <span class="kw2">strings</span> <span class="sy0">|</span> <span class="kw2">perl</span> <span class="re5">-e</span> <span class="st_h">'
while(&lt;&gt;) { chomp; next if /^[^ ]+[ ]*$/;
  if(/^(SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER)/i) {
    if (defined $q) { print &quot;$q\n&quot;; }
    $q=$_;
  } else {
    $_ =~ s/^[ \t]+//; $q.=&quot; $_&quot;;
  }
}'</span></pre></div></div>

<p>This will only work for clients communicating via TCP &#8211; if you are connecting through &#8216;localhost&#8217; you will be going through a unix socket instead. If you switch &#8216;localhost&#8217; to &#8217;127.0.0.1&#8242; then your queries will go through the network stack.</p>
<p>If you just want to dump the traffic to a file for a little bit and analyze it later, do this instead:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family: Monaco, monospace;"><span class="kw2">sudo</span> tcpdump <span class="re5">-i</span> lo port <span class="nu0">3306</span> <span class="re5">-s</span> <span class="nu0">65535</span> <span class="re5">-x</span> <span class="re5">-n</span> <span class="re5">-q</span> -tttt<span class="sy0">&gt;</span> tcpdump.out</pre></div></div>

<p>You can then use <strong>mk-query-digest</strong> from <a href="http://www.maatkit.org/" target="_blank">Maatkit</a> with<strong>&#8211;type=tcpdump</strong>. See more about this at the <a href="http://www.mysqlperformanceblog.com/2009/07/01/gathering-queries-from-a-server-with-maatkit-and-tcpdump/" target="_blank">MySQL Performance Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jetpackweb.com/blog/2009/09/16/unobstrusive-viewing-of-mysql-queries-with-tcpdump/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
