<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Code in the hole (Entries tagged as mysql)</title>
    <link>http://codeinthehole.com/</link>
    <description>David Winterbottom</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.3.1 - http://www.s9y.org/</generator>
    
    

<item>
    <title>How to sync a MySQL table between two remote databases.</title>
    <link>http://codeinthehole.com/archives/35-How-to-sync-a-MySQL-table-between-two-remote-databases..html</link>
            <category>Development</category>
    
    <comments>http://codeinthehole.com/archives/35-How-to-sync-a-MySQL-table-between-two-remote-databases..html#comments</comments>
    <wfw:comment>http://codeinthehole.com/wfwcomment.php?cid=35</wfw:comment>

    <slash:comments>1</slash:comments>
    <wfw:commentRss>http://codeinthehole.com/rss.php?version=2.0&amp;type=comments&amp;cid=35</wfw:commentRss>
    

    <author>nospam@example.com (David Winterbottom)</author>
    <content:encoded>
    &lt;p&gt;Definitely tricker than you might think.&lt;/p&gt;
&lt;p&gt;
Seems like it should be trivial using &quot;SELECT ... INTO OUTFILE&quot; and &quot;LOAD DATA INFILE ...&quot; to make the transfer via dumping the table into a temporary file.  However, the &quot;SELECT ... INTO OUTFILE&quot; creates a file on the remote server, rather than locally.  This prevents the use of LOAD DATA INFILE for the second step as the file being loaded has to be local, or on the destination server.
&lt;/p&gt;
&lt;p&gt;
Following the &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/dev.mysql.com/doc/refman/5.0/en/select.html&#039;);&quot;  href=&quot;http://dev.mysql.com/doc/refman/5.0/en/select.html&quot;&gt;guidance in the docs&lt;/a&gt;, you can create local dump of a table by using the &quot;--execute&quot; option to 
output the results of a &quot;SELECT ...&quot; statement into a local file.  
&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;
mysql -D database_name -e &quot;SELECT ... &quot; &gt; /path/to/file.txt
&lt;/pre&gt;
&lt;p&gt;
This works but has two downsides:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
First, running a shell command forces you to step outside the MySQL adapter of your progamming language which means it is a new place where the database credentials need to be passed.  Shelling out commands always feels like you&#039;ve failed.
&lt;/li&gt;
&lt;li&gt;
Further, as far as I can tell, you can&#039;t control the field separator or line endings using this technique (in the same way as you can with &quot;SELECT ... INTO OUTFILE ...&quot;) and so the file includes an unwanted line with the field names and tab-separates the fields.
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
It&#039;s worth noting the mysqldump isn&#039;t much help here, as the &quot;--tab&quot; option that allows CSV output to be generated only works with a local database connection.
&lt;/p&gt;
&lt;p&gt;
Now that we&#039;ve got our data locally, we load it into the remote database using &quot;LOAD DATA INFILE&quot; and make use of the &quot;LOCAL&quot; keyword which lets us use a local data file: 
&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;
mysql -h x.x.x.x -u user -D database_name --password=... -e \
    &quot;LOAD DATA LOCAL INFILE &#039;/path/to/file.txt&#039; \
     REPLACE INTO TABLE table_name \
     IGNORE 1 LINES&quot;
&lt;/pre&gt;
&lt;p&gt;
Of course, you may want to truncate the table first if you want a clean sync.  As this operations locks the destination table, it often makes sense to load the data into a temporary copy of the table, and then perform a &quot;RENAME TABLE&quot; operation to swap in the new table.
&lt;/p&gt;
&lt;p&gt;
Here&#039;s a quick and dirty PHP implementation:
&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;
$tableName = &#039;some_table&#039;;
$sql =
   &quot;SELECT * 
    FROM $tableName&quot;;
$pathToCsv = &#039;/tmp/some-file.csv&#039;;
$command = sprintf(&quot;mysql -h %s -u %s  --password=%s -D %s -e &#039;%s&#039; &gt; %s&quot;,
    &#039;10.0.0.2&#039;, 
    &#039;db-user&#039;, 
    &#039;db-password&#039;, 
    &#039;database_name&#039;, 
    $sql, 
    $pathToCsv);
exec($command);

