<?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>Technology: Learn and Share &#187; MySQL</title>
	<atom:link href="http://crazytoon.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://crazytoon.com</link>
	<description>Enterprise level solutions, LAMP, Linux, Apache, MySQL, PHP, Perl, Windows, Cache, Optimization</description>
	<lastBuildDate>Thu, 20 Oct 2011 20:56:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL:  How do you enable sphinxse (Sphinx Storage Engine) in your mysql installation?</title>
		<link>http://crazytoon.com/2008/08/05/mysql-how-do-you-enable-sphinxse-sphinx-storage-engine-in-your-mysql-installation/</link>
		<comments>http://crazytoon.com/2008/08/05/mysql-how-do-you-enable-sphinxse-sphinx-storage-engine-in-your-mysql-installation/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 22:21:32 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[System admin]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[fulltext search]]></category>
		<category><![CDATA[open source full text search engine]]></category>
		<category><![CDATA[Redhat]]></category>
		<category><![CDATA[RHEL]]></category>
		<category><![CDATA[sphinx]]></category>
		<category><![CDATA[sphinx storage engine]]></category>
		<category><![CDATA[sphinxse]]></category>

		<guid isPermaLink="false">http://crazytoon.com/?p=88</guid>
		<description><![CDATA[As you may know mysql fulltext search is not highly scalable.  One of the options to get around this scalability limitation, which I prefer, is to use Sphinx.   You can use Sphinx with out having to alter your mysql installation.  But, if you would like to use from within mysql and not have to [...]]]></description>
			<content:encoded><![CDATA[<p>As you may know mysql fulltext search is not highly scalable.  One of the options to get around this scalability limitation, which I prefer, is to use <a title="Sphinx Homepage" href="http://www.sphinxsearch.com/" target="_blank">Sphinx</a>.   You can use Sphinx with out having to alter your mysql installation.  But, if you would like to use from within mysql and not have to worry about how to pass data between Sphinx and MySQL, you can enable sphinxse (sphinx storage engine).  It is not included with mysql by default so you will have to compile it yourself.</p>
<p>Here are the instructions on how to get sphinxse compiled with your mysql installation on CentOS x64.  I am sure same instructions will work for other flavors but I have not tested it.  I will be compiling the most current version of sphinx (0.9.8) with most current stable version of mysql (5.0.51b) at the time of the writing.  Let&#8217;s get the appropriate packages first:</p>
<p><code>wget http://www.sphinxsearch.com/downloads/sphinx-0.9.8.tar.gz<br />
wget http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.51b.tar.gz/from/http://mysql.he.net/<br />
tar zxpf sphinx*<br />
tar zxpf mysql*</code></p>
<p>You will also need &#8220;bison&#8221;, &#8220;patch&#8221;, &#8220;automake&#8221; and &#8220;libtool&#8221; installed.  Let us just do a yum install for it.</p>
<p><code>yum -y install bison patch automake libtool</code></p>
<p><strong>NOTE</strong>:  if you don&#8217;t install bison, you will get the following error:<br />
<code>sed '/^#/ s|y\.tab\.c|sql_yacc.cc|' y.tab.c &gt;sql_yacc.cct &amp;&amp; mv sql_yacc.cct sql_yacc.cc<br />
sed: can't read y.tab.c: No such file or directory<br />
make[2]: *** [sql_yacc.cc] Error 2</code></p>
<p>Let us continue with patching mysql source with sphinx storage engine (sphinxse) code and compile/install our new binaries.</p>
<p><code>cd mysql*<br />
patch -p1 &lt; ../sphinx-0.9.8/mysqlse/sphinx.5.0.37.diff #Make sure everything succeeded.<br />
BUILD/autorun.sh<br />
mkdir sql/sphinx<br />
cp ../sphinx-0.9.8/mysqlse/* sql/sphinx<br />
./configure --prefix=/usr/local/mysql --with-sphinx-storage-engine<br />
make<br />
make install</code></p>
<p>Now start your mysql installtion and check if engine support is compiled in:</p>
<p><code>mysql&gt; show engines\G<br />
Engine: SPHINX<br />
Support: YES<br />
Comment: Sphinx storage engine 0.9.8</code></p>
<p>To read more about how to use Sphinx storage engine, please refer to:  <a title="Sphinx Documentation for using Sphinx Storage Engine" href="http://www.sphinxsearch.com/doc.html#sphinxse-using" target="_blank">Sphinx documentation for using sphinx storage engine</a></p>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/08/05/mysql-how-do-you-enable-sphinxse-sphinx-storage-engine-in-your-mysql-installation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MySQL:  Fix Microsoft Word characters.  Shows weird characters on the web page.</title>
		<link>http://crazytoon.com/2008/06/05/mysql-fix-microsoft-word-characters-shows-weird-characters-on-the-web-page/</link>
		<comments>http://crazytoon.com/2008/06/05/mysql-fix-microsoft-word-characters-shows-weird-characters-on-the-web-page/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 21:03:50 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Linux Apache MySQL PHP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Microsoft Word characters]]></category>

		<guid isPermaLink="false">http://crazytoon.com/?p=82</guid>
		<description><![CDATA[As a consultant, I do a lot of content migrations for clients.  One issue I run into quite often is the encoding of databases, tables, columns differs between source and destination.  Most clients do not want me to go and change the way their encoding is to fix issues since they are too [...]]]></description>
			<content:encoded><![CDATA[<p>As a consultant, I do a lot of content migrations for clients.  One issue I run into quite often is the encoding of databases, tables, columns differs between source and destination.  Most clients do not want me to go and change the way their encoding is to fix issues since they are too afraid about messing with production data.  Of course amongst other issues, it creates weird characters for data which is copied/pasted from Microsoft Word. You see weird characters like:   â€™ â€¦ â€“ â€œ â€ â€˜</p>
<p>So if you just want to replace these with appropriate symbols, you may do it with a simple sql query.  Note that below queries are without <strong>where </strong>clause.  You may what to test it with one of your rows before making changes to the whole table.  Of course, you should always backup your data before you try this out.  If you have a dev system, that is even better.  I put all my sql queries into a file ex: fix.sql and sourced it with mysql client.</p>
<p>vi fix.sql</p>
<p><code>update table_name set  fieldname = replace(fieldname, 'â€™', '\'');<br />
update table_name set  fieldname = replace(fieldname, 'â€¦','...');<br />
update table_name set  fieldname = replace(fieldname, 'â€“','-');<br />
update table_name set  fieldname = replace(fieldname, 'â€œ','"');<br />
update table_name set  fieldname = replace(fieldname, 'â€','"');<br />
update table_name set  fieldname = replace(fieldname, 'â€˜','\'');<br />
update table_name set  fieldname = replace(fieldname, 'â€¢','-');<br />
update table_name set  fieldname = replace(fieldname, 'â€¡','c');</code></p>
<p>Save/exit.</p>
<p><code># mysql<br />
mysql&gt; source fix.sql;<br />
</code><br />
I am not sure if I am missing any other chars.  If you know of any other chars, please comment with them and I will add on to the script here.</p>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/06/05/mysql-fix-microsoft-word-characters-shows-weird-characters-on-the-web-page/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL: How do you install innotop to monitor innodb in real time?</title>
		<link>http://crazytoon.com/2008/05/21/mysql-how-do-you-install-innotop-to-monitor-innodb-in-real-time/</link>
		<comments>http://crazytoon.com/2008/05/21/mysql-how-do-you-install-innotop-to-monitor-innodb-in-real-time/#comments</comments>
		<pubDate>Wed, 21 May 2008 09:06:11 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Linux Apache MySQL PHP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[innodb]]></category>
		<category><![CDATA[innotop]]></category>
		<category><![CDATA[mysql monitoring]]></category>

		<guid isPermaLink="false">http://crazytoon.com/?p=78</guid>
		<description><![CDATA[Innotop is a very useful tool to monitor innodb information in real time.  This tool is written by Baron Schwartz who is also an author of &#8220;High Performance MySQL, Second edition&#8221; book. [Side note: I highly recommend getting this book when it comes out (in June, 08?).  Other authors include: Peter Zaitsev, Jeremy [...]]]></description>
			<content:encoded><![CDATA[<p>Innotop is a very useful tool to monitor innodb information in real time.  This tool is written by Baron Schwartz who is also an author of &#8220;High Performance MySQL, Second edition&#8221; book. <small>[Side note: I highly recommend getting this book when it comes out (in June, 08?).  Other authors include: Peter Zaitsev, Jeremy Zawodny, Arjen Lentz, Vadim Tkachenko and Derek J. Balling.]</small> Quick summary of what innotop can monitor (from: http://innotop.sourceforge.net/):  InnoDB transactions and internals, queries and processes, deadlocks, foreign key errors, replication status, system variables and status and much more.</p>
<p>Following are the instructions on how to install innotop on CentOS x64/Fedora/RHEL (Redhat enterprise).  Most probably same instructions can be used on all flavors of Linux.  If not, leave me a comment and I will research a solution for you.  Let us start with downloading innotop.  I used version 1.6.0 which is the latest at the time of writing.</p>
<p><code>wget http://internap.dl.sourceforge.net/sourceforge/innotop/innotop-1.6.0.tar.gz</code></p>
<p>Now let us go ahead and unzip and create the MakeFile to get it ready for install</p>
<p><code>tar zxpf innotop-1.6.0.tar.gz<br />
cd innotop-1.6.0<br />
perl Makefile.PL</code></p>
<p>At this point if you get the following output, you are good to continue:</p>
<p><code>Checking if your kit is complete...<br />
Looks good<br />
Writing Makefile for innotop</code></p>
<p>If you get something similar to following, you will need to take care of the prerequisites:</p>
<p><code>Looks good<br />
Warning: prerequisite DBD::mysql 1 not found.<br />
Warning: prerequisite DBI 1.13 not found.<br />
Warning: prerequisite Term::ReadKey 2.1 not found.<br />
Writing Makefile for innotop</code></p>
<p>Just because they are warnings does not mean you ignore them.  So let us install those prerequisites.  We will use perl&#8217;s cpan shell to get this installed (visit my post on <a title="How do I install Perl modules? eg. Installing LWP module for Perl." href="http://crazytoon.com/2007/05/21/perl-how-do-i-install-perl-modules-eg-installing-lwp-module-for-perl/">how to install perl modules</a> for more details).  If it is your first time starting this up, you will have to answer some questions.  Defaults will work fine in all cases.</p>
<p><code>perl -MCPAN -eshell<br />
install Term::ReadKey<br />
install DBI<br />
install DBD::mysql</code></p>
<p>Note:  you must install DBI before you can install DBD::mysql.</p>
<p>If you get an error like following when you are installing DBD::mysql:</p>
<p><code>Error:  Can't load '/root/.cpan/build/DBD-mysql-4.007/blib/arch/auto/DBD/mysql/mysql.so' for module DBD::mysql: libmysqlclient.so.15: cannot open shared object file: No such file or directory at /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/DynaLoader.pm line 230.</code></p>
<p>You will have to create a symlink to the object file in your lib64 (or lib if you are not using x64 version) folder:</p>
<p><code>ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.15 /lib64/</code></p>
<p>Once all prerequisites are done, type  perl Makefile.PL and you should have no warnings.  Continue the install:</p>
<p><code>make install</code></p>
<p>At this point you should have innotop installed on your system.  Let us do some quick set up so you can start using innotop.  We start with configuring your .my.cnf to include connection directives.<br />
<code><br />
vi ~/.my.cnf</code></p>
<p>Add the following (edit to reflect your install) and save/exit</p>
<p><code>[mysql]<br />
port            = 3306<br />
socket          = /tmp/mysql.sock</code></p>
<p>Start up innotop by typing innotop at your shell prompt.  First prompt will ask you to &#8220;Enter a name:&#8221;.  I just put localhost since this will be used to connect locally.  Next prompt asks you about DSN entry.  I use:  DBI:mysql:;mysql_read_default_group=mysql</p>
<p>This tells innotop to read .my.cnf file and use group [mysql] directives.  Next prompt is optional (I just press enter).  Next two prompts you enter information if you need to.</p>
<p>At this point your innotop installation / testing is complete.  You can read <a title="man innotop - Man page for innotop" href="http://www.lamp-tips.com/man-pages/innotop/" target="_blank">man innotop</a> to get more details on how to use innotop.</p>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/05/21/mysql-how-do-you-install-innotop-to-monitor-innodb-in-real-time/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Oh dear MySQL slave, where did you put those rows?</title>
		<link>http://crazytoon.com/2008/05/17/mysql-missing-data-on-slave-mysql-replicatio/</link>
		<comments>http://crazytoon.com/2008/05/17/mysql-missing-data-on-slave-mysql-replicatio/#comments</comments>
		<pubDate>Sat, 17 May 2008 10:35:00 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Linux Apache MySQL PHP]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL backup]]></category>
		<category><![CDATA[uh...]]></category>

		<guid isPermaLink="false">http://crazytoon.com/?p=75</guid>
		<description><![CDATA[I need help from my fellow mysql users.  I know some of the people who read this are alot better then me with mysql so hopefully you can help  
So today we decided that we are going to migrate one of our master database servers to new hardware.  Since we got the hardware this [...]]]></description>
			<content:encoded><![CDATA[<p>I need help from my fellow mysql users.  I know some of the people who read this are alot better then me with mysql so hopefully you can help <img src='http://crazytoon.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So today we decided that we are going to migrate one of our master database servers to new hardware.  Since we got the hardware this morning and we wanted to move on to it asap, we decided that we will take our slave down, copy data from it, and bring it up on future master server.  At that point, we will let it run as slave to the current master server until its time for us to take it down.  Reason we did that instead of mysqldump/import was to avoid the lag mysqldump creates on our server.</p>
<p>After we did all this and put up the new master server, we started to notice odd issues.  After looking around and comparing old db with new, we found out that new db was missing data.  How it happened is beyond me and is the reason why I am writing this.  We never had issues with the slave which would cause data to be lost; so what happened to those missing rows?  Is this something which is common?  Can we not trust our slave enough to use it as master if master died?  Can we not run backups off the slave with confident that our data is protected and up to date so to keep load down on our master?  All these questions which keep me awake and wondering&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/05/17/mysql-missing-data-on-slave-mysql-replicatio/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>MySQL:  How do I dump each record from a table to a separate files in csv format?</title>
		<link>http://crazytoon.com/2008/05/12/mysql-how-do-i-dump-export-each-record-from-table-separate-files-in-csv-format/</link>
		<comments>http://crazytoon.com/2008/05/12/mysql-how-do-i-dump-export-each-record-from-table-separate-files-in-csv-format/#comments</comments>
		<pubDate>Mon, 12 May 2008 08:45:14 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Linux Apache MySQL PHP]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://crazytoon.com/?p=70</guid>
		<description><![CDATA[I honestly do not know why somebody would want to export each record from a table in to its&#8217; own files in a csv format.  I am sure people have their own reasons.  But since I got request from couple people, I figure I would post a solution here.  Same script can [...]]]></description>
			<content:encoded><![CDATA[<p>I honestly do not know why somebody would want to export each record from a table in to its&#8217; own files in a csv format.  I am sure people have their own reasons.  But since I got request from couple people, I figure I would post a solution here.  Same script can be used to dump the whole table in to one csv file as well, with little tweaking.  I will start with creating database with a table.  I then insert three rows with test data into the table just to show three separate files creation.</p>
<p><code>mysql&gt; CREATE DATABASE testdump;<br />
mysql&gt; USE testdump<br />
mysql&gt; CREATE TABLE `testtable` (<br />
`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,<br />
`testfield` VARCHAR( 15 ) NOT NULL ,<br />
`testfield2` VARCHAR( 15 ) NOT NULL<br />
) ENGINE = innodb COMMENT = 'test table for dumping each row to file';<br />
mysql&gt; INSERT INTO `testtable` values ('','test1','test2'),('','test3','test4'),('','test5','test6');<br />
mysql&gt; select * from testtable;<br />
+----+-----------+------------+<br />
| id | testfield | testfield2 |<br />
+----+-----------+------------+<br />
|  1 | test1     | test2      |<br />
|  2 | test3     | test4      |<br />
|  3 | test5     | test6      |<br />
+----+-----------+------------+<br />
3 rows in set (0.00 sec)</code></p>
<p>Now we create a php script which we can use to select data from MySQL database and save it to a file under /tmp directory.  You can easily modify this script to save it somewhere else, pull from multiple tables and/or save with different delimiter.</p>
<p><code>&lt;?php<br />
$link = mysql_connect('localhost', 'username', 'password');<br />
if (!$link) {<br />
die('Could not connect: ' . mysql_error());<br />
}<br />
$db = mysql_select_db('testdump',$link);<br />
//specify what query to use to pull data.<br />
$select = "SELECT * FROM `testtable`";<br />
$result = mysql_query($select);<br />
while($myrow = mysql_fetch_array($result))<br />
{<br />
extract($myrow);<br />
//specify what query to use for create text files with csv data<br />
//in our case we create files with testtable_ and ID which is unique<br />
//per row.  We use the same ID in where clause to limit one record at a time<br />
//modify this part to your needs.  Eg change delimiter from , to - or something else<br />
$export = "<br />
SELECT * INTO OUTFILE '/tmp/testtable_$id.txt'<br />
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'<br />
LINES TERMINATED BY '\n'<br />
FROM testtable where id=$id";<br />
mysql_query($export);<br />
}//loop until we are done exporting all records.<br />
mysql_close($link);<br />
?&gt;</code></p>
<p>You can run this script from command line if you have php cli installed by issuing:  <code>php filename.php</code></p>
<p>Above example is a very simple example. t is merely a proof of concept.  You can modify it to do whatever you need it to do.  I would also put some error checking, not use extract() function, etc before using it in production environment.</p>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/05/12/mysql-how-do-i-dump-export-each-record-from-table-separate-files-in-csv-format/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL:  How do you use symlinks with MySQL tables?</title>
		<link>http://crazytoon.com/2008/05/02/mysql-how-do-you-use-symlinks-with-mysql-tables/</link>
		<comments>http://crazytoon.com/2008/05/02/mysql-how-do-you-use-symlinks-with-mysql-tables/#comments</comments>
		<pubDate>Fri, 02 May 2008 07:48:34 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux Apache MySQL PHP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mysql table symlinks]]></category>
		<category><![CDATA[symlinks]]></category>

		<guid isPermaLink="false">http://crazytoon.com/?p=66</guid>
		<description><![CDATA[Creating table symlinks in MySQL is very easy and it is probably one of the features a lot of MySQL users overlook or never think about.  You may ask, why would I want to use symlinks? Some of the reasons you would want symlinks is if you are running low on disk space on [...]]]></description>
			<content:encoded><![CDATA[<p>Creating table symlinks in MySQL is very easy and it is probably one of the features a lot of MySQL users overlook or never think about.  You may ask, why would I want to use symlinks? Some of the reasons you would want symlinks is if you are running low on disk space on the partition where your data generally is and/or you want to move one (or more) table(s) on to a different disk/partition for performance reasons.</p>
<p>One of the things worth mentioning is that MySQL documentation states:  &#8220;Symlinks are fully supported only for MyISAM tables. For files used by tables for other storage engines, you may get strange problems if you try  to use symbolic links.&#8221;  Keeping that in mind, if you have a innodb table and would like to create symlinks for, you should change the engine type to myisam before symlinking. Although, I have heard and personally used symlinks for innodb databases with no side affects.</p>
<p>First let us find out if your MySQL configuration allows symlinks:</p>
<p><code class="literal">mysql&gt; SHOW VARIABLES LIKE 'have_symlink'\G<br />
*************************** 1. row ***************************<br />
Variable_name: have_symlink<br />
Value: YES</code></p>
<p>I am going to make following assumptions:</p>
<ul>
<li>MySQL installation is in /usr/local/mysql.</li>
<li>You want to create new tables under /mnt/another_partition/mysql/var
<ol>
<li>you must create the dir: mkdir /mnt/another_partition/mysql/var -p</li>
<li>Make it owned by mysql user:  chown mysql /mnt/another_partition/mysql/var</li>
</ol>
</li>
</ul>
<p>We will create some test data.  Following are the steps:</p>
<p><code>mysql&gt; create database db1;<br />
Query OK, 1 row affected (0.05 sec)<br />
mysql&gt; use db1;<br />
Database changed<br />
mysql&gt; CREATE TABLE `users` ( `id` int(11) default NULL, `name` varchar(255) default NULL ) ENGINE=myisam, <strong>data directory="/mnt/another_partition/mysql/var", index directory="/mnt/another_partition/mysql/var"</strong>;<br />
Query OK, 0 rows affected (0.09 sec)<br />
mysql&gt; show tables;<br />
+---------------+<br />
| Tables_in_db1 |<br />
+---------------+<br />
| users         |<br />
+---------------+</code></p>
<p>Drop back to command prompt and &#8220;ls&#8221; your db1 directory as shown below.  Here you can see that users table&#8217;s data file (users.MYD) is a symbolic link to the location we specified.  You can see that users index file (users.MYI) is symlinked to the location we specified as well.<br />
<code><br />
# ls -al /usr/local/mysql/var/db1/<br />
total 24K<br />
drwx------   2 mysql mysql 4.0K May  2 00:03 ./<br />
drwx------  23 mysql mysql 4.0K May  1 23:59 ../<br />
-rw-rw----   1 mysql mysql   65 May  1 09:33 db.opt<br />
-rw-rw----   1 mysql mysql 8.4K May  2 00:03 users.frm<br />
lrwxrwxrwx   1 mysql mysql   42 May  2 00:03 users.MYD -&gt; /mnt/another_partition/mysql/var/users.MYD<br />
lrwxrwxrwx   1 mysql mysql   42 May  2 00:03 users.MYI -&gt; /mnt/another_partition/mysql/var/users.MYI</code></p>
<p>Here are the real files which above symlinks are pointing to:</p>
<p><code># ls -al /mnt/another_partition/mysql/var/<br />
total 12K<br />
drwxr-xr-x  2 mysql root  4.0K May  2 00:03 ./<br />
drwxr-xr-x  3 root  root  4.0K May  1 23:39 ../<br />
-rw-rw----  1 mysql mysql    <strong>0</strong> May  2 00:03 users.MYD<br />
-rw-rw----  1 mysql mysql 1.0K May  2 00:03 users.MYI</code></p>
<p>If you go back in to mysql prompt and insert couple rows, you can see the size of users.MYD increase:</p>
<p><code>mysql&gt; insert into db1.users values (1,"test1");<br />
Query OK, 1 row affected (0.01 sec)<br />
mysql&gt; insert into db1.users values (2,"test2");<br />
Query OK, 1 row affected (0.01 sec)<br />
# ls -al /mnt/another_partition/mysql/var/<br />
total 16K<br />
drwxr-xr-x  2 mysql root  4.0K May  2 00:03 ./<br />
drwxr-xr-x  3 root  root  4.0K May  1 23:39 ../<br />
-rw-rw----  1 mysql mysql   <strong>40</strong> May  2 00:08 users.MYD<br />
-rw-rw----  1 mysql mysql 1.0K May  2 00:08 users.MYI</code></p>
<p>If you already have data file and index file you would like to move, I recommend shutting down your MySQL server and moving the files manually and create symbolic links by using &#8220;ln -s&#8221; command.  You can read more about ln by doing <a title="man ln:  man page for ln" href="http://www.lamp-tips.com/man-pages/ln/" target="_blank">man ls</a></p>
<p><strong>NOTE: </strong>Even though MySQL documentation says this is only supported 100% for myisam tables, I  have read few posts where people say they have done this with innodb tables without any problems.  If anybody else can comment on this, I would greatly appreciate it.</p>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/05/02/mysql-how-do-you-use-symlinks-with-mysql-tables/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>MySQL Replication Series (tip #1): what should be replicated and what should not be replicated?</title>
		<link>http://crazytoon.com/2008/04/21/mysql-replication-replicate-by-choice/</link>
		<comments>http://crazytoon.com/2008/04/21/mysql-replication-replicate-by-choice/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 01:26:26 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Linux Apache MySQL PHP]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL Replication Series]]></category>
		<category><![CDATA[MySQL cluster]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://crazytoon.com/?p=62</guid>
		<description><![CDATA[Welcome to Tip #1 in MySQL Replication Series.  In this tip we will go over what to do when you only want to replicate certain data to slave(s).  Most general way to tell what is replicated to which slave is to include following configuration directive in my.cnf file depending on your environment and [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to Tip #1 in MySQL Replication Series.  In this tip we will go over what to do when you only want to replicate certain data to slave(s).  Most general way to tell what is replicated to which slave is to include following configuration directive in my.cnf file depending on your environment and your goals.  We will start with slave server side options since you have more flexibility on slave on what to replicate and what not to.</p>
<p>Slave server options:</p>
<ul>
<li><em>replicate-do-db = dbname</em> (or) <em>replicate-do-db = dbname1, dbname2, &#8230;, dbnameN</em><br />
This option is used on slave server to tell the server to only replicate <em>dbname </em>db on this particular host.  You would want to use this if you have a master which is replicating to multiple slaves and each slave may contain different database for read performance reasons.</li>
<li><em>replicate-ignore-db = dbname</em> (or)  <em>replicate-ignore-db = dbname1, dbname2, &#8230;, dbnameN</em><br />
This option is used on slave server to tell server to ignore database(s) listed.  This is used when you want to replicate every database except certain individual ones.  For example, you may want to replicate-ignore-db=testdb</li>
<li><em>replicate-do-table = dbname.tablename</em><br />
This specifies a table from a database to be replicated.</li>
<li><em>replicate-ignore-table = dbname.tablename</em><br />
This is very useful and often ignored.  If you have logging table which you only do writes to but never read from, there is no real point to replicate that table to slave(s).  This way you ignore specific tables.</li>
<li><em>replicate-wild-do-table=dbname.tablename%</em><br />
This is another option which can prove itself to be very useful.  Let&#8217;s say you have database with multiple type of open source installations (phpbb, wordpress, drupal, etc), and you want to designate slave(s) to only be used for phpbb, you would do:  <em>replicate-wild-do-table=dbname.phpbb%</em></li>
<li><em>replicate-wild-ignore-table=dbname.tablename%<br />
</em>Another option which can be used instead of above example.  Say you wanted everything on this particular slave BUT phpbb tables.  You would put this in your config:  <em>replicate-wild-ignore-table=dbname.phpbb%</em></li>
</ul>
<p style="padding-left: 30px;">NOTE:  for wildcards, you can use generic mysql wildcards.</p>
<p>Master server options:</p>
<ul>
<li><em>binlog-do-db = dbnam</em>e (or) <em>binlog-do-db = dbname1, dbname2, &#8230;, dbnameN</em><br />
This option is used on master server to tell the master server to only log queries for dbname db.  This is helpful if your master has multiple databases but you only want to replicate selected few to all slaves.</li>
<li><em>binlog-ignore-db = dbnam</em>e (or) <em>binlog-</em><em>ignore</em><em>-db = dbname1, dbname2, &#8230;, dbnameN<br />
</em>Ignore specified database and do not log queries referring to list database(s).</li>
</ul>
<p>In order for mysql to know which database queries to log (or not to log) you have to make sure you specify what is your default db by doing mysql_select_db() or issuing &#8220;use dbname&#8221; command.   For example, if you had binlog-do-db=forums and you issue a query:  delete from main.users (basically saying, <em>delete </em>everything from <em>users </em>table in <em>main </em>database), it will still log the command even though <em>main </em>database is not in the binlog-do-db list.  To get around this, you should do the following:  <code>use main; delete from users;</code> If you do not do that, your query &#8220;delete from main.users&#8221; will be logged and executed on slave(s).  There is a high probability that you do not have a test database on your slave.</p>
<p>NOTE:  This post is part of series on MySQL Replication.  Here is the original post: <a title="MySQL Replication Series:  How does MySQL replication work?" href="http://crazytoon.com/2008/04/17/mysql-replication-series-how-does-mysql-replication-works/" target="_blank">MySQL Replication Series:  How does MySQL replication work?</a></p>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/04/21/mysql-replication-replicate-by-choice/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MySQL Replication Series:  How does MySQL replication work?</title>
		<link>http://crazytoon.com/2008/04/17/mysql-replication-series-how-does-mysql-replication-works/</link>
		<comments>http://crazytoon.com/2008/04/17/mysql-replication-series-how-does-mysql-replication-works/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 16:00:51 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Linux Apache MySQL PHP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL cluster]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://crazytoon.com/?p=61</guid>
		<description><![CDATA[Setting up replication is not hard and can be done by following simple directions. You can learn setting up two type of replication MySQL offers here:  master-master replication and master-slave replication. Once you have replication set up, you can start playing with it without doing any more changes to the configuration. But let us [...]]]></description>
			<content:encoded><![CDATA[<p>Setting up replication is not hard and can be done by following simple directions. You can learn setting up two type of replication MySQL offers here:  <a title="Master-Master replication in MySQL" href="http://crazytoon.com/2008/02/29/mysql-how-do-you-set-up-mastermaster-replication-in-mysql-centos-rhel-fedora/" target="_blank">master-master replication</a> and <a title="Master-Slave replication in MySQL" href="http://crazytoon.com/2008/01/29/mysql-how-do-you-set-up-masterslave-replication-in-mysql-centos-rhel-fedora/" target="_blank">master-slave replication</a>. Once you have replication set up, you can start playing with it without doing any more changes to the configuration. But let us face it, using replication in production environment and for sites which are either going live or are live, requires deeper knowledge of how replication can be used and what it can (or can not) do. We will start this &#8220;MySQL Replication Series&#8221; with briefly going over how MySQL replication works. Later in the series I will be posting some tips on how to fine tune the settings to get replication to do what you want it to do. I will be keeping all the tips separate and will add links to all of the tips at the bottom of this post as I post them. Let us start by quick overview of how replication works.</p>
<p>In a MySQL replication setup master server puts all the commands it is executing which affects data (insert, delete, update, truncate, etc) into a file referred to as bin-log file.  Slave(s) than pick up the big-log files and execute those commands on themselves.  You can see which bin file is currently being used by typing: show master status, you would see output similar to:</p>
<p><code>mysql&gt; show master status\G<br />
*************************** 1. row ***************************<br />
File: mysql-bin.000001<br />
Position: 519132576</code></p>
<p>This tells us that right now we are writing to mysql-bin.000001 and it is at position 519132576. Why does this position matter? It matters if you are looking at your slaves and trying to figure out where it is currently as far as position goes. If you were to do mysqldump and do it with master info, it is this number which gets added to the dump file so when you import on slave, it knows where to start from.</p>
<p>At this point in time if you go to slave, and type: show slave status\G, you would see something similiar to below:</p>
<p><code>mysql&gt; show slave status\G<br />
*************************** 1. row ***************************<br />
Slave_IO_State: Waiting for master to send event<br />
Master_Host: 10.0.0.1<br />
Master_User: slaveuser<br />
Master_Port: 3306<br />
Connect_Retry: 60<br />
Master_Log_File: mysql-bin.000001<br />
Read_Master_Log_Pos: 519132576</code></p>
<p>As you can see it is at the same point meaning it is all caught up to the master (this snapshot was taken from a site which was not accepting traffic at the time).  Another thing you can look at, even though it is not the most accurate way to measure how behind you are, is to check last row in &#8220;show slave status&#8221; labeled: Seconds_Behind_Master: 0 &lt;- if this is greater than 0, than you are lagging behind the master.  Which in fact just means that it has not played back some of the queries.  If it is NULL, it means that your slave is either not started or it has errors which needs attention. You can start and stop the slave by typing: <code>start slave;</code> and <code>stop slave;</code> respectively.</p>
<p>Hopefully this gives an overview of how MySQL replication works.  As always, please feel free to correct me, add on to what I have said or just make comments.  I am, like many others, learning new things every day.</p>
<p>Tip #1: <a title="MySQL Replication Series (tip #1): what should be replicated and what should not be replicatied?" rel="bookmark" href="http://crazytoon.com/2008/04/21/mysql-replication-replicate-by-choice">What should be replicated and what should not be replicated?</a><br />
————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/04/17/mysql-replication-series-how-does-mysql-replication-works/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL: Solution for ERROR 1442 (HY000): Can&#8217;t update table &#8216;t1&#8242; in stored function/trigger because it is already used by statement which invoked this stored function/trigger.</title>
		<link>http://crazytoon.com/2008/03/03/mysql-error-1442-hy000-cant-update-table-t1-in-stored-functiontrigger-because-it-is-already-used-by-statement-which-invoked-this-stored-functiontrigger/</link>
		<comments>http://crazytoon.com/2008/03/03/mysql-error-1442-hy000-cant-update-table-t1-in-stored-functiontrigger-because-it-is-already-used-by-statement-which-invoked-this-stored-functiontrigger/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 02:42:02 +0000</pubDate>
		<dc:creator>Tracy</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux Apache MySQL PHP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL Triggers]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2008/03/03/mysql-error-1442-hy000-cant-update-table-t1-in-stored-functiontrigger-because-it-is-already-used-by-statement-which-invoked-this-stored-functiontrigger/</guid>
		<description><![CDATA[Here is a sample table you can create to test following problem/solution on:
CREATE TABLE `t1` (
`a` char(1) default NULL,
`b` smallint(6) default NULL
);
insert into t1 values ('y','1');
I have a table t1 which has column a and b, I want column a to be updated to &#8216;n&#8217; when column b = 0. Here is the first version [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a sample table you can create to test following problem/solution on:</p>
<p><code>CREATE TABLE `t1` (<br />
`a` char(1) default NULL,<br />
`b` smallint(6) default NULL<br />
);<br />
insert into t1 values ('y','1');</code></p>
<p>I have a table t1 which has column a and b, I want column a to be updated to &#8216;n&#8217; when column b = 0. Here is the first version I created:</p>
<p><code>DELIMITER |<br />
CREATE TRIGGER trigger1 AFTER UPDATE ON t1<br />
FOR EACH ROW UPDATE t1 SET a= 'n' WHERE b=0;<br />
|<br />
DELIMITER ;</code></p>
<p>The trigger created successfully but I got this error when I tried to do an update on column b on table t1:<br />
<code>mysql&gt; update t1 set b=0;<br />
ERROR 1442 (HY000): Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.<br />
</code></p>
<p>After searching online for a while and trying different solutions, I finally found a way to update the table which has trigger on it:<br />
<code><br />
drop trigger trigger1;<br />
DELIMITER |<br />
CREATE TRIGGER trigger1 BEFORE UPDATE ON t1<br />
FOR EACH ROW<br />
BEGIN<br />
IF NEW.b=0 THEN<br />
SET NEW.a = 'n';<br />
END IF;<br />
END<br />
|<br />
DELIMITER ;</code></p>
<p>After the new trigger is in, I issued the same update query and &#8220;<small>ERROR 1442 (HY000): Can&#8217;t update table &#8216;t1&#8242; in stored function/trigger because it is already used by statement which invoked this stored function/trigger.</small>&#8221;  didn&#8217;t show up and it updated the col a value to &#8220;n&#8221; as it suppose to.</p>
<p><code>mysql&gt; update t1 set b=0;<br />
Query OK, 1 row affected (0.01 sec)<br />
Rows matched: 1  Changed: 1  Warnings: 0<br />
mysql&gt; select * from t1\G<br />
*************************** 1. row ***************************<br />
a: <strong>n</strong><br />
b: 0</code></p>
<p>Therefore, if you want to create a trigger on the table which will update itself, make sure you use the NEW.column_name to refer to the row after it&#8217;s updated and don&#8217;t do the full update statement!</p>
<p>However, if you are updating some other table, then you can use the regular update statement:</p>
<p><code>DELIMITER |<br />
CREATE TRIGGER trigger1 AFTER UPDATE ON <strong>t1 </strong><br />
FOR EACH ROW UPDATE<strong> t2 </strong>SET a= 'n' WHERE b=0;<br />
|<br />
DELIMITER ;</code></p>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/03/03/mysql-error-1442-hy000-cant-update-table-t1-in-stored-functiontrigger-because-it-is-already-used-by-statement-which-invoked-this-stored-functiontrigger/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>MySQL: How do you set up master-master replication in MySQL? (CentOS, RHEL, Fedora)</title>
		<link>http://crazytoon.com/2008/02/29/mysql-how-do-you-set-up-mastermaster-replication-in-mysql-centos-rhel-fedora/</link>
		<comments>http://crazytoon.com/2008/02/29/mysql-how-do-you-set-up-mastermaster-replication-in-mysql-centos-rhel-fedora/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 21:40:02 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux Apache MySQL PHP]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[Linux Tips]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL cluster]]></category>
		<category><![CDATA[Redhat]]></category>
		<category><![CDATA[System admin]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2008/02/29/mysql-how-do-you-set-up-mastermaster-replication-in-mysql-centos-rhel-fedora/</guid>
		<description><![CDATA[Setting up master-master replication in MySQL is very similar to how we set up master/slave replication.  You can read up about how to setup master/slave replication in my previous post: How to set up master/slave replication in MySQL.  There is obviously pros and cons about using master/master replication.  But this is not [...]]]></description>
			<content:encoded><![CDATA[<p>Setting up master-master replication in MySQL is very similar to how we <a href="http://crazytoon.com/2008/01/29/mysql-how-do-you-set-up-masterslave-replication-in-mysql-centos-rhel-fedora/">set up master/slave replication</a>.  You can read up about how to setup master/slave replication in my previous post: <a href="http://crazytoon.com/2008/01/29/mysql-how-do-you-set-up-masterslave-replication-in-mysql-centos-rhel-fedora/" title="How to set up master/slave replication in MySQL" target="_blank">How to set up master/slave replication in MySQL</a>.  There is obviously pros and cons about using master/master replication.  But this is not a post which discuses advantages and disadvantages for using master/master replication. One of the differences between master/master set up and master/slave is that in master/master set up, you have true redundancy.  If one server dies, second server can take all the inserts/selects.  In master/slave setup, if master dies, you will have to go through steps to make slave become the master.  Master/master set up we are going to set up is essentially master/slave and slave/master.  Meaning, if you had two servers, db0 and db1, your setup will be db0(master)/db1(slave) and also db0(slave)/db1(master).  Here are some assumptions:</p>
<p><code>Master1 server ip:  10.0.0.1<br />
Master2 server ip:  10.0.0.2<br />
Slave username:  slaveuser<br />
Slave pw:  slavepw<br />
Your data directory is:  /usr/local/mysql/var/</code></p>
<p>Let us go through the steps you must take on Master1 to enable it to act as master and slave by using following configuration which goes  under [mysqld] section:</p>
<p><code># let's make it so auto increment columns behave by having different increments on both servers<br />
auto_increment_increment=2<br />
auto_increment_offset=1<br />
# Replication Master Server<br />
# binary logging is required for replication<br />
log-bin=master1-bin<br />
binlog-ignore-db=mysql<br />
binlog-ignore-db=test<br />
# required unique id between 1 and 2^32 - 1<br />
server-id       = 1<br />
#following is the slave settings so this server can connect to master2<br />
master-host = 10.0.0.2<br />
master-user = slaveuser<br />
master-password = slavepw<br />
master-port = 3306</code></p>
<p>Following is the configuration which goes on master2 under [mysqld] section:</p>
<p><code># let's make it so auto increment columns behave by having different increments on both servers<br />
auto_increment_increment=2<br />
auto_increment_offset=2<br />
# Replication Master Server<br />
# binary logging is required for replication<br />
log-bin=master2-bin<br />
binlog-ignore-db=mysql<br />
binlog-ignore-db=test<br />
# required unique id between 1 and 2^32 - 1<br />
server-id       = 2<br />
#following is the slave settings so this server can connect to master2<br />
master-host = 10.0.0.1<br />
master-user = slaveuser<br />
master-password = slavepw<br />
master-port = 3306</code></p>
<p>On master1 server, go to mysql&gt; prompt and add the appropriate user:</p>
<p><code>mysql&gt; grant replication slave on *.* to slaveuser@'10.0.0.2' identified by 'slavepw';</code></p>
<p>On master2 server do the same but allow right ip:</p>
<p><code>mysql&gt; grant replication slave on *.* to slaveuser@'10.0.0.1' identified by 'slavepw';</code></p>
<p>Restart both of the master servers and check slave status:</p>
<p><code>mysql&gt; show slave status\G</code></p>
<p>That is all you have to do to set up the replication.  Of course there are a lot more configuration options but this should get your replication going and you can tweak from here on.</p>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/02/29/mysql-how-do-you-set-up-mastermaster-replication-in-mysql-centos-rhel-fedora/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>MySQL:  How do you set up master-slave replication in MySQL? (CentOS, RHEL, Fedora)</title>
		<link>http://crazytoon.com/2008/01/29/mysql-how-do-you-set-up-masterslave-replication-in-mysql-centos-rhel-fedora/</link>
		<comments>http://crazytoon.com/2008/01/29/mysql-how-do-you-set-up-masterslave-replication-in-mysql-centos-rhel-fedora/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 03:12:57 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Data Backup]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux Apache MySQL PHP]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL backup]]></category>
		<category><![CDATA[MySQL cluster]]></category>
		<category><![CDATA[Redhat]]></category>
		<category><![CDATA[System admin]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2008/01/29/mysql-how-do-you-set-up-masterslave-replication-in-mysql-centos-rhel-fedora/</guid>
		<description><![CDATA[Before we go into how to set up master-slave replication in MySQL, let us talk about some of the reasons I have set up master-slave replication using MySQL.
1)    Offload some of the queries from one server to another and spread the load:  One of the biggest advantages to have master-slave set [...]]]></description>
			<content:encoded><![CDATA[<p>Before we go into how to set up master-slave replication in MySQL, let us talk about some of the reasons I have set up master-slave replication using MySQL.</p>
<p>1)    Offload some of the queries from one server to another and spread the load:  One of the biggest advantages to have master-slave set up in MySQL is to be able to use master for all of the inserts and send some, if not all, select queries to slave.  This will most probably speed up your application without having to diving into optimizing all the queries or buying more hardware.</p>
<p>2)    Do backups from slave:  One of the advantages people overlook is that you can use MySQL slave to do backups from.  That way site is not affected at all when doing backups.  This becomes a big deal when your database has grown to multiple gigs and every time you do backups using mysqldump, site lags when table locks happen.  For some sites, this could mean that site goes down for few secs to minutes.  If you have slave, you just take slave out of rotation (should be built into code) and run backups off the slave.  You can even stop slave MySQL instance and copy the var folder instead of doing mysqldump.</p>
<p>Ok let us dive into how to setup master-slave replication under MySQL.  There are many configuration changes you can do to optimize your MySQL set up.  I will just touch on very basic ones to get the replication to work.  Here are some assumptions:</p>
<blockquote><p>Master server ip:  10.0.0.1<br />
Slave server ip:  10.0.0.2<br />
Slave username:  slaveuser<br />
Slave pw:  slavepw<br />
Your data directory is:  /usr/local/mysql/var/</p></blockquote>
<p>Put the following in your master my.cnf file under [mysqld] section:</p>
<p><code># changes made to do master<br />
server-id           = 1<br />
relay-log           = /usr/local/mysql/var/mysql-relay-bin<br />
relay-log-index     = /usr/local/mysql/var/mysql-relay-bin.index<br />
log-error           = /usr/local/mysql/var/mysql.err<br />
master-info-file    = /usr/local/mysql/var/mysql-master.info<br />
relay-log-info-file = /usr/local/mysql/var/mysql-relay-log.info<br />
datadir             = /usr/local/mysql/var<br />
log-bin             = /usr/local/mysql/var/mysql-bin<br />
# end master</code></p>
<p>Copy the following to slave&#8217;s my.cnf under [mysqld] section:</p>
<p><code># changes made to do slave<br />
server-id           = 2<br />
relay-log           = /usr/local/mysql/var/mysql-relay-bin<br />
relay-log-index     = /usr/local/mysql/var/mysql-relay-bin.index<br />
log-error           = /usr/local/mysql/var/mysql.err<br />
master-info-file    = /usr/local/mysql/var/mysql-master.info<br />
relay-log-info-file = /usr/local/mysql/var/mysql-relay-log.info<br />
datadir             = /usr/local/mysql/var<br />
# end slave setup</code></p>
<p>Create user on master:<br />
<code>mysql&gt; grant replication slave on *.* to slaveuser@'10.0.0.2' identified by 'slavepw';</code></p>
<p>Do a dump of data to move to slave<br />
<code>mysqldump -u root --all-databases --single-transaction --master-data=1 &gt; masterdump.sql</code></p>
<p>import dump on slave<br />
<code>mysql &lt; masterdump.sql</code></p>
<p>After dump is imported go in to mysql client by typing mysql.  Let us tell the slave which master to connect to and what login/password to use:<br />
<code>mysql&gt; CHANGE MASTER TO MASTER_HOST='10.0.0.1',  MASTER_USER='slaveuser', MASTER_PASSWORD='slavepw';</code></p>
<p>Let us start the slave:<br />
<code>mysql&gt; start slave;</code></p>
<p>You can check the status of the slave by typing<br />
<code>mysql&gt; show slave status\G</code></p>
<p>The last row tells you how many seconds its behind the master.  Don’t worry if it doesn’t say 0, the number should be going down over time until it catches up with master (at that time it will show Seconds_Behind_Master: 0)  If it shows NULL, it could be that slave is not started (you can start by typing:  start slave) or it could be that it ran into an error (shows up in Last_errno: and Last_error under show slave status\G).</p>
<p>Related posts:</p>
<ul>
<li><a href="http://crazytoon.com/2007/11/26/mysql-how-do-i-dump-all-tables-in-a-database-into-separate-files/" title="How do I dump all tables in a database into separate files?" target="_blank">How do I dump all tables in a database into separate files?</a></li>
<li><a href="http://crazytoon.com/2007/11/28/mysql-how-do-i-import-individual-table-dump-files-in-to-mysql-using-shell-script/" title="How do I import individual table dump files in to MySQL using shell script?" target="_blank">How do I import individual table dump files in to MySQL using shell script?</a></li>
<li><a href="http://crazytoon.com/2007/01/23/mysql-backups-using-mysqldump/" title="MySQL backups using mysqldump" target="_blank">MySQL backups using mysqldump</a></li>
</ul>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2008/01/29/mysql-how-do-you-set-up-masterslave-replication-in-mysql-centos-rhel-fedora/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>MySQL:  How do I import individual table dump files in to MySQL using shell script?</title>
		<link>http://crazytoon.com/2007/11/28/mysql-how-do-i-import-individual-table-dump-files-in-to-mysql-using-shell-script/</link>
		<comments>http://crazytoon.com/2007/11/28/mysql-how-do-i-import-individual-table-dump-files-in-to-mysql-using-shell-script/#comments</comments>
		<pubDate>Thu, 29 Nov 2007 03:35:49 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[Linux Tips]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL backup]]></category>
		<category><![CDATA[Notes for self]]></category>
		<category><![CDATA[Redhat]]></category>
		<category><![CDATA[System admin]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/11/28/mysql-how-do-i-import-individual-table-dump-files-in-to-mysql-using-shell-script/</guid>
		<description><![CDATA[After I wrote the post: How do I dump all tables in a database into separate files? I got emails from couple people asking how to import the individual table files back in to MySQL.  First way to import each sql file created by  the post is to import each file individually by [...]]]></description>
			<content:encoded><![CDATA[<p>After I wrote the post: <a href="http://crazytoon.com/2007/11/26/mysql-how-do-i-dump-all-tables-in-a-database-into-separate-files/" rel="bookmark" title="MySQL:  How do I dump all tables in a database into separate files?">How do I dump all tables in a database into separate files?</a> I got emails from couple people asking how to import the individual table files back in to MySQL.  First way to import each sql file created by  <a href="http://crazytoon.com/2007/11/26/mysql-how-do-i-dump-all-tables-in-a-database-into-separate-files/" rel="bookmark" title="MySQL:  How do I dump all tables in a database into separate files?">the post</a> is to import each file individually by typing:<code>mysql db_name &lt; table1.sql</code>  This will work as long as you are only importing few files.  But if you need to import all of the files in the directory, which could be in 100&#8217;s, this method does not scale well.  To achieve this I wrote a shell script which does the work for me.  Of course, there are other ways to do this and I am only showing you one way of doing it.  This works for me so here it is:</p>
<p><code>#!/bin/bash<br />
db=$1<br />
if [ "$db" = "" ]; then<br />
echo "Usage: $0 db_name"<br />
exit 1<br />
fi<br />
mkdir done<br />
clear<br />
for sql_file in *.sql; do<br />
echo "Importing $sql_file";<br />
mysql $db&lt; $sql_file;<br />
mv $sql_file done;<br />
done</code></p>
<p>Related posts:</p>
<ul>
<li><a href="http://crazytoon.com/2007/11/26/mysql-how-do-i-dump-all-tables-in-a-database-into-separate-files/" title="How do I dump all tables in a database into separate files?" target="_blank">How do I dump all tables in a database into separate files?</a></li>
<li><a href="http://crazytoon.com/2007/01/23/mysql-backups-using-mysqldump/" title="MySQL backups using mysqldump" target="_blank">MySQL backups using mysqldump</a></li>
</ul>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/11/28/mysql-how-do-i-import-individual-table-dump-files-in-to-mysql-using-shell-script/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>MySQL:  How do I dump all tables in a database into separate files?</title>
		<link>http://crazytoon.com/2007/11/26/mysql-how-do-i-dump-all-tables-in-a-database-into-separate-files/</link>
		<comments>http://crazytoon.com/2007/11/26/mysql-how-do-i-dump-all-tables-in-a-database-into-separate-files/#comments</comments>
		<pubDate>Tue, 27 Nov 2007 00:22:53 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Data Backup]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL backup]]></category>
		<category><![CDATA[System admin]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/11/26/mysql-how-do-i-dump-all-tables-in-a-database-into-separate-files/</guid>
		<description><![CDATA[There have been numerous occasions where I needed to make backups of individual tables from selected database.  Usually I can achieve this by typing:
mysqldump database_name table1 &#62; table1.sql
mysqldump database_name table2 &#62; table2.sql

This could be very painful if you have 10’s or 100’s of tables. Until today, I never ran into a situation where I [...]]]></description>
			<content:encoded><![CDATA[<p>There have been numerous occasions where I needed to make backups of individual tables from selected database.  Usually I can achieve this by typing:</p>
<p><code>mysqldump database_name table1 &gt; table1.sql<br />
mysqldump database_name table2 &gt; table2.sql<br />
</code><br />
This could be very painful if you have 10’s or 100’s of tables. Until today, I never ran into a situation where I had to deal with dumping more than few tables at a time.   Today I had to do a dump of 181 tables.  I was not going to sit there and type in that command with 181 table names.  It is not just time consuming but it is also stupid.  So I wrote this script to help me with this task.  We still use mysqldump command as described above, except we do it programmatically to make it easier on us:</p>
<p><code>#!/bin/bash<br />
db=$1<br />
if [ "$db" = "" ]; then<br />
echo "Usage: $0 db_name"<br />
exit 1<br />
fi<br />
mkdir $$<br />
cd $$<br />
clear<br />
for table in `mysql $db -e 'show tables' | egrep -v 'Tables_in_' `; do<br />
echo "Dumping $table"<br />
mysqldump --opt -Q $db $table &gt; $table.sql<br />
done<br />
if [ "$table" = "" ]; then<br />
echo "No tables found in db:  $db"<br />
fi</code></p>
<p>You can also compress your files by adding bzip2, zip or any other compression commands after mysqldump command.  Here is the same script with bzip2 command added:<br />
<code>#!/bin/bash<br />
db=$1<br />
if [ "$db" = "" ]; then<br />
echo "Usage: $0 db_name"<br />
exit 1<br />
fi<br />
mkdir $$<br />
cd $$<br />
clear<br />
for table in `mysql $db -e 'show tables' | egrep -v 'Tables_in_' `; do<br />
echo "Dumping $table"<br />
mysqldump --opt -Q $db $table &gt; $table.sql<br />
bzip2 $table.sql<br />
done<br />
if [ "$table" = "" ]; then<br />
echo "No tables found in db:  $db"<br />
fi</code></p>
<p>I do not recommend doing compression on a production server since most compression program put descent amount of load on the server.  Also note that this will delay your dump considerably.  You may also want to use different parameters for running mysqldump.  Type <a href="http://www.lamp-tips.com/man-pages/mysqldump/" title="man mysqldump - man page for mysqldump" target="_blank"><code>man mysqldump</code></a> in your shell to read more.</p>
<p>Related posts:</p>
<ul>
<li><a href="http://crazytoon.com/2007/03/19/mysql-database-backup-file-compression-gzip-vs-bzip2/" target="_blank" title="MySQL database backup file compress gzip vs bzip2">MySQL database backup file compress gzip vs bzip2</a></li>
<li><a href="http://crazytoon.com/2007/01/23/mysql-backups-using-mysqldump/" title="MySQL backups using mysqldump" target="_blank">MySQL backups using mysqldump</a></li>
</ul>
<p>————————————-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/11/26/mysql-how-do-i-dump-all-tables-in-a-database-into-separate-files/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>MySQL Cluster:  Changing datamemory requires node restart with &#8211;initial?</title>
		<link>http://crazytoon.com/2007/11/13/mysql-cluster-changing-datamemory-requires-node-restart-with-initial/</link>
		<comments>http://crazytoon.com/2007/11/13/mysql-cluster-changing-datamemory-requires-node-restart-with-initial/#comments</comments>
		<pubDate>Wed, 14 Nov 2007 02:52:57 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL cluster]]></category>
		<category><![CDATA[System admin]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/11/13/mysql-cluster-changing-datamemory-requires-node-restart-with-initial/</guid>
		<description><![CDATA[I ran into something with mysql cluster today which boggles my mind.  On http://dev.mysql.com/doc/refman/5.0/en/mysql-cluster-config-params-ndbd.html page, it is documented that if we you change datamemory parameter in the config.ini under mysql-cluster like below, you would have to restart nodes to reread the configuration.
[NDBD]
id=2
HostName=10.0.0.2   # the IP of the first data node
DataMemory=6G
IndexMemory=512M
But when I tried the: [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into something with mysql cluster today which boggles my mind.  On <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-cluster-config-params-ndbd.html" title="MySQL cluster configuration Documentation" target="_blank">http://dev.mysql.com/doc/refman/5.0/en/mysql-cluster-config-params-ndbd.html</a> page, it is documented that if we you change datamemory parameter in the config.ini under mysql-cluster like below, you would have to restart nodes to reread the configuration.</p>
<p>[NDBD]<br />
id=2<br />
HostName=10.0.0.2   # the IP of the first data node<br />
DataMemory=6G<br />
IndexMemory=512M</p>
<p>But when I tried the: 2 restart in the mysql cluster managment console (ndb_mgm), it didn&#8217;t re-read the new configuration.  DataMemory was initially set to 2G and it continued using that configuration.  I even tried: 2 stop, and than logged on the node server and started ndbd manually, but it refused to read the new configuration.  It is only when I did ndbd &#8211;initial, nodes read in the configuration and were able to use more than 2 gigs of ram.</p>
<p>Did anybody else run into this?  Am I reading the documentation on MySQL site incorrectly?</p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/11/13/mysql-cluster-changing-datamemory-requires-node-restart-with-initial/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MySQL:  Slave lag behind master and data corruption.</title>
		<link>http://crazytoon.com/2007/10/15/mysql-slave-lag-behind-master-and-data-corruption/</link>
		<comments>http://crazytoon.com/2007/10/15/mysql-slave-lag-behind-master-and-data-corruption/#comments</comments>
		<pubDate>Tue, 16 Oct 2007 00:09:10 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[System admin]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/10/15/mysql-slave-lag-behind-master-and-data-corruption/</guid>
		<description><![CDATA[I am reaching out to mysql experts out there!  We just inherited a site which uses MySQL master/slave replication.  We set up the master and slaves per MySQL documents and did some of our own tweaking in my.cnf to get things working.  Things were great when we were testing the site but [...]]]></description>
			<content:encoded><![CDATA[<p>I am reaching out to mysql experts out there!  We just inherited a site which uses MySQL master/slave replication.  We set up the master and slaves per MySQL documents and did some of our own tweaking in my.cnf to get things working.  Things were great when we were testing the site but than after we put it up, we started to notice missing data on slaves.  At first, we thought maybe we missed something with the import of data on slaves, so we re-imported.  All tables in our db, at that time, were myisam.  After a day or so, we started to see data corruption on some of the slaves.   We went through and repaired all the tables.  Few hours after the repair, we checked row counts and the repaired tables didn&#8217;t have same count as master did.   Eventually most of our slaves were at the same state so we decided to re-import data from master again.</p>
<p>Before we went ahead and re-imported, we made a decision to move to innodb for *most* of our crucial tables.  After altering on master, we did a dump/import.  Converting tables to innodb got rid of our data corruption problem but our missing rows issue is still there.  Hardware on the master is 4&#215;1.86Ghz, 8 gigs of ram; slaves hardware is: 4&#215;1.86, 4 gigs of ram.</p>
<p>Here is the config running on our master:<br />
<code>&lt; some of the part of the config has been cut off to keep it short/simple and to the point &gt;<br />
skip-locking<br />
ft_min_word_len=2<br />
long_query_time=30<br />
max_connect_errors=10000<br />
max_connections=1000<br />
#skip-grant-tables<br />
skip-bdb<br />
delay-key-write=ALL<br />
key_buffer = 1G<br />
read_buffer_size = 2M<br />
read_rnd_buffer_size = 8M<br />
myisam_sort_buffer_size = 64M<br />
thread_concurrency = 8<br />
skip-name-resolve<br />
max_allowed_packet = 32M<br />
#low-priority-updates<br />
table_cache = 1024<br />
thread_cache = 8<br />
query_cache_size = 64M<br />
#<br />
# added by sunny &gt;<br />
wait_timeout = 120<br />
tmp_table_size = 128M<br />
sort_buffer_size = 128M   # Sort buffer is used to perform sorts for some ORDER BY and GROUP BY queries.<br />
read_rnd_buffer_size = 8M # Read the rows in sorted order by using the row<br />
# pointers in the result file. To optimize this,<br />
# we read in a big block of row pointers, sort them,<br />
# and use them to read the rows in sorted order into a row buffer.<br />
thread_cache_size = 8<br />
#<br />
# Try number of CPU's*2 for thread_concurrency<br />
thread_concurrency = 16<br />
log_slow_queries<br />
log_long_format<br />
#<br />
# INNODB Settings<br />
innodb_additional_mem_pool_size = 32M # was 16<br />
innodb_buffer_pool_size = 4G<br />
innodb_file_per_table<br />
innodb_log_file_size = 50M<br />
innodb_log_buffer_size = 8M<br />
innodb_flush_log_at_trx_commit = 0<br />
innodb_lock_wait_timeout = 50<br />
innodb_flush_method=O_DIRECT</code></p>
<p>Here is the config running on our slave:</p>
<p><code># The MySQL server<br />
[mysqld]<br />
port                = 3306<br />
socket              = /tmp/mysql1.sock<br />
pid-file            = /usr/local/mysql/var/mysql.pid<br />
relay-log           = /usr/local/mysql/var/mysql-relay-bin<br />
relay-log-index     = /usr/local/mysql/var/mysql-relay-bin.index<br />
log-error           = /usr/local/mysql/var/mysql.err<br />
master-info-file    = /usr/local/mysql/var/mysql-master.info<br />
relay-log-info-file = /usr/local/mysql/var/mysql-relay-log.info<br />
datadir             = /usr/local/mysql/var<br />
server-id           = 31<br />
skip-slave-start<br />
skip-locking<br />
skip-name-resolve<br />
ft_min_word_len=2<br />
long_query_time=30<br />
max_connect_errors=10000<br />
max_connections=1000<br />
#skip-grant-tables<br />
skip-bdb<br />
delay-key-write=ALL<br />
key_buffer = 512M<br />
max_allowed_packet = 1M<br />
table_cache = 512M<br />
myisam_sort_buffer_size = 64M<br />
thread_cache_size = 4<br />
read-only<br />
#slave-skip-errors=all<br />
#<br />
# added by sunny &gt;<br />
wait_timeout = 120<br />
tmp_table_size = 128M<br />
read_buffer_size = 2M<br />
sort_buffer_size = 128M   # Sort buffer is used to perform sorts for some ORDER BY and GROUP BY queries.<br />
read_rnd_buffer_size = 8M # Read the rows in sorted order by using the row<br />
# pointers in the result file. To optimize this,<br />
# we read in a big block of row pointers, sort them,<br />
# and use them to read the rows in sorted order into a row buffer.<br />
thread_cache_size = 8<br />
query_cache_size =  16M # changed to 16 3/1/07<br />
#<br />
# Try number of CPU's*2 for thread_concurrency<br />
thread_concurrency = 8<br />
log_slow_queries<br />
log_long_format<br />
#<br />
# INNODB Settings<br />
default_table_type = INNODB<br />
innodb_additional_mem_pool_size = 32M # was 16<br />
innodb_buffer_pool_size = 2G<br />
innodb_file_per_table<br />
innodb_log_file_size = 50M<br />
innodb_log_buffer_size = 8M<br />
innodb_flush_log_at_trx_commit = 0<br />
innodb_lock_wait_timeout = 50<br />
innodb_flush_method=O_DIRECT</code></p>
<p>Please feel free to comment/scrutinize any of these settings.  If you think they should be different, please give quick summary about why they should be different.  Some of these settings were inherited from old setup of mysql (where they had it hosted before).</p>
<p>Right now we are just trying to find out why we have missing rows and why our slaves lag behind constantly.  We have refreshed data almost every day for last two weeks.  We barely have load average above 1 or 2.  What can we do to track down the problem?   What are the things we should be looking out for?</p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/10/15/mysql-slave-lag-behind-master-and-data-corruption/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>MySQL:  How do I reset MySQL to have only the databases when I first installed it? (CentOS, Redhat, Fedora, Linux)</title>
		<link>http://crazytoon.com/2007/09/13/mysql-reset-mysql-have-only-databases-when-first-installed-centos-redhat-fedora-linux/</link>
		<comments>http://crazytoon.com/2007/09/13/mysql-reset-mysql-have-only-databases-when-first-installed-centos-redhat-fedora-linux/#comments</comments>
		<pubDate>Fri, 14 Sep 2007 06:44:07 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[System admin]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/09/13/mysql-how-do-i-reset-mysql-to-have-only-the-databases-when-i-first-installed-it-centos-redhat-fedora-linux/</guid>
		<description><![CDATA[One of my clients asked me today to make their MySQL installation go back to default database install.  Basically they wanted me to get rid of all their databases (in this case test databases) so they can start fresh and go live with only the databases they needed.  So here are the steps [...]]]></description>
			<content:encoded><![CDATA[<p>One of my clients asked me today to make their MySQL installation go back to default database install.  Basically they wanted me to get rid of all their databases (in this case test databases) so they can start fresh and go live with only the databases they needed.  So here are the steps I followed.</p>
<p>Note:  I have installed their MySQL from source and installed MySQL in to /usr/local/mysql folder.  Your installation path might be different.</p>
<p><code>service mysql stop<br />
cd /usr/local/mysql<br />
mv var var.bak<br />
mkdir var<br />
bin/mysql_install_db<br />
chown -R mysql.mysql  var<br />
chmod 700 -R var<br />
service mysql start<br />
</code><br />
At this point we should have fresh MySQL db setup.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/09/13/mysql-reset-mysql-have-only-databases-when-first-installed-centos-redhat-fedora-linux/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL:  InnoDB: ERROR: the age of the last checkpoint is [number]</title>
		<link>http://crazytoon.com/2007/08/16/mysql-innodb-error-the-age-of-the-last-checkpoint-is-number/</link>
		<comments>http://crazytoon.com/2007/08/16/mysql-innodb-error-the-age-of-the-last-checkpoint-is-number/#comments</comments>
		<pubDate>Thu, 16 Aug 2007 21:32:59 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Data Backup]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL backup]]></category>
		<category><![CDATA[System admin]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/08/16/mysql-innodb-error-the-age-of-the-last-checkpoint-is-number/</guid>
		<description><![CDATA[One of the mysql database servers I manage started to have issues with doing backups yesterday.  mysqldump was running but nothing was happening on the backup side.  I started to investigate to see why our full backups were failing.  I opened up the mysql error log file (mine is at:  /usr/local/mysql/var/hostname.err) [...]]]></description>
			<content:encoded><![CDATA[<p>One of the mysql database servers I manage started to have issues with doing backups yesterday.  mysqldump was running but nothing was happening on the backup side.  I started to investigate to see why our full backups were failing.  I opened up the mysql error log file (mine is at:  /usr/local/mysql/var/hostname.err) and notice there were many instances of following error.</p>
<p><code>070815 15:31:46  InnoDB: ERROR: the age of the last checkpoint is 9433957,<br />
InnoDB: which exceeds the log group capacity 9433498.<br />
InnoDB: If you are using big BLOB or TEXT rows, you must set the<br />
InnoDB: combined size of log files at least 10 times bigger than the<br />
InnoDB: largest such row.</code></p>
<p>I poked around and found out our log files were set to default (5 mb) and they needed to be increased.  After doing some calculations and research, I decided going to 50 mb at this point would be a good way to go.   So I made the change in my.cnf file and added:  <code>innodb_log_file_size = 50M </code></p>
<p>I then stopped our mysql server and restarted it.  And to my horror I saw following error messages show up in the mysql error logs:</p>
<p><code>070815 17:37:40 [ERROR] /usr/local/mysql/libexec/mysqld: Incorrect information in file: './dbname/table_name.frm'<br />
070815 17:37:40 [ERROR] /usr/local/mysql/libexec/mysqld: Incorrect information in file: './dbname/table_name.frm'</code></p>
<p>I stopped the mysql server right away.  I then remember, by help of a friend, that I have to remove the old log files if I change the size by changing <code>innodb_log_file_size</code>.  So I issued a command:  <code>mv ib_logfile? ..</code></p>
<p>This command moves both ib_logfile1 and ib_logfile2 down one directory.  I didn&#8217;t want to just remove &#8216;em so instead I moved them.  After that, I restarted mysql again and to my comfort, everything came back up without errors.  So lesson learned:  if you change ib_logfile size by using  <code>innodb_log_file_size</code> setting, make sure you move existing log files before you start mysql again.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
<small>DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. <strong>Use at your own risk</strong>.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/08/16/mysql-innodb-error-the-age-of-the-last-checkpoint-is-number/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MySQL:  Changing runtime variables with out restarting MySQL server.</title>
		<link>http://crazytoon.com/2007/07/23/mysql-changing-runtime-variables-with-out-restarting-mysql-server/</link>
		<comments>http://crazytoon.com/2007/07/23/mysql-changing-runtime-variables-with-out-restarting-mysql-server/#comments</comments>
		<pubDate>Mon, 23 Jul 2007 10:43:28 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Linux System]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[System admin]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/07/23/mysql-changing-runtime-variables-with-out-restarting-mysql-server/</guid>
		<description><![CDATA[I have run in to the issue where I wanted to change some configuration on our production MySQL server but did not want to restart MySQL since its in production.  One of the parameters we like to change time to time is the &#8220;log_slow_queries&#8221; so we can see what queries are slow in production environment.  [...]]]></description>
			<content:encoded><![CDATA[<p>I have run in to the issue where I wanted to change some configuration on our production MySQL server but did not want to restart MySQL since its in production.  One of the parameters we like to change time to time is the &#8220;log_slow_queries&#8221; so we can see what queries are slow in production environment.  But obviously we don&#8217;t want to restart mysql server so we start up mysql client and type: </p>
<p><code>mysql&gt; set @@long_query_time=3;<br />
Query OK, 0 rows affected (0.00 sec)</code><code>mysql&gt; show variables like 'long%';<br />
+-----------------+-------+<br />
| Variable_name | Value |<br />
+-----------------+-------+<br />
| long_query_time | 3 |<br />
+-----------------+-------+<br />
1 row in set (0.00 sec)</code></p>
<p>Once you are done checking for slow queries, you can reset it back to whatever value you want by repeating the command with higher number (default is 10).</p>
<p><code>mysql&gt; set @@long_query_time=10;<br />
Query OK, 0 rows affected (0.00 sec<br />
mysql&gt; show variables like 'long%';<br />
+-----------------+-------+<br />
| Variable_name | Value |<br />
+-----------------+-------+<br />
| long_query_time | 10 |<br />
+-----------------+-------+<br />
1 row in set (0.00 sec)</code></p>
<p>There are many more variables you can change.  Type <code>show variables </code>in the mysql client to see the list and what all those variables are set to.</p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/07/23/mysql-changing-runtime-variables-with-out-restarting-mysql-server/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Green America:  Things you can do to make world more green and in the proccess save money</title>
		<link>http://crazytoon.com/2007/04/23/green-america-things-you-can-do-make-world-more-green-and-in-proccess-save-money/</link>
		<comments>http://crazytoon.com/2007/04/23/green-america-things-you-can-do-make-world-more-green-and-in-proccess-save-money/#comments</comments>
		<pubDate>Mon, 23 Apr 2007 10:38:37 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Earth Day]]></category>
		<category><![CDATA[Green America]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Quick Tips]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/04/23/green-america-things-you-can-do-make-world-more-green-and-in-proccess-save-money/</guid>
		<description><![CDATA[There are many things we can do on daily basis which can make our lives better and make future better for our kids and ourselves.  Most of these things are overlooked or not stressed enough.  Lets start with cheap way to save some money and make world a better place.

Replace all the bulbs [...]]]></description>
			<content:encoded><![CDATA[<p>There are many things we can do on daily basis which can make our lives better and make future better for our kids and ourselves.  Most of these things are overlooked or not stressed enough.  Lets start with <em>cheap</em> way to save some money and make world a better place.</p>
<ol>
<li>Replace all the bulbs in your house with energy savings one.  Energy savings bulbs will save you upto 70% in utility bills and at the same time help out to make world greener by using less electricity.</li>
<li>Unplug appliances which are not in use.  Even some of the chargers like your laptop charger, consumes some energy while plugged in to outlet even when there is nothing is plugged in the other side.  That home entertainment system or just a cable box you have sitting around, consumes energy.  All this could add upto significant $$&#8217;s.  One of the products worth looking at:  <a href="http://www.smarthomeusa.com/Shop/Bits-Ltd./Item/LCG4/" title="Smart Strip Power Strip">Smart Strip Power Strip</a></li>
<li>If you can afford to and have a house, put solar panels on your roof.  Even though this is sort of an expensive way to go but in long (or short, depends on costs etc.) run it can save you money an maybe even make you some money.   You can also depreciate this investment just like any other investments.  Talk to your tax adviser for more details on tax credits.  You can read more details about return on your investment at: <a href="http://www.motherearthnews.com/Alternative-Energy/2006-10-01/You-Can-Afford-Solar-Power.aspx" title="Afford solar power" target="_blank">motherearthnews.com</a><a href="http://www.motherearthnews.com/Alternative-Energy/2006-10-01/You-Can-Afford-Solar-Power.aspx" target="_blank"></a></li>
<li>Windmills.  Yeah..not the coolest thing around but if you are living in an area with lots of yard space and do qualify to put windmills there, its worth looking into.</li>
<li>Buy hybrids!  Not only that you get some tax credit for buying hybrids, you can also pay A LOT less on gas stations.  Of course there are many more alternatives then hybrids.  You can also buy electric cars, cars which run on ethanol (<a href="http://www.msnbc.msn.com/id/11603199/" title="Green machines" target="_blank">http://www.msnbc.msn.com/id/11603199/</a>) and cars which are powered by solar energy.   Hybrid seems to be more mainstream choice even though for short commutes electric cars make more sense.  One of the car I know of is <a href="http://www.teslamotors.com/" title="Tesla Motors">Tesla Roadster</a> which by no means is a cheap car!  It could cost you are $100k to buy this puppy but before you say anything, check out the specs.  You also have to wait until next year to get it so <em>maybe</em> that is too much a turn off and you rather have one of the many available hybrids.</li>
<li>Use less paper!  Opt in for online statements. This alone will save a lot paper and in turn will protect the trees!  Other places you can cut back would be at your work.   Ask for your paystubs electronically instead of paper.  You really don&#8217;t need a piece of paper to see how much you made.  But if you really want to, just log in to the website and look.  File your taxes via computer using e-file.  All these are little things which does not make our life harder but helps with making sure we are not destroying our nature!</li>
<li>Use recycled material!  Promote recycling.  Ask for recycling at your work, your apartment, church, where ever you can!</li>
<li>Paper or plastic?  ALWAYS pick paper.  Plastic bags are VERY hard to recycle.</li>
<li>If you live close to stores, work or any other destinations, walk there instead of driving.  You save on some pollution and at the same time its good for you to go for walk.</li>
<li>Make your home energy efficient.  There are many ways to make this happen.  Of course you are already doing #1 by now, right?  Other ways would be to buy appliances which qualify.  Install windows which insulate your house better.  Put insulation in the outside walls.  All these will make it so you don&#8217;t use all that electricity you are producing by following #3.  Which in turn means you are pumping electricity back in the power grid and making some money from doing it!</li>
</ol>
<p>This list is very brief but should be enough to get you started and thinking about things you can do to make this world a better place to live!  Please feel free to add comments with your own suggestions, comments, etc.<br />
<small>Tagged as mysql so hopefully more people can benefit from this</small></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/04/23/green-america-things-you-can-do-make-world-more-green-and-in-proccess-save-money/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>MySQL:  ibdata files do not shrink on database deletion [innodb]</title>
		<link>http://crazytoon.com/2007/04/03/mysql-ibdata-files-do-not-shrink-on-database-deletion-innodb/</link>
		<comments>http://crazytoon.com/2007/04/03/mysql-ibdata-files-do-not-shrink-on-database-deletion-innodb/#comments</comments>
		<pubDate>Tue, 03 Apr 2007 23:07:03 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Data Backup]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL backup]]></category>
		<category><![CDATA[System admin]]></category>
		<category><![CDATA[innodb]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/04/03/mysql-ibdata-files-do-not-shrink-on-database-deletion-innodb/</guid>
		<description><![CDATA[One very interesting thing I noticed with MySQL was that if you delete a database, ibdata file doesn&#8217;t shrink by that much space to minimize disk usage.  I deleted the database and checked usage of /usr/local/mysql/var folder and noticed that ibdata file is still the same size.  So the problem I face now [...]]]></description>
			<content:encoded><![CDATA[<p>One very interesting thing I noticed with MySQL was that if you delete a database, ibdata file doesn&#8217;t shrink by that much space to minimize disk usage.  I deleted the database and checked usage of /usr/local/mysql/var folder and noticed that ibdata file is still the same size.  So the problem I face now is, how do I claim back this space?</p>
<p>After searching for a bit on google about this problem, apparently only way you can do that is by exporting your mysql databases, delete ibdata1 file, import databases.  This creates new ibdata file with correct space usage.  Atleast there is a way to get around this issue.  But honestly, too much pain on production boxes where we might be trying to remove old databases to reclaim some of the hard drive space.</p>
<p>An preventive measure one can use is to use option: <code>innodb_file_per_table</code> (&#8216;put innodb_file_per_table&#8217; in your my.cnf file under [mysqld] section).  This will create individual files for tables under database directory.  So now when I delete the database, all the space is returned since the directory is now deleted along with database along with all the tables inside the directory.  In my test after you put option <code>innodb_file_per_table </code>your my.cnf, you will have to still do export/import to be able to minimize disk usage and have the ability to delete database at your leisure without worrying about reclaiming the disk space.  Here are the steps I took.  DISCLAIMER:  Please make backup of your data and use following steps at your own risk.  Doing it on test server is <strong>HIGHLY</strong> recommended.  Please don&#8217;t come back and tell me that you lost your data because you followed my steps.  They work for <em>me</em> and they <em>may not </em>work for <em>you</em>!</p>
<p>That said, here are the steps:</p>
<ol>
<li>Add  <code>innodb_file_per_table </code>in your my.cnf file under [mysqld] section</li>
<li>run following commands at the prompt.  Your path to binaries might be different.</li>
<p><code>#note:  following assumes you are logged in as root<br />
mkdir -p /temp  #temp dir to save our sql dump<br />
#lets make a backup of current database. -p is used if there is pw set<br />
/usr/local/mysql/bin/mysqldump -R -q -p --all-databases &gt; /temp/all.sql<br />
#stop mysql so we can remove all the files in the dir<br />
/etc/init.d/mysql stop<br />
rm -fr /usr/local/mysql/var/* #remove all the files<br />
/usr/local/mysql/bin/mysql_install_db  #install default dbs<br />
#change ownership so mysql user can read/write to/from files<br />
chown -R mysql.mysql /usr/local/mysql/var/<br />
#start mysql so we can import our dump<br />
/etc/init.d/mysql start<br />
#note there is no -p since defaults don't have mysql pw set<br />
/usr/local/mysql/bin/mysql &lt; /temp/all.sql<br />
/etc/init.d/mysql restart</code></ol>
<p>This should be all you need to do.  At this point when you remove a database, it will delete the directory of the db and all the data contained within which in turn will give you your disk space back.</p>
<p>REMEMBER:  <strong>Backup your data</strong> and be smart about using code found on internet.  If you don&#8217;t know what you are doing, hire a consultant who does.</p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/04/03/mysql-ibdata-files-do-not-shrink-on-database-deletion-innodb/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>MySQL database backup file compression:  gzip vs bzip2</title>
		<link>http://crazytoon.com/2007/03/19/mysql-database-backup-file-compression-gzip-vs-bzip2/</link>
		<comments>http://crazytoon.com/2007/03/19/mysql-database-backup-file-compression-gzip-vs-bzip2/#comments</comments>
		<pubDate>Tue, 20 Mar 2007 01:12:29 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Data Backup]]></category>
		<category><![CDATA[Enterprise level solutions]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL backup]]></category>
		<category><![CDATA[System admin]]></category>
		<category><![CDATA[uh...]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/03/19/mysql-database-backup-file-compression-gzip-vs-bzip2/</guid>
		<description><![CDATA[In one of my previous posts: MySQL backups, I talked about using a script for automating backups.  I show that we can use gzip to compress backup file to compress and save.  Since then, our backup file has been growing meg or two a day which is causing our backup files to get [...]]]></description>
			<content:encoded><![CDATA[<p>In one of my previous posts: <a href="http://crazytoon.com/2007/01/23/mysql-backups-using-mysqldump/" title="MySQL Backups">MySQL backups</a>, I talked about using a script for automating backups.  I show that we can use gzip to compress backup file to compress and save.  Since then, our backup file has been growing meg or two a day which is causing our backup files to get bigger and bigger every day.  We keep hourly backups for a week so you can imagine space usage is quite high.</p>
<p>I went and did few tests to see if its beneficial for us to use Bzip2 instead of gzip.  I could&#8217;ve tried 7z also but that is not something installed on most linux machines I work on and I didn&#8217;t want use a solution which will require me to add more software.  Here are the results:</p>
<p>Original sql file size: 584M<br />
Using gzip: 166M<br />
Time spent on compressing 1 minute 25 secs<br />
Using bzip2:  125M<br />
Time spent on compressing 1 minute 50 secs</p>
<p>As we can see that it takes longer to compress but file size is much smaller (adds up fast for multiple backups).  As our database grows bigger and bigger, the size difference will matter quite a bit.  Since we also ftp the file off the server to onsite/offsite location hourly as well.</p>
<p>Does anybody know of any backup techniques which we can use for free and we can do incrementals with? comments are always welcome.  To learn more about gzip or bzip2, see <a href="http://www.lamp-tips.com/man-pages/gzip/" title="man gzip - man page for gzip" target="_blank">man gzip</a> or <a href="http://www.lamp-tips.com/man-pages/bzip2/" title="man bzip2 - man page for bzip2" target="_blank">man bzip2</a> respectively.</p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/03/19/mysql-database-backup-file-compression-gzip-vs-bzip2/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>MySQL wait_timeout setting</title>
		<link>http://crazytoon.com/2007/02/20/mysql-wait_timeout-setting/</link>
		<comments>http://crazytoon.com/2007/02/20/mysql-wait_timeout-setting/#comments</comments>
		<pubDate>Wed, 21 Feb 2007 01:17:37 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Server load]]></category>
		<category><![CDATA[System admin]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/02/20/mysql-wait_timeout-setting/</guid>
		<description><![CDATA[We were having issues with mysql threads where they would be in sleep mode and wouldn&#8217;t die off for long time.  At the same time we started having issues with our servers where the load will spike and eventually server will come to halt unless we killed all the apache processes and restarted apache [...]]]></description>
			<content:encoded><![CDATA[<p>We were having issues with mysql threads where they would be in sleep mode and wouldn&#8217;t die off for long time.  At the same time we started having issues with our servers where the load will spike and eventually server will come to halt unless we killed all the apache processes and restarted apache (which seems to be the hung application).  We traced it back eventually and noticed that the time when server hung was when it burned through all the ram and was using up all the swap also.  So we started to work backwards and tried to resolve one thing at a time.  We started with MySQL.  We put in wait_timeout = 30 in to my.cnf and restarted mysql.  Than I closely watched the server for few hours and noticed that we didn&#8217;t have any more of those sleep connections.  GREAT! A work around until we get to bottom of whats causing this.  That was on Friday.  Sat we started noticing different problem.  Problem worsened and we started to look into what might&#8217;ve caused it and found out that we had a script which was pulling row at a time, processing it, and deleting the row.  Except, it was never getting to delete the row due to timeout would kick in and close the connection.  We found this out when we watched error logs and saw:  Mysql has gone away message.</p>
<p>We took out the wait timeout and everything seems to started to work fine.  Did anybody ever notice this behavior where you would loose connection to the mysql server due to timeout?  The script which processes line by line and deletes line by line takes fraction of second to process that particular line.  Does wait timeout starts counting from the starting of the connection?  Does it mean that wait timeout is actually a max connection time limit?  Suggestions/comments?</p>
<p>Edit 5/31/09:  Friend of mine was getting this error: <code>database error: Lost connection to MySQL server during query </code><br />
he got around it by adjusting wait_timeout setting. </p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/02/20/mysql-wait_timeout-setting/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>MySQL backups using mysqldump</title>
		<link>http://crazytoon.com/2007/01/23/mysql-backups-using-mysqldump/</link>
		<comments>http://crazytoon.com/2007/01/23/mysql-backups-using-mysqldump/#comments</comments>
		<pubDate>Wed, 24 Jan 2007 00:03:28 +0000</pubDate>
		<dc:creator>Sunny Walia</dc:creator>
				<category><![CDATA[Data Backup]]></category>
		<category><![CDATA[Linux System]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL backup]]></category>
		<category><![CDATA[System admin]]></category>

		<guid isPermaLink="false">http://crazytoon.com/2007/01/23/mysql-backups-using-mysqldump/</guid>
		<description><![CDATA[MySQL backups are essential to running a site with MySQL backend.  Generally you can get away with doing nightly backups but on our site, due to couple issues we had in past, we are forced to do hourly backups of our db.
Intially I was doing backup by using: mysqldump dbname &#62; weekdayHour.dbname.sql  hourly. [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL backups are essential to running a site with MySQL backend.  Generally you can get away with doing nightly backups but on our site, due to couple issues we had in past, we are forced to do hourly backups of our db.</p>
<p>Intially I was doing backup by using: <em>mysqldump dbname &gt; weekdayHour.dbname.sql</em>  hourly.  This allowed us to have week worth of backups done every hour and auto overwriting old backups.  Eventually we added stored procedures and triggers to the mix and all of the sudden this dump wasn&#8217;t getting all the stored procedures and triggers.  So we started using mysqldump with -R parameter which in man states:</p>
<blockquote><p>Dump stored routines (functions and procedures) from the dumped databases.</p></blockquote>
<p>Recently when we started to see more and more traffic, we noticed that our server was under heavy load on the hour.  Ofcourse we quickly found it was due to mysqldump running on the hour which was causing the lag.  Eventually we had enough traffic on the site where this was causing connection problems with mysql.  So I went through man mysqldump again to find solution.  And thanks to those smart people in mysqldump dev team, there I found couple more parameters to make this backup process little less painful.  At this point we are using:  <em>mysqldump -R  -q &#8211;single-transaction &gt; weekdayHour.dbname.sql</em>  This seems to have decreased the load on the server and we haven&#8217;t got errors connecting to mysql.  Since we use innodb tables in this particular db, we could use <em>single-transaction</em> parameter.  Here is what our friendly man mysqldump has to say about these two parameters:</p>
<blockquote><p> -q    &#8230;This option is useful for dumping large tables. It forces mysqldump to retrieve rows for a table from the   server a row at a time rather than retrieving the entire row set and buffering it in memory before writing   it out&#8230;</p>
<p>&#8211;single-transaction   &#8230;This option issues a BEGIN SQL statement before dumping data from the server. It is useful only with transactional tables such as InnoDB and BDB, because then it dumps the consistent state of the database at the time when BEGIN was issued without blocking any applications&#8230;</p></blockquote>
<p>If you are thinking about using these two parameters, please spend couple minutes reading through man mysqldump and look at other notes which might pertain to your setup.</p>
<p>One last but very important thing we do after our backups are run is to move the new backup files off the server just in case server dies.  I achieved this by using ncftp package which includes ncftpput command line utility.  With ncftp, you can store ip/login/pw in a file and tell ncftp to use that for login information.  Lets look at the script as a whole:</p>
<p>NOTE:  comments in this script is for information purposes only.  You can remove them if you like</p>
<p><code>#!/bin/bash<br />
#<br />
#<a href="http://crazytoon.com/2007/01/23/mysql-backups-using-mysqldump/">http://crazytoon.com/2007/01/23/mysql-backups-using-mysqldump/</a><br />
#<br />
DATE=`date '+%u%H'` # this sets up weekly rotation of backup files<br />
BACKUP_DIR="/admin/backups/domain.com/" # this dir will be created if it doesn't exist<br />
HOST=`hostname` #you may want to hard code this if you hostname returns wrong information<br />
mkdir ${BACKUP_DIR}mysql/ -p<br />
/usr/local/mysql/bin/mysqldump -R  -q --single-transaction --databases dbname1 dbname2 -ppassword &gt; ${BACKUP_DIR}mysql/$HOST$DATE.sql<br />
rm ${BACKUP_DIR}mysql/$HOST$DATE.sql.gz &gt; /dev/null 2&gt;&amp;1<br />
gzip -9 ${BACKUP_DIR}mysql/$HOST$DATE.sql &gt; /dev/null 2&gt;&amp;1<br />
/usr/bin/ncftpput -R -f ${BACKUP_DIR}hostinfo / ${BACKUP_DIR}mysql/$HOST$DATE.sql.gz</code></p>
<p>This is what your hostinfo file should contain (just make sure you edit it and put your own server ip, username, and password):<br />
<code>host 123.123.123.123<br />
user loginname<br />
pass loginpassword</code></p>
<p>To read more about mysqldump, see <a href="http://www.lamp-tips.com/man-pages/mysqldump/" title="man mysqldump - man page for mysqldump" target="_blank">man mysqldump</a></p>
]]></content:encoded>
			<wfw:commentRss>http://crazytoon.com/2007/01/23/mysql-backups-using-mysqldump/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

