<?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>Life Scaling &#187; MySQL</title>
	<atom:link href="http://orensol.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://orensol.com</link>
	<description>Oren Solomianik's Blog</description>
	<lastBuildDate>Mon, 21 Jun 2010 08:10:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ERROR 2026 (HY000): SSL connection error &#8212; The Solution</title>
		<link>http://orensol.com/2010/06/21/error-2026-hy000-ssl-connection-error-the-solution/</link>
		<comments>http://orensol.com/2010/06/21/error-2026-hy000-ssl-connection-error-the-solution/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 08:09:40 +0000</pubDate>
		<dc:creator>Oren</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[ERROR 2026]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://orensol.com/?p=303</guid>
		<description><![CDATA[<p>If you&#8217;re using mysql server with SSL, and ever encountered this error whenever you try to access the database after it&#8217;s running:</p>
ERROR 2026 (HY000): SSL connection error
<p>One thing to check is if your client certificate and server certificate have the same common name. You&#8217;ve probably went through the <a href="http://dev.mysql.com/doc/refman/5.0/en/secure-create-certs.html" target="_blank">certificate generation procedure</a>, and (like [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re using mysql server with SSL, and ever encountered this error whenever you try to access the database after it&#8217;s running:</p>
<pre>ERROR 2026 (HY000): SSL connection error</pre>
<p>One thing to check is if your client certificate and server certificate have the same common name. You&#8217;ve probably went through the <a href="http://dev.mysql.com/doc/refman/5.0/en/secure-create-certs.html" target="_blank">certificate generation procedure</a>, and (like I did) just entered the same common name for both without noticing.</p>
<p>This is a nasty error message that doesn&#8217;t tell you anything, and there&#8217;s nothing in the error log to imply what went wrong. So remember &#8211; when generating your own certificates for a mysql server, use different common names for client and server!</p>
]]></content:encoded>
			<wfw:commentRss>http://orensol.com/2010/06/21/error-2026-hy000-ssl-connection-error-the-solution/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Cron Script To Snapshot Any Attached EBS Volume</title>
		<link>http://orensol.com/2009/08/10/cron-script-to-snapshot-any-attached-ebs-volume/</link>
		<comments>http://orensol.com/2009/08/10/cron-script-to-snapshot-any-attached-ebs-volume/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 08:12:40 +0000</pubDate>
		<dc:creator>Oren</dc:creator>
				<category><![CDATA[Cloud Computing]]></category>
		<category><![CDATA[EC2]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Scaling]]></category>
		<category><![CDATA[Automatic]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[EBS]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Snapshot]]></category>
		<category><![CDATA[Volume]]></category>
		<category><![CDATA[XFS]]></category>

		<guid isPermaLink="false">http://orensol.com/?p=267</guid>
		<description><![CDATA[<p>If you would like to cron snapshots of any attached volume to an instance, you can use the following script. It uses the EC2 command line tools to see what volumes are currently attached to this instance, and takes a snapshot. Make sure to replace all the variables on the top of the script to [...]]]></description>
			<content:encoded><![CDATA[<p>If you would like to cron snapshots of any attached volume to an instance, you can use the following script. It uses the EC2 command line tools to see what volumes are currently attached to this instance, and takes a snapshot. Make sure to replace all the variables on the top of the script to match your own.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">JAVA_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>java<span style="color: #000000; font-weight: bold;">/</span>default
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">EC2_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>vol<span style="color: #000000; font-weight: bold;">/</span>snap<span style="color: #000000; font-weight: bold;">/</span>ec2-api-tools-<span style="color: #000000;">1.3</span>-<span style="color: #000000;">26369</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">EC2_PRIVATE_KEY</span>=<span style="color: #7a0874; font-weight: bold;">&#91;</span>PATH-TO-YOUR-PRIVATE-KEY<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">EC2_CERT</span>=<span style="color: #7a0874; font-weight: bold;">&#91;</span>PATH-TO-YOUR-CERT<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_ACCESS_KEY_ID</span>=<span style="color: #ff0000;">&quot;[YOUR-ACCESS-KEY]&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_SECRET_ACCESS_KEY</span>=<span style="color: #ff0000;">&quot;[YOUR-SECRET-KEY]&quot;</span>
&nbsp;
<span style="color: #007800;">INSTANCE_ID</span>=<span style="color: #000000; font-weight: bold;">`</span>curl <span style="color: #660033;">-s</span> http:<span style="color: #000000; font-weight: bold;">//</span>169.254.169.254<span style="color: #000000; font-weight: bold;">/</span>latest<span style="color: #000000; font-weight: bold;">/</span>meta-data<span style="color: #000000; font-weight: bold;">/</span>instance-id<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Instance ID is <span style="color: #007800;">$INSTANCE_ID</span>&quot;</span>
<span style="color: #007800;">VOLUMES</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$EC2_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ec2-describe-volumes <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;ATTACHMENT&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$INSTANCE_ID</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2}'</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Volumes are: <span style="color: #007800;">$VOLUMES</span>&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> VOLUME <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$VOLUMES</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Snapping Volume <span style="color: #007800;">$VOLUME</span>&quot;</span>
        <span style="color: #007800;">DEVICE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$EC2_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ec2-describe-volumes <span style="color: #007800;">$VOLUME</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;ATTACHMENT&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$INSTANCE_ID</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $4}'</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Device is <span style="color: #007800;">$DEVICE</span>&quot;</span>
        <span style="color: #007800;">MOUNTPOINT</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">df</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$DEVICE</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $6}'</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Mountpoint is <span style="color: #007800;">$MOUNTPOINT</span>&quot;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># Snapshot</span>
        <span style="color: #007800;">SNAPSHOT_ID</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$EC2_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ec2-create-snapshot <span style="color: #007800;">$VOLUME</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Snapshotted: <span style="color: #007800;">$SNAPSHOT_ID</span>&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>If you&#8217;re wondering why $MOUNTPOINT is important (it&#8217;s not used here after all), it&#8217;s because you might want to freeze your filesystem if it&#8217;s XFS, so you could safely take a snapshot of a MySQL database for example. So you could easily wrap the snapshot create command with this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">        <span style="color: #666666; font-style: italic;"># freeze</span>
        xfs_freeze <span style="color: #660033;">-f</span> <span style="color: #007800;">$MOUNTPOINT</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># Snapshot</span>
        <span style="color: #007800;">SNAPSHOT_ID</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$EC2_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ec2-create-snapshot <span style="color: #007800;">$VOLUME</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># unfreeze</span>
        xfs_freeze <span style="color: #660033;">-u</span> <span style="color: #007800;">$MOUNTPOINT</span></pre></div></div>

<p>And if you are indeed using this script to snapshot a volume with MySQL on it, you need also to flush tables with read lock, and gather information on master and slave positions. For this task you can use <a href="http://twitter.com/esh" target="_blank">Eric Hammond</a>&#8217;s <a href="http://ec2-snapshot-xfs-mysql.notlong.com/" target="_blank">script</a>, and incorporate it to the cron script. (You can read more about MySQL and XFS on EC2 <a href="http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1663" target="_blank">on the AWS site</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://orensol.com/2009/08/10/cron-script-to-snapshot-any-attached-ebs-volume/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Connecting Several PHP Processes To The Same MySQL Transaction</title>
		<link>http://orensol.com/2009/04/20/connecting-several-php-processes-to-the-same-mysql-transaction/</link>
		<comments>http://orensol.com/2009/04/20/connecting-several-php-processes-to-the-same-mysql-transaction/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 09:55:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Commit]]></category>
		<category><![CDATA[InnoDB]]></category>
		<category><![CDATA[Multi-Process]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rollback]]></category>
		<category><![CDATA[Semaphore]]></category>
		<category><![CDATA[Transaction]]></category>
		<category><![CDATA[XML-RPC]]></category>

		<guid isPermaLink="false">http://orensol.com/?p=198</guid>
		<description><![CDATA[<p>The following concept is still under examination, but my initial tests proved successful, so I thought it&#8217;s time to share.</p>
<p>Here is a problem: There is a batch job reading remote data through XML-RPC, and updating a local MySQL database according to the XML-RPC responses. The db is InnoDB, and the entire batch job should be [...]]]></description>
			<content:encoded><![CDATA[<p>The following concept is still under examination, but my initial tests proved successful, so I thought it&#8217;s time to share.</p>
<p>Here is a problem: There is a batch job reading remote data through XML-RPC, and updating a local MySQL database according to the XML-RPC responses. The db is InnoDB, and the entire batch job should be transacted. That is, in any case of failure, there should be rollback, and on success there should be commit.</p>
<p>So, the simple way is of course a single process script that uses a linear workflow:</p>
<ol>
<li>START TRANSACTION locally.</li>
<li>Make XML-RPC request, fetch data.</li>
<li>Insert data into db as needed.</li>
<li>Repeat 2-3 until Error or Finish.</li>
<li>If Error ROLLBACK, if Finish COMMIT.</li>
</ol>
<p>This works, but you may notice a bottleneck, being the XML-RPC request. It&#8217;s using http, and it&#8217;s connecting to a remote server. Sometimes the XML-RPC server also takes time to perform the work that generates the response. Add the network latency, and you get a single process that most of the time sits idle and waits for response.</p>
<p>So if we have a process that just sits and waits most of the time, let&#8217;s spread its work over several processes, and assume that while most of the processes will be waiting, at least one can be free to deal with the local database. This way we will get maximum utilization of our resources.</p>
<p>So the multi-process workflow:</p>
<ol>
<li>START TRANSACTION locally.</li>
<li>Fork children as necessary.</li>
<li>From child, make XML-RPC request, fetch data.</li>
<li>From child, acquire database access through semaphore.</li>
<li>From child, insert data into db as needed.</li>
<li>From child, release database access through semaphore.</li>
<li>From child, repeat 3-6 until Error or Finish.</li>
<li>From parent, monitor children until Error or Finish.</li>
<li>From parent, if Error ROLLBACK, if FINISH COMMIT.</li>
</ol>
<p>Now, the workflow seems all and well in theory, but can it work in practice? Can we connect to the same transaction from several different PHP processes?</p>
<p>I was surprised to find out that the answer is positive. As long as all processes share the same connection resource, they all use the same connection. And in MySQL, <a href="http://dev.mysql.com/doc/refman/5.0/en/commit.html">the same connection means the same transaction</a>, given that a transaction was started and not yet committed or rolled back (either explicitly or implictly).</p>
<p>The secret is to create the connection resource with the parent, and when <a href="http://us.php.net/manual/en/function.pcntl-fork.php">forking</a> children, they have a reference to the same connection. The caveat is that they must access the resource atomically, otherwise unexpected behavior occurs (usually the connection hangs, I am guessing that it is when one child tries to read() from the socket and the other to write() to it). So in order to streamline the access to the db connection, we use a <a href="http://us.php.net/manual/en/book.sem.php">semaphore</a>. Each child can access the connection only when it&#8217;s available, and it&#8217;s blocking if not available.</p>
<p>In the end of the workflow, our parent process acts much like a Transaction Manager in an <a href="http://dev.mysql.com/doc/refman/5.0/en/xa.html" target="_blank">XA Transaction</a>, and according to what the children report, decides whether to commit or rollback.</p>
<p>Here is a proof of concept code (not tested in this version, but similar code tested and succeeded):</p>
<h3>The DBHandler Class</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> DBHandler
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$link</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$sem</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">const</span> SEMKEY <span style="color: #339933;">=</span> <span style="color: #0000ff;">'123456'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbname</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #000088;">$new_link</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$client_flags</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #000088;">$new_link</span><span style="color: #339933;">,</span> <span style="color: #000088;">$client_flags</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#41;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Could not connect to db. MySQL error was: '</span><span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$isDb</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbname</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$isDb</span><span style="color: #009900;">&#41;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Could not select db. MySQL error was: '</span><span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> enterSemaphore<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sem</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sem_get</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">SEMKEY</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">sem_acquire</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sem</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> exitSemaphore<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">sem_release</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sem</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">enterSemaphore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_unbuffered_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #009900;">&#41;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Could not query: {'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sql</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'}. MySQL error was: '</span><span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span> <span style="color: #339933;">===</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// INSERT, UPDATE, etc..., no result set</span>
			<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// SELECT etc..., we have a result set</span>
			<span style="color: #000088;">$retArray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #000088;">$retArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">mysql_free_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$retArray</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exitSemaphore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> beginTransaction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SET AUTOCOMMIT = 0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SET NAMES utf8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'START TRANSACTION'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rollback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ROLLBACK'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> commit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'COMMIT'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>The Forking Process</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pid</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'initial'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$maxProcs</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$maxProcs</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	 <span style="color: #000088;">$maxProcs</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$runningProcs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will be $runningProcs[pid] = status;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PRIORITY_SUCCESS'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'-20'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PRIORITY_FAILURE'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'-19'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
try 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$dbh</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DBHandler<span style="color: #009900;">&#40;</span>DBHOST<span style="color: #339933;">,</span>DBNAME<span style="color: #339933;">,</span>DBUSER<span style="color: #339933;">,</span>DBPASS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// fork all needed children</span>
		<span style="color: #000088;">$currentProcs</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pid</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$currentProcs</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$maxProcs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$pid</span> <span style="color: #339933;">=</span> pcntl_fork<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$currentProcs</span><span style="color: #339933;">++;</span>
			<span style="color: #000088;">$runningProcs</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$pid</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pid</span><span style="color: #339933;">==-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fork failed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pid</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// parent</span>
			<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;+++ in parent +++<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;+++ children are: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$runningProcs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// wait for children</span>
			<span style="color: #666666; font-style: italic;">// NOTE -- here we do it with priority signaling</span>
			<span style="color: #666666; font-style: italic;">// @TBD -- posix signaling or IPC signaling.</span>
			<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$runningProcs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span>PRIORITY_FAILURE<span style="color: #339933;">,</span><span style="color: #000088;">$runningProcs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;+++ some child failed, finish waiting for children +++<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$runningProcs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$child_pid</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$status</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$runningProcs</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$child_pid</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> pcntl_getpriority<span style="color: #009900;">&#40;</span><span style="color: #000088;">$child_pid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;+++ children status: <span style="color: #006699; font-weight: bold;">$child_pid</span>, <span style="color: #006699; font-weight: bold;">$status</span> +++<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;+++ checking if should commit or rollback +++<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span>PRIORITY_FAILURE<span style="color: #339933;">,</span><span style="color: #000088;">$runningProcs</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$runningProcs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>		
				<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;+++ some child had problem! rollback! +++<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rollback</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;+++ all my sons successful! committing! +++<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// signal all children to exit</span>
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$runningProcs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$child_pid</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$status</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;+++ killing child <span style="color: #006699; font-weight: bold;">$child_pid</span> +++<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #990000;">posix_kill</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$child_pid</span><span style="color: #339933;">,</span>SIGTERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// child</span>
			<span style="color: #000088;">$mypid</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getmypid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;--- in child <span style="color: #006699; font-weight: bold;">$mypid</span> ---<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//sleep(1);</span>
			<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;--- child <span style="color: #006699; font-weight: bold;">$mypid</span> current priority is &quot;</span> <span style="color: #339933;">.</span> pcntl_getpriority<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; ---<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// NOTE -- following queries do not work, for example only</span>
			<span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select ...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;--- child <span style="color: #006699; font-weight: bold;">$mypid</span> finished, setting priority to success and halting ---<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			pcntl_setpriority<span style="color: #009900;">&#40;</span>PRIORITY_SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;--- child <span style="color: #006699; font-weight: bold;">$mypid</span> waiting to be killed ---<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> 
catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// output error</span>
	<span style="color: #990000;">print</span> <span style="color: #0000ff;">&quot;Error!: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// if parent -- rollback, signal children to exit</span>
	<span style="color: #666666; font-style: italic;">// if child  -- make priority failure to signal</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pid</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// rollback</span>
		<span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rollBack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$runningProcs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$child_pid</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$status</span><span style="color: #009900;">&#41;</span>
			<span style="color: #990000;">posix_kill</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$child_pid</span><span style="color: #339933;">,</span>SIGTERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		pcntl_setpriority<span style="color: #009900;">&#40;</span>PRIORITY_FAILURE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$mypid</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getmypid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;--- child <span style="color: #006699; font-weight: bold;">$mypid</span> waiting to be killed ---<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Well, all of this sounds well, and also worked well on a development environment. But it should be taken out of the lab and tested on a production environment. Once I give it a shot, I will update with benchmarks.</p>
]]></content:encoded>
			<wfw:commentRss>http://orensol.com/2009/04/20/connecting-several-php-processes-to-the-same-mysql-transaction/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zend Framework Database Admin</title>
		<link>http://orensol.com/2009/04/02/zend-framework-database-admin/</link>
		<comments>http://orensol.com/2009/04/02/zend-framework-database-admin/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 11:23:58 +0000</pubDate>
		<dc:creator>Oren</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Admin]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[Interface]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[phpMyAdmin]]></category>
		<category><![CDATA[zdbform]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://orensol.com/?p=179</guid>
		<description><![CDATA[<p>If you&#8217;re looking for a simple tool that uses Zend Framework&#8217;s robust database classes (such as Zend_Db and Zend_Db_Table), you can check out <a href="http://www.gregphoto.net/index.php/2006/03/25/zdbform-simple-database-admin-forms-with-the-zend-framework/" target="_blank">zdbform</a>. It&#8217;s a short yet effective library that let&#8217;s you perform simple administration tasks on your database, with minimal coding.</p>
<p>It&#8217;s not a full blown phpMyAdmin, but it&#8217;s a simple way [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re looking for a simple tool that uses Zend Framework&#8217;s robust database classes (such as Zend_Db and Zend_Db_Table), you can check out <a href="http://www.gregphoto.net/index.php/2006/03/25/zdbform-simple-database-admin-forms-with-the-zend-framework/" target="_blank">zdbform</a>. It&#8217;s a short yet effective library that let&#8217;s you perform simple administration tasks on your database, with minimal coding.</p>
<p>It&#8217;s not a full blown phpMyAdmin, but it&#8217;s a simple way to view, edit and add your tables rows in a web interface. Also, don&#8217;t expect it to scale, because I am sure this library was written to serve some quick table administration needs, and that it is not ready to handle large datasets. But, it is very convenient if you have a small database to administer.</p>
<p>I implemented it with the Zend MVC components, and following is a brief overview.</p>
<p>In the front controller, or front plugin, or any class that your controller subclasses:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span> <span style="color: #339933;">=</span> Zend_Db<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Pdo_Mysql'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'host'</span>     <span style="color: #339933;">=&gt;</span> DB_HOST<span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> DB_USER<span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> DB_PASS<span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'dbname'</span>   <span style="color: #339933;">=&gt;</span> DB_NAME
		<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Zend_Db_Table_Abstract<span style="color: #339933;">::</span><span style="color: #004000;">setDefaultAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then set your controller and view scripts as necessary. Let&#8217;s say you have two tables to admin, &#8220;clients&#8221; and &#8220;history&#8221;. First make sure they are declared as subclasses of Zend_Db_Table:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;Zend/Db/Table/Abstract.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Clients <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Table_Abstract
<span style="color: #009900;">&#123;</span>
    protected <span style="color: #000088;">$_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'clients'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> History <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Table_Abstract
<span style="color: #009900;">&#123;</span>
    protected <span style="color: #000088;">$_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'history'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Your controller would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;Zend/Controller/Action.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> AdminController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'zdbform/zdbform.class.php'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'zdbform/zdbform_widgets.class.php'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'zdbform/zdbform_validations.php'</span><span style="color: #339933;">;</span>
&nbsp;
		parent<span style="color: #339933;">::</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headLink</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendStylesheet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/zdbform/zdbform.css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">viewRenderer</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clientsAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbform</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zdbform<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Clients'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbform</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setWidget</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'textarea'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbform</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">processForms</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> historyAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbform</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zdbform<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'History'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbform</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">processForms</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And the single view script you need is admin/index.phtml:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headLink</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">&quot;Zend/Filter/Word/CamelCaseToDash.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">&quot;Zend/Filter/Word/CamelCaseToUnderscore.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cctd</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Word_CamelCaseToDash<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cctu</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Word_CamelCaseToUnderscore<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$classes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_declared_classes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$classes</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_subclass_of</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Zend_Db_Table_Abstract'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;a href=&quot;/admin/<span style="color: #000000; font-weight: bold;">&lt;?=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cctd</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cctu</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;&amp;nbsp;&amp;nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbform</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;h1&gt;Table: <span style="color: #000000; font-weight: bold;">&lt;?=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbform</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tableName</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h1&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbform</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">showForms</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbform</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">showTable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>There were also a couple things that needed changing in the zdbform class itself:</p>
<ul>
<li>Replace all PHP_SELF with REQUEST_URL. On the mvc case, PHP_SELF is empty or index.php, and we don&#8217;t want all the forms posted there, we want them to go back to /admin/clients or /admin/history</li>
<li>After this line

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pk</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tableInfo</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'primary'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I had to add this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pk</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pk</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pk</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>zdbform-&gt;orderBy is treated as a single column, of you want multiple column sorting you have to hack a bit with getAllRows().</li>
</ul>
<p>That&#8217;s it, point your browser to /admin and you&#8217;re good to go. In a very short time and with a little bit of code, you can get something similar to a stripped down version of phpMyAdmin, using the power of Zend Framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://orensol.com/2009/04/02/zend-framework-database-admin/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>MMM (Mysql Master-Master Replication) on EC2</title>
		<link>http://orensol.com/2009/02/01/mmm-mysql-master-master-replication-on-ec2/</link>
		<comments>http://orensol.com/2009/02/01/mmm-mysql-master-master-replication-on-ec2/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 16:21:23 +0000</pubDate>
		<dc:creator>Oren</dc:creator>
				<category><![CDATA[Cloud Computing]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Scaling]]></category>
		<category><![CDATA[Amazon]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[DNS]]></category>
		<category><![CDATA[EC2]]></category>
		<category><![CDATA[master-master]]></category>
		<category><![CDATA[master-slave]]></category>
		<category><![CDATA[MMM]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://orensol.com/?p=61</guid>
		<description><![CDATA[<p>Maintaining a MySQL high availablity cluster is one of the first missions encountered when scaling web applications. Very quickly your application gets to the point where the one database machine you have is not enough to handle the load, and you need to make sure that when failure happens (and it always happens), your cluster [...]]]></description>
			<content:encoded><![CDATA[<p>Maintaining a MySQL high availablity cluster is one of the first missions encountered when scaling web applications. Very quickly your application gets to the point where the one database machine you have is not enough to handle the load, and you need to make sure that when failure happens (and it always happens), your cluster is ready to failover gracefully.</p>
<h2><strong>Some basic MySQL replication paradigms</strong></h2>
<p>MySQL master-slave replication was one of the first architectures used for failovers. The rationale is that if a master fails. a slave can be promoted to master, and start handle the writes. For this you could use several combinations of ip tools and monitoring software, for example, iproute and nagios, or heartbeat and mon.</p>
<p>However, master-slave architecure for MySQL replication has several flaws, most notable are:</p>
<ul>
<li>The need to manually take care of bringing the relegated master back to life, as a slave to the now-promoted master (this can be scripted, but usually contains many pitfalls when trying to automate as a script).</li>
<li>The possibility of failover during a crash, which can result in same transaction being committed both on the old master and the new master. Good luck then, when trying to bring back the master as a slave. You&#8217;ll most likely get some duplicate key failure because of auto increments on the last transaction when starting replication again, and then the whole database on the relegated master is useless.</li>
<li>The inablity to switch roles quickly. Say the master is on a better machine than the slave, and now there was a failover. How can you easily restore the situation the way it was before, with the master being on the better machine? Double the headache.</li>
</ul>
<p>Along came master-master architecture, which in essence is an architecture which keeps two live masters at all times, with one being a hot standby for the other, and switching between them is painless. (Baron Schwartz has a <a href="http://www.mysqlperformanceblog.com/2009/02/01/fast-mysql-master-master-failover-with-select-mirroring/trackback" target="_blank">very interesting post</a> about why referring to master-master replication as a &#8220;hot&#8221; standby could be dangerous, but this is out of the scope of this post). One of the important things that lies in the bottom of this paradigm, is that every master works in its own scope in regards to auto-increment keys, thanks to the configuration settings <a href="http://dev.mysql.com/doc/refman/5.0/en/replication-options-master.html#sysvar_auto_increment_increment" target="_blank">auto_increment_increment</a> and <a href="http://dev.mysql.com/doc/refman/5.0/en/replication-options-master.html#sysvar_auto_increment_offset" target="_blank">auto_increment_offset</a>. For example, say you have two masters, call them db1 and db2, then db1 works on the odd auto-increments, and db2 on the even auto-increments. Thus the problem of duplication on auto increment keys is avoided.</p>
<h2><strong>Managing master-master replication with MMM</strong></h2>
<p>Master-master replication is easily managed by a great piece of perl code called <a href="http://code.google.com/p/mysql-master-master/" target="_blank">MMM</a>. Written initially by <a href="http://blog.kovyrin.net/mysql-master-master-replication-manager/" target="_blank">Alexey Kovyrin</a>, and now maintained and being installed daily in production environments by <a href="http://www.precona.com" target="_blank">percona</a>, MMM is a management daemon for master-master clusters. It&#8217;s a convenient and reliable cluster management tool, which simplifies switching roles between machines in the cluster, takes care of their monitoring and health issues, and prevents you from doing stupid mistakes (like making an offline server an active master&#8230;).</p>
<p>And now comes the hard, <a href="http://aws.amazon.com/ec2/" target="_blank">EC2</a> part. Managing high availability MySQL clusters is always based on the ability to control machines ip addresses in the internal network. You set up a floating internal ip address for each of the masters, configure your router to handle these addresses, and you&#8217;re done. When the time comes to failover, the passive master sends ARP and takes over the active master&#8217;s ip address, and everything swtiches smoothly. It&#8217;s just that on EC2, the router part or any internal ip address can not be determined by you (I was told <a href="http://www.flexiscale.com/" target="_blank">FlexiScale</a> gives you the option to control ip addresses on the internal network, but I never got to testing it).</p>
<h2><strong>So how can we use MMM on EC2 for master-master replication?</strong></h2>
<p>One way is to try using EC2&#8217;s elastic ip feature. The problem with it, is that currently moving an elastic ip address from one instance to another, takes several minutes. Imagine a failover from active master to passive master in which you would have to wait several minutes for the application to respond again &#8212; not acceptable.</p>
<p>Another way is to use name resolving instead of ip addresses. This has <strong>major</strong> drawbacks, especially the non reliable nature of the DNS. But it seems to work if you can set up a name resolver that serves your application alone, and use the MMM ns_agent contribution I wrote. You can <a href="http://code.google.com/p/mysql-master-master/source/checkout" target="_blank">check out MMM source</a>, and install the ns_agent module according to contrib/ns_agent/README.</p>
<p>I am happy to say that I currently have a testing cluster set up on EC2 using this feature, and up until now it worked as expected, with the exception of several false-positive master switching due to routing issues (ping failed). Any questions or comments on the issue are welcome, and you can also post to the <a href="http://groups.google.com/group/mmm-devel" target="_blank">devolpement group</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://orensol.com/2009/02/01/mmm-mysql-master-master-replication-on-ec2/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>MySQL off-site replication and timezones</title>
		<link>http://orensol.com/2009/01/15/mysql-off-site-replication-and-timezones/</link>
		<comments>http://orensol.com/2009/01/15/mysql-off-site-replication-and-timezones/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 09:17:58 +0000</pubDate>
		<dc:creator>Oren</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[replication]]></category>
		<category><![CDATA[slave]]></category>
		<category><![CDATA[timezone]]></category>

		<guid isPermaLink="false">http://orensol.com/?p=21</guid>
		<description><![CDATA[<p>Recently we were doing some testing on a mysql slave server which was located at an off-site location from the master server. While the on-site slave was having no problems replicating, the off-site slave would once a day break replication on a duplicate entry error because of a unique key insert problem.</p>
<p>This was very weird, [...]]]></description>
			<content:encoded><![CDATA[<p>Recently we were doing some testing on a mysql slave server which was located at an off-site location from the master server. While the on-site slave was having no problems replicating, the off-site slave would once a day break replication on a duplicate entry error because of a unique key insert problem.</p>
<p>This was very weird, especially because the on-site slave was having no trouble at all. After some brainstorming aided by <a href="http://www.percona.com/" target="_blank">percona</a>, we realized the cause. The off-site slave was in a different timezone than the other master and slave. This fact together with the fact that we had a unique key that contained curdate(), caused the following scenario:</p>
<ul>
<li>On January 3, 23:57, there was an insert on table t, unique key was 2009-01-03.</li>
<li>On January 4, 00:01, there was an insert on table t, unique key was 2009-01-04</li>
</ul>
<p>No problem, but &#8212; times are replicated as timestamps, so on the off-site slave, which was 1 hour ahead (EST instead of CST):</p>
<ul>
<li>First insert was replicated as January 4, 00:57, insert on table t, unique key 2009-01-04</li>
<li>Second insert was replicated as January 4, 01:01, insert on table t, and error on unique key for 2009-01-04</li>
</ul>
<p>The solution is either to set the whole machine&#8217;s timezone to the timezone of the master you&#8217;re replicating, or using the same <a href="http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html" target="_blank">time-zone and default-time-zone settings</a> for the mysql server.</p>
]]></content:encoded>
			<wfw:commentRss>http://orensol.com/2009/01/15/mysql-off-site-replication-and-timezones/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