$sql =
   &quot;LOAD DATA LOCAL INFILE &#039;$pathToCsv&#039;
    REPLACE INTO TABLE `$tableName`
    CHARACTER SET &#039;utf8&#039;
    IGNORE 1 LINES&quot;;
$db-&gt;execute($sql); // Using your favourite database adapter
&lt;/pre&gt;
 
    </content:encoded>

    <pubDate>Fri, 03 Sep 2010 21:04:58 +0000</pubDate>
    <guid isPermaLink="false">http://codeinthehole.com/archives/35-guid.html</guid>
    <category>mysql</category>

</item>
<item>
    <title>Monitoring MySQL with Ganglia and gmetric</title>
    <link>http://codeinthehole.com/archives/8-Monitoring-MySQL-with-Ganglia-and-gmetric.html</link>
            <category>Monitoring</category>
    
    <comments>http://codeinthehole.com/archives/8-Monitoring-MySQL-with-Ganglia-and-gmetric.html#comments</comments>
    <wfw:comment>http://codeinthehole.com/wfwcomment.php?cid=8</wfw:comment>

    <slash:comments>4</slash:comments>
    <wfw:commentRss>http://codeinthehole.com/rss.php?version=2.0&amp;type=comments&amp;cid=8</wfw:commentRss>
    

    <author>nospam@example.com (David Winterbottom)</author>
    <content:encoded>
    &lt;div class=&quot;book_cover&quot;&gt;
&lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/www.amazon.co.uk/gp/product/0596102356?ie=UTF8&amp;amp;tag=codinthehol-21&amp;amp;linkCode=as2&amp;amp;camp=1634&amp;amp;creative=6738&amp;amp;creativeASIN=0596102356&#039;);&quot;  href=&quot;http://www.amazon.co.uk/gp/product/0596102356?ie=UTF8&amp;tag=codinthehol-21&amp;linkCode=as2&amp;camp=1634&amp;creative=6738&amp;creativeASIN=0596102356&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://codeinthehole.com/app/images/books/building-scalable-websites.jpg&quot;&gt;&lt;/a&gt;&lt;img src=&quot;http://www.assoc-amazon.co.uk/e/ir?t=codinthehol-21&amp;l=as2&amp;o=2&amp;a=0596102356&quot; width=&quot;1&quot; height=&quot;1&quot; border=&quot;0&quot; alt=&quot;&quot; style=&quot;border:none !important; margin:0px !important;&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;Following some server monitoring advice from the excellent &#039;Building Scalable Web Sites&#039; by Cal Henderson, I&#039;ve recently been experimenting with &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/ganglia.info/&#039;);&quot;  href=&quot;http://ganglia.info/&quot; title=&quot;Ganglia homepage&quot;&gt;Ganglia&lt;/a&gt; on a cluster of servers at &lt;a href=&quot;www.tangentlabs.co.uk&quot; title=&quot;Tangent Labs&quot;&gt;Tangent Labs&lt;/a&gt;.  It has proved to be deeply impressive and has given us a great tool for keeping an eye on how our servers are performing, as well as providing an invaluable diagnostic tool should things go wrong.&lt;/p&gt;

&lt;p&gt;In essence, Ganglia is a distributed monitoring application that allows statistics on a cluster of servers to be aggregated in a single place.  Moreover, an excellent web front-end is also provided to view charts of the statistics being collected.  You can view a &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/monitor.millennium.berkeley.edu/&#039;);&quot;  href=&quot;http://monitor.millennium.berkeley.edu/&quot; title=&quot;Live demo of Ganglia&quot;&gt;live demo of the Ganglia monitoring application for the UC Berkeley Millennium Project&lt;/a&gt; which shows exactly what Ganglia can provide.&lt;/p&gt;

