<?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>Saiweb &#187; mySQL</title>
	<atom:link href="http://www.saiweb.co.uk/tag/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.saiweb.co.uk</link>
	<description>Ramblings of a Sys admin</description>
	<lastBuildDate>Mon, 06 Feb 2012 14:57:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PenTesting &#8211; mySQL password hash generation and lookup</title>
		<link>http://www.saiweb.co.uk/security/pentesting-mysql-password-hash-generation-and-lookup</link>
		<comments>http://www.saiweb.co.uk/security/pentesting-mysql-password-hash-generation-and-lookup#comments</comments>
		<pubDate>Wed, 17 Aug 2011 12:20:04 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[hashdump]]></category>
		<category><![CDATA[hashing]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[old_passwords]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/?p=1104</guid>
		<description><![CDATA[One of the worst things you can have in any secure system is a user with a simple password, no matter what steps you take to protect your data, if a privileged user is using a simple password, it&#8217;s akin to having a safe door that&#8217;s glass window. First off we need to take a [...]]]></description>
			<content:encoded><![CDATA[<p>One of the worst things you can have in any secure system is a user with a simple password, no matter what steps you take to protect your data, if a privileged user is using a simple password, it&#8217;s akin to having a safe door that&#8217;s glass window.</p>
<p>First off we need to take a hash dump:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysql -Bse 'select distinct(password),user from mysql.user;' &gt; hashdump.txt</div></td></tr></tbody></table></div>
<p>Now of course you can do the same using SQL Injection etc (WHY when you have SQLi already? duh privilege escalation!) , I&#8217;m going to cover this from the perspective that you are the administrator looking to strengthen your security &#8230;</p>
<p>Now you have your hashdump you need a hash table with the equivelent passwords within it, for this you will need 2 things</p>
<p>1. A dictionary file<br />
2. <a href="https://github.com/Oneiroi/PenTesting/blob/master/crypto/generators/mysql/csv_gen.py">https://github.com/Oneiroi/PenTesting/blob/master/crypto/generators/mysql/csv_gen.py</a></p>
<p>The python script above I wrote to use multiprocessing to map words onto the hash function, and I have had it grind through mySQL hashes at a rate of ~98k per second, there is no &#8220;lookup&#8221; script at this time though one is currently being written.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">./csv_gen.py -f /path/to/wordlist.txt -o /output/path/to/output.csv -t &lt;max threads, default 1&gt; [-l optional use legacy hash]</div></td></tr></tbody></table></div>
<p>once this has ground through your wordlist you will have a CSV file, which will be in the format <hash>,
<password>
the script defaults to the new PASSWORD() function, if you are using old_password=1 in your configuration then pass the -l flag to use legacy hashing instead.</p>
<p>ok let&#8217;s assume the following fictional scenario </p>
<p>1. old_passwords is in use, and we want chip&#8217;s password<br />
2. 077b91e3491e2fdd	chip<br />
3.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">grep 077b91e3491e2fdd output.txt<br />
077b91e3491e2fdd,a</div></td></tr></tbody></table></div>
<p>4. Chip has a password that is just he letter &#8220;a&#8221; which he will tell you is the best password ever &#8230;</p>
<p>And that&#8217;s about a simple as it gets you generate a set of hashes and you compare known hashes to your generate set to see if you can discern simple passwords, hopefully going on then to chastise the user and instructing them on proper password etiquette, there are more complicated methods of getting the password from the hash, in the case of old_passwords I believe it is possible to reverse the hash to get the original string for one (so don&#8217;t use old_passwords!)</p>
<p>If you go on to use my python scripts, please let me know how they perform, my test were carried out using an intel i5, I&#8217;d love to know how they perform on other CPUs.</p>
<p><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/security/pentesting-mysql-password-hash-generation-and-lookup" data-text="PenTesting &#8211; mySQL password hash generation and lookup" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fsecurity%2Fpentesting-mysql-password-hash-generation-and-lookup&#038;text=PenTesting%20%26%238211%3B%20mySQL%20password%20hash%20generation%20and%20lookup" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/security/pentesting-mysql-password-hash-generation-and-lookup/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mySQL &gt;= 5.0.37 community profiling SQL queries.</title>
		<link>http://www.saiweb.co.uk/mysql/mysql-5-0-37-community-profiling-sql-queries</link>
		<comments>http://www.saiweb.co.uk/mysql/mysql-5-0-37-community-profiling-sql-queries#comments</comments>
		<pubDate>Thu, 15 Jul 2010 19:30:06 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[mySQL]]></category>
		<category><![CDATA[5.0.37]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[slow]]></category>
		<category><![CDATA[slow query]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/?p=885</guid>
		<description><![CDATA[Whilst there indeed seems to be a veritable plethora of SQL profiling / benchmarking tools, most of them with insane commercial license fees (&#62;= $400 per annum on most) I have found it intriguing that as of mySQL community edition &#62;= 5.0.37 mySQL offers an inbuilt method for profiling SQL queries, as can be see [...]]]></description>
			<content:encoded><![CDATA[<p>Whilst there indeed seems to be a veritable plethora of SQL profiling / benchmarking tools, most of them with insane commercial license fees (&gt;= $400 per annum on most)</p>
<p>I have found it intriguing that as of mySQL community edition &gt;= 5.0.37 mySQL offers an inbuilt method for profiling SQL queries, as can be see <a href="http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html">here</a> the downside is that this is session based, meaning it can only provide profiling information for the current connection, almost useless for trying to profile a running web app (that is without code modification to set profiling and harvest the data).</p>
<p>However it can be useful if you have a known slow query.</p>
<p>So lets work on the basis that we have a known slow SQL query we&#8217;d like profiling information for,</p>
<p>check to see if profiling is enabled:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> &nbsp;@@profiling;</div></td></tr></tbody></table></div>
<p>The returned value is generally 0 so lets enable it.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SET</span> profiling_history_size<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">100</span>;<br />
<span style="color: #993333; font-weight: bold;">SET</span> profiling<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>;</div></td></tr></tbody></table></div>
<p>This tells mySQL to retain the profile of 100 queries in memory, and to enable profiling.</p>
<p>Now at this point this can also be used to diagnose slow loading datases, simply</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #66cc66;">&lt;</span>dbname<span style="color: #66cc66;">&gt;</span>;<br />
<span style="color: #993333; font-weight: bold;">SHOW</span> profiles;<br />
<span style="color: #993333; font-weight: bold;">SHOW</span> profile <span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #cc66cc;">1</span>;</div></td></tr></tbody></table></div>
<p>Upon running the above you will now be using your database and will see an output similar to</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">+----------+------------+-------------------+<br />
| Query_ID | Duration &nbsp; | Query &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
+----------+------------+-------------------+<br />
| &nbsp; &nbsp; &nbsp; &nbsp;1 | 0.00011400 | SELECT DATABASE() |<br />
| &nbsp; &nbsp; &nbsp; &nbsp;2 | 0.00048900 | show databases &nbsp; &nbsp;|<br />
| &nbsp; &nbsp; &nbsp; &nbsp;3 | 0.00026600 | show tables &nbsp; &nbsp; &nbsp; |<br />
+----------+------------+-------------------+</div></td></tr></tbody></table></div>
<p>Followed by</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">+----------------------+----------+<br />
| Status &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Duration |<br />
+----------------------+----------+<br />
| starting &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0.000053 |<br />
| checking permissions | 0.000004 |<br />
| Opening tables &nbsp; &nbsp; &nbsp; | 0.000009 |<br />
| init &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0.000011 |<br />
| optimizing &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0.000004 |<br />
| executing &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000017 |<br />
| end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000003 |<br />
| end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000002 |<br />
| query end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000002 |<br />
| freeing items &nbsp; &nbsp; &nbsp; &nbsp;| 0.000005 |<br />
| logging slow query &nbsp; | 0.000002 |<br />
| cleaning up &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000002 |<br />
+----------------------+----------+</div></td></tr></tbody></table></div>
<p>In my case here nothing really eventful, lets assume for the moment you are using a wordpress database, and you have numerous posts</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> wp_posts <span style="color: #993333; font-weight: bold;">WHERE</span> ID <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">100</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span>ID<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> wp_posts <span style="color: #993333; font-weight: bold;">WHERE</span> ID <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">100</span></div></td></tr></tbody></table></div>
<p>in my case I got the following results:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0.00072600 | select count(*) from wp_posts where ID &amp;gt; 100<br />
0.00069900 | select count(ID) from wp_posts where ID &amp;gt; 100</div></td></tr></tbody></table></div>
<p>a simple demonstration showing the difference between a count() on an indexed field vs *, in this case the saving is ~4%.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SHOW</span> profiles;<br />
<span style="color: #993333; font-weight: bold;">SHOW</span> profile <span style="color: #993333; font-weight: bold;">FOR</span> query <span style="color: #66cc66;">&lt;</span>n<span style="color: #66cc66;">&gt;</span>;</div></td></tr></tbody></table></div>
<p>Will give you an output similar to:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">+--------------------+----------+<br />
| Status &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Duration |<br />
+--------------------+----------+<br />
| starting &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0.000079 |<br />
| Opening tables &nbsp; &nbsp; | 0.000014 |<br />
| System lock &nbsp; &nbsp; &nbsp; &nbsp;| 0.000005 |<br />
| Table lock &nbsp; &nbsp; &nbsp; &nbsp; | 0.000008 |<br />
| init &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0.000025 |<br />
| optimizing &nbsp; &nbsp; &nbsp; &nbsp; | 0.000012 |<br />
| statistics &nbsp; &nbsp; &nbsp; &nbsp; | 0.000049 |<br />
| preparing &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000012 |<br />
| executing &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000006 |<br />
| Sending data &nbsp; &nbsp; &nbsp; | 0.000461 |<br />
| end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000004 |<br />
| end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000003 |<br />
| query end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 0.000003 |<br />
| freeing items &nbsp; &nbsp; &nbsp;| 0.000007 |<br />
| closing tables &nbsp; &nbsp; | 0.000005 |<br />
| logging slow query | 0.000003 |<br />
| cleaning up &nbsp; &nbsp; &nbsp; &nbsp;| 0.000003 |<br />
+--------------------+----------+<br />
17 rows in set (0.00 sec)</div></td></tr></tbody></table></div>
<p>this is very similar to a <a href="http://en.wikipedia.org/wiki/Strace">stack trace</a> you may run on a problematic script, or <a href="http://www.xdebug.org">xdebug</a> + <a href="http://code.google.com/p/webgrind/">webgrind</a>, and will gain futher insight into your SQL should <a href="http://dev.mysql.com/doc/refman/5.0/en/explain.html">EXPLAIN</a> no give you enough of an insight.</p>
<p>I&#8217;ll post more information on this as I get time to work with it more, this is still knew to me, and aside from knowing how to use it I know relatively little about this profiling functionality, please feel free to post references / examples in the comments.</p>
<p>Cheers</p>
<p>Buzz<span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/mysql/mysql-5-0-37-community-profiling-sql-queries" data-text="mySQL >= 5.0.37 community profiling SQL queries.&#8221; href=&#8221;http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fmysql%2Fmysql-5-0-37-community-profiling-sql-queries&#038;text=mySQL%20%3E%3D%205.0.37%20community%20profiling%20SQL%20queries.&#8221; >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/mysql/mysql-5-0-37-community-profiling-sql-queries/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql csv export scripting using TCL and expect</title>
		<link>http://www.saiweb.co.uk/linux/mysql-csv-export-scripting-using-tcl-and-expect</link>
		<comments>http://www.saiweb.co.uk/linux/mysql-csv-export-scripting-using-tcl-and-expect#comments</comments>
		<pubDate>Mon, 05 Jul 2010 15:06:15 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[expect]]></category>
		<category><![CDATA[tcl]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/?p=880</guid>
		<description><![CDATA[I&#8217;ve no idea to this day why my bash script would not work with a CSV export from mysql by simply using mysql -e &#8220;SQL COMMAND HERE&#8221;. So I had to come up with a workaround quickly. This lead to using expect, scripting in this method can be used for numerous purposes, I am currently [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve no idea to this day why my bash script would not work with a CSV export from mysql by simply using mysql -e &#8220;SQL COMMAND HERE&#8221;.</p>
<p>So I had to come up with a workaround quickly.</p>
<p>This lead to using <a href="http://linux.die.net/man/1/expect">expect</a>, scripting in this method can be used for numerous purposes, I am currently in the process of writing a few test scripts using tcl and this package for pop,imap,smtp testing.</p>
<div class="codecolorer-container tcl default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="tcl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">#!/usr/bin/expect -f</span><br />
<span style="color: #ff7700;font-weight:bold;">set</span> DB <span style="color: #483d8b;">&quot;&lt;database&gt;&quot;</span><br />
<span style="color: #ff7700;font-weight:bold;">set</span> USER <span style="color: #483d8b;">&quot;&lt;user&gt;&quot;</span><br />
<span style="color: #ff7700;font-weight:bold;">set</span> PASS <span style="color: #483d8b;">&quot;&lt;password&gt;&quot;</span><br />
<br />
spawn mysql -u <span style="color: #ff3333;">$USER</span> -p <span style="color: #ff3333;">$DB</span><br />
match_max <span style="color: #ff4500;">100000</span><br />
expect -exact <span style="color: #483d8b;">&quot;assword: &quot;</span><br />
send -- <span style="color: #483d8b;">&quot;$PASS<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">set</span> SQL <span style="color: #483d8b;">&quot;SELECT * INTO OUTFILE '/tmp/csvfile.csv' FROM table&quot;</span><span style="color: #66cc66;">;</span><br />
<br />
expect -exact <span style="color: #483d8b;">&quot;mysql&gt; &quot;</span><br />
send -- <span style="color: #483d8b;">&quot;$SQL;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><br />
expect -exact <span style="color: #483d8b;">&quot;mysql&gt; &quot;</span><br />
sent -- <span style="color: #483d8b;">&quot;exit;/r&quot;</span></div></td></tr></tbody></table></div>
<p>Pretty simple realy once you have the hang of it, you tell it what to expect and what to reply with, there are more advanced methods going on from here, including conditional sends based on response.</p>
<p>I&#8217;ll be covering those soon.<span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/linux/mysql-csv-export-scripting-using-tcl-and-expect" data-text="mysql csv export scripting using TCL and expect" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Flinux%2Fmysql-csv-export-scripting-using-tcl-and-expect&#038;text=mysql%20csv%20export%20scripting%20using%20TCL%20and%20expect" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/linux/mysql-csv-export-scripting-using-tcl-and-expect/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting mySQL latin1 to utf8</title>
		<link>http://www.saiweb.co.uk/mysql/converting-mysql-latin1-to-utf8</link>
		<comments>http://www.saiweb.co.uk/mysql/converting-mysql-latin1-to-utf8#comments</comments>
		<pubDate>Tue, 14 Jul 2009 08:46:02 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[mySQL]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[converting]]></category>
		<category><![CDATA[iconv]]></category>
		<category><![CDATA[latin-1]]></category>
		<category><![CDATA[latin1]]></category>
		<category><![CDATA[multibyte]]></category>
		<category><![CDATA[mysqldump]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[utf-8]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/?p=696</guid>
		<description><![CDATA[The problem We&#8217;ve all been in this position at some point, working for a company who wants to internationalize their website, and so their mySQL CMS data &#8230; But all is not so well as just using &#8216;SET NAMES utf8&#8242; and changing all &#8216;charset&#8217; on tables to utf8, You may fall foul of seeing content [...]]]></description>
			<content:encoded><![CDATA[<ul>
<strong>The problem</strong></ul>
<p>We&#8217;ve all been in this position at some point, working for a company who wants to internationalize their website, and so their mySQL CMS data &#8230;</p>
<p>But all is not so well as just using &#8216;SET NAMES utf8&#8242; and changing all &#8216;charset&#8217; on tables to utf8,</p>
<p>You may fall foul of seeing content like &#193;&pound;</p>
<p>This is due to the fact in this case the latin1 encoded £ has not properly been converted to utf8 and as such is not rendering correctly, this is true of most &#8216;multibyte&#8217; characters.</p>
<ul>
<strong>The solution</strong></ul>
<p>What you need to do is actually convert the character set to utf8, in order to do this your going to need to run it through a program you could use iconv if you are already familiar with it, however if your system has python installed you can grab a copy of my <a href="http://www.saiweb.co.uk/sysadmin">sysadmin</a> program which has iconv like functionality but is far more user friendly.</p>
<ul>
<strong>What you will need</strong></ul>
<ul>
<li>Text Editor (vi/nano/pico/emacs)</li>
<li>Python 2.4 or higher</li>
<li><a href="http://linux.about.com/od/commands/l/blcmdl1_sed.htm">SED</a> package</li>
<li><a href="http://www.saiweb.co.uk/sysadmin">Sysadmin program</a></li>
<li>mySQL</li>
</ul>
<ul>
<strong>Preparing the file</strong></ul>
<p>This assumes the database is currently using latin1, in theory this could be any encoding.</p>
<p>Get a dump of the database:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysqldump <span style="color: #660033;">--set-character-set</span>=latin-<span style="color: #000000;">1</span> <span style="color: #660033;">--set-charset</span> <span style="color: #660033;">-u</span> user <span style="color: #660033;">-pPASSWORD</span> databasename <span style="color: #000000; font-weight: bold;">&gt;</span> databasename-latin1.sql</div></td></tr></tbody></table></div>
<p>Now you have to be aware of what you need to replace using SED, you can&#8217;t just replace all instances of &#8216;latin1&#8242; as <a href="http://en.wikipedia.org/wiki/Murphy%27s_law">Murphy&#8217;s law</a> being as it is means that somewhere there will be &#8216;latin1&#8242; in the physical content, especially for instance if I was using a mysql dump from this blog.</p>
<p>As such you need to replace the following:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">/*!</span><span style="color: #000000;">40101</span> SET NAMES latin1 <span style="color: #000000; font-weight: bold;">*/</span>;</div></td></tr></tbody></table></div>
<p>If your database dump is small enough (sub 100mb) you can edit this line directly in your text editor, alternatively you can do the following.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">cat</span> .<span style="color: #000000; font-weight: bold;">/</span>databasename-latin1.sql <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/SET NAMES latin1/SET NAMES utf8/g'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> tmp<br />
<span style="color: #c20cb9; font-weight: bold;">cat</span> .<span style="color: #000000; font-weight: bold;">/</span>tmp <span style="color: #000000; font-weight: bold;">&gt;</span> .<span style="color: #000000; font-weight: bold;">/</span>databasename-latin1.sql<br />
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> .<span style="color: #000000; font-weight: bold;">/</span>tmp</div></td></tr></tbody></table></div>
<p>Now you need to replace all instances of &#8216;CHARSET=latin1&#8242;</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">cat</span> .<span style="color: #000000; font-weight: bold;">/</span>databasename-latin1.sql <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/CHARSET=latin1/CHARSET=utf8/g'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> tmp<br />
<span style="color: #c20cb9; font-weight: bold;">cat</span> .<span style="color: #000000; font-weight: bold;">/</span>tmp <span style="color: #000000; font-weight: bold;">&gt;</span> .<span style="color: #000000; font-weight: bold;">/</span>databasename-latin1.sql<br />
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> .<span style="color: #000000; font-weight: bold;">/</span>tmp</div></td></tr></tbody></table></div>
<p>Now we have to run the file through the charset converter</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sysadmin <span style="color: #660033;">-c</span> iconv <span style="color: #660033;">-d</span> .<span style="color: #000000; font-weight: bold;">/</span>databasename-latin1.sql,latin-<span style="color: #000000;">1</span>,utf-<span style="color: #000000;">8</span></div></td></tr></tbody></table></div>
<p>If your sql dump is over 30mb, you will be prompted to confirm you wish to proceed, please remember that this will load the entire file into memory, so ensure you have enough available system memory before proceeding, I also suggest not running this on a production server.</p>
<p>If any characters could not be converted you will be alerted to their exact position within the file, from there you will either need to use sed to replace the character or use your text editor.</p>
<p>If all went well you now have ./databasename-latin1.sql.utf-8 (note the utf-8 extension), you now have a complete utf8 mySQL dump, all you need do now is import the dump.</p>
<p><strong>
<ul>Further reading</ul>
<p></strong></p>
<ol>
<li><a href="http://www.saiweb.co.uk/mysql/mysql-forcing-utf-8-compliance-for-all-connections">Force mySQL utf8 connections</a></li>
<li><a href="http://www.saiweb.co.uk/mysql/mysql-bash-backup-script">mySQL backup script</a></li>
</ol>
<p><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/mysql/converting-mysql-latin1-to-utf8" data-text="Converting mySQL latin1 to utf8" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fmysql%2Fconverting-mysql-latin1-to-utf8&#038;text=Converting%20mySQL%20latin1%20to%20utf8" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/mysql/converting-mysql-latin1-to-utf8/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>High Availability mySQL binlog position comparrison woes</title>
		<link>http://www.saiweb.co.uk/nagios/high-availability-mysql-binlog-position-comparrison-woes</link>
		<comments>http://www.saiweb.co.uk/nagios/high-availability-mysql-binlog-position-comparrison-woes#comments</comments>
		<pubDate>Tue, 16 Jun 2009 14:10:19 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[mySQL]]></category>
		<category><![CDATA[Nagios]]></category>
		<category><![CDATA[high availability]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/?p=666</guid>
		<description><![CDATA[I ment to note this down yesterday but everything is going ten to the dozen at the moment. basically I have now authored a nagios addon for monitoring master-master replication between two servers, this carries out 4 stages of checks Validates all required data is passed by servers Slave IO is running on both servers [...]]]></description>
			<content:encoded><![CDATA[<p>I ment to note this down yesterday but everything is going ten to the dozen at the moment.</p>
<p>basically I have now authored a nagios addon for monitoring master-master replication between two servers, this carries out 4 stages of checks</p>
<ol>
<li>Validates all required data is passed by servers</li>
<li>Slave IO is running on both servers</li>
<li>Seconds_Behind_Master check, args can be passed to vary warn and critical thresholds</li>
<li>(slave) Master_Log_File == (master) File</li>
</ol>
<p>The 5th check was a comparison on the binlog positions themselves, comparing (slave) Read_Master_Log_Pos and (master) Position</p>
<p>Here in lies the problem, which took a while to track down, the problem is that no matter what I tried the slave was <strong>ALWAYS</strong> behind the master position &#8230; but why?</p>
<p>The reason is why I designed the High Availability solution in the first place &#8230; Very high traffic level, in the region of 20,800 transactions per second.</p>
<p>Why was this the problem? the two queries run to gather the data are done sequentially per server, using the python time library I was able to find that there is a 0.02s interval between gathering datasets (20 milliseconds) &#8230; in that time 416 transactions had take place.</p>
<p>i.e.</p>
<p>time: binlog pos</p>
<p>Slave A</p>
<p>0.000: 100</p>
<p>Master B</p>
<p>0.020: 516</p>
<p>This unfortunately has now lead to some 32 lines of code being commented out, as I can see no way to reliably use the binlog positions for monitoring the replication in this situation, if any delay occurs anywhere at any point during the dataset collection i.e. network latency, delay in query processing due to traffic peak on one server &#8230; etc. the collected samples will always be different</p>
<p>The only way I ever see this working is if you can validate that the datasets came from the same exact point in time down to the nanosecond, this however is again not possible, on the network the servers currently reside there is a 0.13 millisecond ping response time this works out to 13,000 nanoseconds (0.00013 * 10^9)</p>
<p>If anyone has any theories on how to overcome this please let me know.</p>
<p>NOTE: At present due to the programming of this addon being done during working hours the nagios addons are not for public release at this time, this may be subject to change in the future should my employers allow their release.</p>
<p><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/nagios/high-availability-mysql-binlog-position-comparrison-woes" data-text="High Availability mySQL binlog position comparrison woes" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fnagios%2Fhigh-availability-mysql-binlog-position-comparrison-woes&#038;text=High%20Availability%20mySQL%20binlog%20position%20comparrison%20woes" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/nagios/high-availability-mysql-binlog-position-comparrison-woes/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mySQL bash backup script</title>
		<link>http://www.saiweb.co.uk/mysql/mysql-bash-backup-script</link>
		<comments>http://www.saiweb.co.uk/mysql/mysql-bash-backup-script#comments</comments>
		<pubDate>Tue, 31 Mar 2009 09:06:33 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[Bash Script]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[gzip]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/?p=622</guid>
		<description><![CDATA[In on of those &#8220;oh ffs&#8221; moments I found myself writing a BASH script to quickly dump all database on a mySQL server. 12345678910111213141516#!/bin/bash MYSQL=`which mysql`; MYSQLDUMP=`which mysqldump`; GZIP=`which gzip`; DEST=&#34;/path/to/dump/folder&#34; USER=&#34;root&#34;; PWD=&#34;XXXXXX&#34;; DBS=&#40;`$MYSQL &#160;-u $USER -p$PWD &#160;-Bse 'show databases'`&#41;; for db in ${DBS[@]}; do &#160; &#160; &#160; &#160; `$MYSQLDUMP --default-character-set=utf8 --set-charset -u $USER -p$PWD [...]]]></description>
			<content:encoded><![CDATA[<p>In on of those &#8220;oh ffs&#8221; moments I found myself writing a BASH script to quickly dump all database on a mySQL server.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/bin/bash</span><br />
<span style="color: #007800;">MYSQL</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> mysql<span style="color: #000000; font-weight: bold;">`</span>;<br />
<span style="color: #007800;">MYSQLDUMP</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> mysqldump<span style="color: #000000; font-weight: bold;">`</span>;<br />
<span style="color: #007800;">GZIP</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">gzip</span><span style="color: #000000; font-weight: bold;">`</span>;<br />
<span style="color: #007800;">DEST</span>=<span style="color: #ff0000;">&quot;/path/to/dump/folder&quot;</span><br />
<br />
<span style="color: #007800;">USER</span>=<span style="color: #ff0000;">&quot;root&quot;</span>;<br />
<span style="color: #007800;">PWD</span>=<span style="color: #ff0000;">&quot;XXXXXX&quot;</span>;<br />
<br />
<span style="color: #007800;">DBS</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$MYSQL</span> &nbsp;<span style="color: #660033;">-u</span> <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> &nbsp;<span style="color: #660033;">-Bse</span> <span style="color: #ff0000;">'show databases'</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">for</span> db <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${DBS[@]}</span>;<br />
<span style="color: #000000; font-weight: bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$MYSQLDUMP</span> <span style="color: #660033;">--default-character-set</span>=utf8 <span style="color: #660033;">--set-charset</span> <span style="color: #660033;">-u</span> <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> <span style="color: #007800;">$db</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #007800;">$GZIP</span> <span style="color: #660033;">-9</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$DEST</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$db</span>.sql.gz<span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$db</span> - DONE&quot;</span>;<br />
<span style="color: #000000; font-weight: bold;">done</span>;</div></td></tr></tbody></table></div>
<p>This script gets a list of all databases, dumps them out with UTF8 encoding, and gzip compresses the SQL file into the given &#8220;DEST&#8221; folder.</p>
<p>If you want to skip over certain databases i.e. &#8220;mysql&#8221;</p>
<p>Change this line:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007800;">DBS</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$MYSQL</span> &nbsp;<span style="color: #660033;">-u</span> <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> &nbsp;<span style="color: #660033;">-Bse</span> <span style="color: #ff0000;">'show databases'</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>To:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007800;">DBS</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$MYSQL</span> &nbsp;<span style="color: #660033;">-u</span> <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> &nbsp;<span style="color: #660033;">-Bse</span> <span style="color: #ff0000;">'show databases'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;database_to_exclude&quot;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>Or for multiple exclusions</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007800;">DBS</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$MYSQL</span> &nbsp;<span style="color: #660033;">-u</span> <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> &nbsp;<span style="color: #660033;">-Bse</span> <span style="color: #ff0000;">'show databases'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;database_to_exclude&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;another_database_to_exclude&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;etc&quot;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>I may re-write this in Python, if I get time.</p>
<p><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/mysql/mysql-bash-backup-script" data-text="mySQL bash backup script" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fmysql%2Fmysql-bash-backup-script&#038;text=mySQL%20bash%20backup%20script" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/mysql/mysql-bash-backup-script/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>mySQL slow query log rotation</title>
		<link>http://www.saiweb.co.uk/mysql/mysql-slow-query-log-rotation</link>
		<comments>http://www.saiweb.co.uk/mysql/mysql-slow-query-log-rotation#comments</comments>
		<pubDate>Mon, 16 Mar 2009 11:50:11 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[mySQL]]></category>
		<category><![CDATA[log rotation]]></category>
		<category><![CDATA[slow query]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/?p=613</guid>
		<description><![CDATA[One of the issues facing log rotation in mySQL is that mySQL doesn&#8217;t seem to have the ability to perform a &#8220;reload&#8221;. Meaning standard methods of rotating logs using logrotate leave mySQL logging an error in the syslog saying that the log file could not be found, and refusing to to any logging until the [...]]]></description>
			<content:encoded><![CDATA[<p>One of the issues facing log rotation in mySQL is that mySQL doesn&#8217;t seem to have the ability to perform a &#8220;reload&#8221;.</p>
<p>Meaning standard methods of rotating logs using logrotate leave mySQL logging an error in the syslog saying that the log file could not be found, and refusing to to any logging until the server is restarted, not fun if like me you manage high availability solutions and restarting a service is never the best option.</p>
<p>As such I have written this log rotate script, this does however make some assumptions.</p>
<ol>
<li>Your mysql user is &#8220;mysql&#8221;</li>
<li>Your mysql slow query log is /var/log/mysql-slow.log</li>
</ol>
<p>The script is written to perform the following actions:</p>
<ol>
<li>Rotate weekly</li>
<li>Retain 3 rotations (3 files + live log)</li>
<li>Compress on rotate (gzip)</li>
<li>Create new logfile with 660 permissions chowned to mysql:mysql</li>
<li>Run: mysqladmin flush-logs</li>
</ol>
<p><strong>Please be aware that the flush logs command will also rotate any binary logging currently in place, please ensure this will not adversely affect your deployment prior to use </strong></p>
<p><strong>Please ensure you carry out your own testing prior to deploying this script into a live environment.</strong></p>
<p><a href="http://svn.saiweb.co.uk/branches/linux-the-sysadmin-script/branches/logrotate.d/mysql">http://svn.saiweb.co.uk/branches/linux-the-sysadmin-script/branches/logrotate.d/mysql</a></p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#</span><br />
<span style="color: #666666; font-style: italic;"># mySQL slow log rotation script by D.Busby</span><br />
<span style="color: #666666; font-style: italic;"># place this script in /etc/logrotate.d/ or your appropriate logrotate dir.</span><br />
<span style="color: #666666; font-style: italic;"># http://creativecommons.org/licenses/by-sa/2.0/uk/ CC BY-SA</span><br />
<span style="color: #666666; font-style: italic;">#</span><br />
<span style="color: #666666; font-style: italic;"># NOTE: if you are reliant on binlogs i.e. for replication, 'flush logs' closes the current binlog and moves to the next itteration of log files</span><br />
<span style="color: #666666; font-style: italic;"># You should test this does not cause an issue with your deployment before using this script</span><br />
<span style="color: #666666; font-style: italic;"># </span><br />
<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>mysql-slow.log <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; weekly<br />
&nbsp; &nbsp; rotate <span style="color: #000000;">3</span><br />
&nbsp; &nbsp; compress<br />
&nbsp; &nbsp; missingok<br />
&nbsp; &nbsp; notifempty<br />
&nbsp; &nbsp; sharedscripts<br />
&nbsp; &nbsp; create <span style="color: #000000;">660</span> mysql mysql<br />
&nbsp; &nbsp; postrotate<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mysqladmin flush-logs<br />
&nbsp; &nbsp; endscript<br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<p><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/mysql/mysql-slow-query-log-rotation" data-text="mySQL slow query log rotation" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fmysql%2Fmysql-slow-query-log-rotation&#038;text=mySQL%20slow%20query%20log%20rotation" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/mysql/mysql-slow-query-log-rotation/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>High Availability Joomla, WordPress &#8211; Load balance persistant PHP database sessions</title>
		<link>http://www.saiweb.co.uk/php/high-availability-joomla-wordpress-load-balance-persistant-php-database-sessions</link>
		<comments>http://www.saiweb.co.uk/php/high-availability-joomla-wordpress-load-balance-persistant-php-database-sessions#comments</comments>
		<pubDate>Thu, 27 Nov 2008 16:27:09 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[mySQL]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[high availability]]></category>
		<category><![CDATA[joomla]]></category>
		<category><![CDATA[load balance]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/php/high-availability-joomla-wordpress-load-balance-persistant-php-database-sessions</guid>
		<description><![CDATA[If you&#8217;ve seen the new twitter feed to the right you may of seem some ramblings about &#8216;cura&#8217;. Cura is a PHP class I have authored in Co-operation with Psycle Interactive (The company I now work for, so be sure to thank them for allowing me to publish this write up!) So what does it [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve seen the new twitter feed to the right you may of seem some ramblings about &#8216;cura&#8217;.</p>
<p>Cura is a PHP class I have authored in Co-operation with <a href="http://www.psycle.com/en/">Psycle Interactive</a> (The company I now work for, so be sure to thank them for allowing me to publish this write up!)</p>
<p><strong>So what does it do?</strong><br />
Cura sets several call back objects in your PHP application that re-directs all session data to a mySQL database.</p>
<p><strong>But why do I need that?</strong><br />
The average 1 server end user can stop reading here, as I can tell you now that Cura is not for you.</p>
<p>If however you are a business fielding multiple web servers then read on.</p>
<p>By passing all your PHP sessions to a database you remove the work around requirements for a load balanced solution.</p>
<p>i.e. web1 web2</p>
<p>1) Shopper arrives at web1 and logs in.<br />
2) Shopper adds item to cart, which is logged against their session.<br />
3) web1 is subjected to a search engine index.<br />
4) web2 is now serving the shopper, shoppers basket is now logged out as their session id has changed &#8230;</p>
<p>There are numerous work around methods for this, such as having a single shared mount point for the PHP session files, the use of cookies etc &#8230;</p>
<p>The problem is in a high availability solution that a single mount point is just that, it&#8217;s singular and therefor a single point of failure.</p>
<p>Then there is the use of cookies, which is fine until you start to store a lot of data during your users session, at which point on each server change you are reliant on the cookie data being transmitted back to the server each time, raising the question what is the point of adding a load balanced solution if the user experience becomes degraded due to it&#8217;s deployment?</p>
<p>So secret option number 3 is to use a database, you can remove the single point of failure by having a mySQL cluster, and you haven&#8217;t got to worry too much about how much data you are storing.</p>
<p>Because everything is in a database whenever your web application is run (web1, web2) it will read the data from one source, allowing persistent sessions across your whole platform without the need for single mount points or session replication.</p>
<p>The source files are available from: <a href="http://svn.saiweb.co.uk/branches/cura-php/trunk">http://svn.saiweb.co.uk/branches/cura-php/trunk/</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">svn co http://svn.saiweb.co.uk/branches/cura-php/trunk/</div></td></tr></tbody></table></div>
<p>To deploy this solution simply add the following lines to any file that calls session_start();</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">require_once('/path/to/cura.class.php');<br />
$cura = new cura($db, $user, $password, $host);<br />
session_start();<br />
...<br />
the rest of your file...</div></td></tr></tbody></table></div>
<p>Ensure that you have created a &#8216;sessions&#8217; table as per the provided sessions.sql file in your database.</p>
<p>I will be adding simplified support for wordpress and joomla shortly these will become available from: <a href="http://svn.saiweb.co.uk/branches/cura-php/trunk/">http://svn.saiweb.co.uk/branches/cura-php/trunk/</a><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/php/high-availability-joomla-wordpress-load-balance-persistant-php-database-sessions" data-text="High Availability Joomla, WordPress &#8211; Load balance persistant PHP database sessions" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fphp%2Fhigh-availability-joomla-wordpress-load-balance-persistant-php-database-sessions&#038;text=High%20Availability%20Joomla%2C%20WordPress%20%26%238211%3B%20Load%20balance%20persistant%20PHP%20database%20sessions" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/php/high-availability-joomla-wordpress-load-balance-persistant-php-database-sessions/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>mySQL forcing utf-8 compliance for all connections.</title>
		<link>http://www.saiweb.co.uk/mysql/mysql-forcing-utf-8-compliance-for-all-connections</link>
		<comments>http://www.saiweb.co.uk/mysql/mysql-forcing-utf-8-compliance-for-all-connections#comments</comments>
		<pubDate>Wed, 12 Nov 2008 10:05:45 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[mySQL]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[init_connect]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[utf-8]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/mysql/mysql-forcing-utf-8-compliance-for-all-connections</guid>
		<description><![CDATA[The problem that most people face when setting up a UTF-8 database in mySQL is that without calling &#8216;SET NAMES&#8217; in the mySQL client prior to issuing any queries (PHP, C++ etc &#8230;) that the client connection will actually in most cases default to  latin-1. However as of mySQL 5.x or higher you can issue [...]]]></description>
			<content:encoded><![CDATA[<p>The problem that most people face when setting up a UTF-8 database in mySQL is that without calling &#8216;SET NAMES&#8217; in the mySQL client prior to issuing any queries (PHP, C++ etc &#8230;) that the client connection will actually in most cases default to  latin-1.</p>
<p>However as of mySQL 5.x or higher you can issue a statement in the my.cnf file calling init_connect.</p>
<p>This will trigger a series of defined commands / queries every time a non super user connects (So if you are using root to connect to your mySQL database, stop reading now and slap yourself HARD).</p>
<p>i.e.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[mysqld]<br />
init_connect='SET collation_connection = utf8_general_ci'<br />
init_connect='SET NAMES utf8'<br />
default-character-set=utf8<br />
character-set-server=utf8<br />
collation-server=utf8_general_ci<br />
skip-character-set-client-handshake</div></td></tr></tbody></table></div>
<p><strong>UPDATE 04/09/09</strong></p>
<p>my mySQL version 5.0.45 x64 only picks up the last entry of init_connect</p>
<p>Use this example in this case:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[mysqld]<br />
init_connect='SET collation_connection = utf8_general_ci; SET NAMES utf8;'<br />
default-character-set=utf8<br />
character-set-server=utf8<br />
collation-server=utf8_general_ci</div></td></tr></tbody></table></div>
<p>Restart mySQL and check the mysqld.log has not returned any errors (Or your event viewer if you are using windows).</p>
<p>Every client connection will now default to utf-8 encoding and not latin-1, removing the need to add a SET NAMES call on every connection.</p>
<p>This will work for PHP, C++, ruby etc&#8230; as the client encoding is now handeled server side, rather that waiting on the client to issue a SET NAMES command.</p>
<p><strong>UPDATE 30/03/09</strong>: Added &#8220;skip-character-set-client-handshake&#8221; this ignores the clients request to set the connection charset, this info courtesy of &#8220;wardo&#8221; <a href="http://word.wardosworld.com/?p=164 ">http://word.wardosworld.com/?p=164 </a></p>
<p><strong>UPDATE 10/09/09</strong></p>
<p>Been having some issues with this working the workaround is to add this config as a single line:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">init_connect='SET collation_connection = utf8_general_ci; SET NAMES utf8;'</div></td></tr></tbody></table></div>
<p><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/mysql/mysql-forcing-utf-8-compliance-for-all-connections" data-text="mySQL forcing utf-8 compliance for all connections." href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fmysql%2Fmysql-forcing-utf-8-compliance-for-all-connections&#038;text=mySQL%20forcing%20utf-8%20compliance%20for%20all%20connections." >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/mysql/mysql-forcing-utf-8-compliance-for-all-connections/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>mysql moving /var/lib/mysql and error: 121</title>
		<link>http://www.saiweb.co.uk/hacking/mysql-moving-varlibmysql-and-error-121</link>
		<comments>http://www.saiweb.co.uk/hacking/mysql-moving-varlibmysql-and-error-121#comments</comments>
		<pubDate>Mon, 28 Jul 2008 15:11:30 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[bind]]></category>
		<category><![CDATA[mount]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/hacking/mysql-moving-varlibmysql-and-error-121</guid>
		<description><![CDATA[The downside of a development server is &#8230; it&#8217;s for development. It is  not always cost effective to have the exact same setup as you you have in your production environment &#8230; Especially if you have a multi server setup. So I find myself today moving /var/lib/mysql &#8230; being as the OS drive is very [...]]]></description>
			<content:encoded><![CDATA[<p>The downside of a development server is &#8230; it&#8217;s for development.</p>
<p>It is  not always cost effective to have the exact same setup as you you have in your production environment &#8230;</p>
<p>Especially if you have a multi server setup.</p>
<p>So I find myself today moving /var/lib/mysql &#8230; being as the OS drive is very small, and pulling down a near 20GB database backup and then trying to rebuild the database on the same drive &#8230; well as you can imagine caused a few problems *doh*</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">/etc/init.d/mysql stop<br />
mv /var/lib/mysql /raid_5/</div></td></tr></tbody></table></div>
<p>So surely you just symlink &#8230; right?</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ln -s /raid_5/mysql /var/lib/mysql<br />
/etc/init.d/mysql start</div></td></tr></tbody></table></div>
<p>Well then answer would be no &#8230; upon importing the backup</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysql &amp;lt; backup.sql<br />
Can't create table './database/table.frm' (errno: 121)</div></td></tr></tbody></table></div>
<p>A nice errorno: 121</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">/etc/init.d/mysql stop<br />
rm -rf /var/lib/mysql<br />
mkdir /var/lib/mysql<br />
chown mysql:mysql /var/lib/mysql<br />
mount --bind /raid_5/mysql /var/lib/mysql<br />
/etc/init.d/mysql start</div></td></tr></tbody></table></div>
<p>et voila &#8230;</p>
<p>Data directory is relocated and the import working smoothly. Feel free to suggest any &#8220;cleaner&#8221; methods.</p>
<p><strong>UPDATE</strong>: Please rememeber to add the &#8216;mount&#8217; line into your rc.local otherwise when you reboot this mount will be gone!<span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/hacking/mysql-moving-varlibmysql-and-error-121" data-text="mysql moving /var/lib/mysql and error: 121" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fhacking%2Fmysql-moving-varlibmysql-and-error-121&#038;text=mysql%20moving%20%2Fvar%2Flib%2Fmysql%20and%20error%3A%20121" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/hacking/mysql-moving-varlibmysql-and-error-121/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc
Database Caching 21/62 queries in 0.078 seconds using apc
Object Caching 2520/2638 objects using apc
Content Delivery Network via Rackspace Cloud Files: cdn.saiweb.co.uk

Served from: www.saiweb.co.uk @ 2012-02-07 16:47:49 -->