&lt;p&gt;Out of the box, Ganglia collects basic server stats on load, memory, CPU, network and disk.  However, what makes it great is that it can be easily extended to collect statistics on anything that you can measure from the command line.  There is a &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/ganglia.sourceforge.net/gmetric/&#039;);&quot;  href=&quot;http://ganglia.sourceforge.net/gmetric/&quot; title=&quot;Visit the repository&quot;&gt;repository of user-contributed scripts&lt;/a&gt; although it&#039;s not that extensive.  In this post, I detail a script that can used to feed statistics about MySQL into Ganglia for monitoring - something important to any LAMP-stack application.&lt;/p&gt;&lt;p&gt;A few technical details first: Ganglia utilises three utilities to run each with a distinct responsibility:&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;gmond&lt;/dt&gt;&lt;dd&gt;A daemon responsible for collecting monitoring stats and sharing them using XML over TCP.   The gmond daemon runs on all nodes that you want to monitor.&lt;/dd&gt;
&lt;dt&gt;gmetad&lt;/dt&gt;&lt;dd&gt;A daemon responsible for collecting and storing data from other gmetad and gmomd services.  This only needs to be running on a single, central node.&lt;/dd&gt;
&lt;dt&gt;gmetric&lt;/dt&gt;&lt;dd&gt;A command-line application used for using Ganglia to monitor custom metrics.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;All very simple really - see &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/ganglia.wiki.sourceforge.net/ganglia_documents&#039;);&quot;  href=&quot;http://ganglia.wiki.sourceforge.net/ganglia_documents&quot; title=&quot;Ganglia docs&quot;&gt;the docs for the nitty gritty&lt;/a&gt;.  Ok, enough intro - here&#039;s my script for MySQL:&lt;/p&gt;

&lt;p&gt;&amp;raquo; &lt;a href=&quot;http://codeinthehole.com/app/downloads/gmetric-mysql.sh&quot;&gt;gmetric-mysql.sh (3.1kb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It&#039;s written in Bash and uses a temporary file to read the MySQL stats into before parsing and submitting to gmetric.  This is done as many of the MySQL stats of interest are cumulative counts rather than rates (eg SELECTs per second) which are generally of more interest.  Hence, the script estimates the rate of change by dividing the value delta by the timestamp delta.&lt;/p&gt;

&lt;p&gt;To use this script, simply copy it into a convenient location, ensure it is executable and add the following line to your crontab.&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;* * * * * /path/to/gmetric-mysql.sh &amp;gt; /dev/null &lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;&amp;gt;&amp;amp;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;As soon as this is done, your Ganglia web front-end should start plotting the new metrics.&lt;/p&gt;
&lt;img src=&quot;http://codeinthehole.com/app/images/ganglia-snapshot.jpeg&quot; alt=&quot;Sample Ganglia charts&quot; /&gt;

 
    </content:encoded>

    <pubDate>Sun, 14 Dec 2008 21:34:00 +0000</pubDate>
    <guid isPermaLink="false">http://codeinthehole.com/archives/8-guid.html</guid>
    <category>bash</category>
<category>ganglia</category>
<category>linux</category>
<category>monitoring</category>
<category>mysql</category>

</item>
<item>
    <title>Checking all MySQL tables</title>
    <link>http://codeinthehole.com/archives/10-Checking-all-MySQL-tables.html</link>
            <category>Tidbits</category>
    
    <comments>http://codeinthehole.com/archives/10-Checking-all-MySQL-tables.html#comments</comments>
    <wfw:comment>http://codeinthehole.com/wfwcomment.php?cid=10</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://codeinthehole.com/rss.php?version=2.0&amp;type=comments&amp;cid=10</wfw:commentRss>
    

    <author>nospam@example.com (David Winterbottom)</author>
    <content:encoded>
    &lt;p&gt;It&#039;s well known that MyISAM tables are prone to corruption and need to be regularly checked and repaired.  Moreover, in a production environment, it can be beneficial to run a daily check of all tables and mail news of any errors to an appropriate developer/DBA.&lt;/p&gt;
&lt;p&gt;There are two options for checking MySQL tables.  The most effective method is to run the &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/dev.mysql.com/doc/refman/5.0/en/myisamchk.html&#039;);&quot;  href=&quot;http://dev.mysql.com/doc/refman/5.0/en/myisamchk.html&quot; title=&quot;Manual page for myisamchk&quot;&gt;myisamchk utility&lt;/a&gt; directly on the index files (.MYI) of the tables in question (some simple shell expansion makes this easy):&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;myisamchk --silent --fast /path/to/datadir/*/*.MYI&lt;/div&gt;
&lt;p&gt;However, this proses a problem in that you must ensure that no other programs are accessing the tables while they are being checked.  Hence they must be locked, or better still, the MySQL daemon stopped before running any checks.  Perversely, if this is not done, the act of checking the tables can corrupt them.&lt;/p&gt;
&lt;p&gt;Another option is to use the CHECK TABLE syntax in SQL (which does not pose a risk of corruption).  There are various scripts (written in PHP and bash) posted on the &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/dev.mysql.com/doc/mysql/en/CHECK_TABLE.html&#039;);&quot;  href=&quot;http://dev.mysql.com/doc/mysql/en/CHECK_TABLE.html&quot; title=&quot;The manual page for CHECK TABLE&quot;&gt;CHECK TABLE manual page&lt;/a&gt; but this operation can be done easily through a single line:
&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;mysql -p&amp;lt;password&amp;gt; -D&amp;lt;database&amp;gt; -B -e &lt;span style=&quot;color: #ff0000;&quot;&gt;&quot;SHOW TABLES&quot;&lt;/span&gt; \&lt;br /&gt;| awk &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;{print &amp;quot;CHECK TABLE &amp;quot;$1&amp;quot;;&amp;quot;}&#039;&lt;/span&gt; \&lt;br /&gt;| mysql -p&amp;lt;password&amp;gt; -D&amp;lt;database&amp;gt;&lt;/div&gt;
&lt;p&gt;This dynamically creates a list of &quot;CHECK TABLE ...&quot; commands which is piped into MySQL for execution. 

&lt;p&gt;For checking a selection of tables rather than all, use the LIKE operator when selecting the tables to check:&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;mysql -p&amp;lt;password&amp;gt; -D&amp;lt;database&amp;gt; -B -e &lt;span style=&quot;color: #ff0000;&quot;&gt;&quot;SHOW TABLES LIKE &#039;User%&#039;&quot;&lt;/span&gt; \&lt;br /&gt;| awk &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;NR != 1 {print &amp;quot;CHECK TABLE &amp;quot;$1&amp;quot;;&amp;quot;}&#039;&lt;/span&gt; \&lt;br /&gt;| mysql -p&amp;lt;password&amp;gt; -D&amp;lt;database&amp;gt;&lt;/div&gt;
&lt;p&gt;This only checks the tables that start &#039;User&#039;.  Note that the awk program has an extra clause to ensure that the first line of MySQL output is skipped.&lt;/p&gt; 
    </content:encoded>

    <pubDate>Sun, 23 Nov 2008 11:30:40 +0000</pubDate>
    <guid isPermaLink="false">http://codeinthehole.com/archives/10-guid.html</guid>
    <category>bash</category>
<category>mysql</category>

</item>
<item>
    <title>Monitoring MySQL processes</title>
    <link>http://codeinthehole.com/archives/2-Monitoring-MySQL-processes.html</link>
            <category>Tidbits</category>
    
    <comments>http://codeinthehole.com/archives/2-Monitoring-MySQL-processes.html#comments</comments>
    <wfw:comment>http://codeinthehole.com/wfwcomment.php?cid=2</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://codeinthehole.com/rss.php?version=2.0&amp;type=comments&amp;cid=2</wfw:commentRss>
    

    <author>nospam@example.com (David Winterbottom)</author>
    <content:encoded>
    &lt;p&gt;Just a quick tip on monitoring the queries that mysql is handling on a production site.  You can use the mysqladmin tool to return a list of the processes currently being handled. Combining this with the UNIX watch command allows a real-time monitoring of what&#039;s going on. &lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;watch -n &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt; mysqladmin processlist&lt;/div&gt;
&lt;p&gt;The &quot;-n 1&quot; specifies that mysqladmin executes every second.  Depending on your set-up, you may need to specify a mysql user and password:&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;watch -n &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt; mysqladmin --&lt;span style=&quot;color: #0000ff;&quot;&gt;user=&lt;/span&gt;&amp;lt;user&amp;gt; --&lt;span style=&quot;color: #0000ff;&quot;&gt;password=&lt;/span&gt;&amp;lt;password&amp;gt; processlist&lt;/div&gt; 
    </content:encoded>

    <pubDate>Sun, 26 Oct 2008 22:06:00 +0000</pubDate>
    <guid isPermaLink="false">http://codeinthehole.com/archives/2-guid.html</guid>
    <category>bash</category>
<category>linux</category>
<category>mysql</category>

</item>

</channel>
</rss>