<?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>MySQL How 2 &#187; PhP</title>
	<atom:link href="http://www.mysqlhow2.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlhow2.com</link>
	<description>MySQL DBA information and helpful tips and tricks to make life easier.</description>
	<lastBuildDate>Wed, 14 Jul 2010 15:54:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=2496</generator>
		<item>
		<title>Following your replication flow</title>
		<link>http://www.mysqlhow2.com/2010/02/02/following-your-replication-flow/</link>
		<comments>http://www.mysqlhow2.com/2010/02/02/following-your-replication-flow/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 15:49:55 +0000</pubDate>
		<dc:creator>Lee Thompson</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PhP]]></category>
		<category><![CDATA[flow chart]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://www.mysqlhow2.com/?p=179</guid>
		<description><![CDATA[Many environments I manage are large and span over multiple data centers across the world. We replicate data across and sometimes need to have a visual aid to help look at the replication flow. This simple PhP script allows me to see the replication flow starting from master to all slaves connected. You can start [...]]]></description>
			<content:encoded><![CDATA[<p>Many environments I manage are large and span over multiple data centers across the world. We replicate data across and sometimes need to have a visual aid to help look at the replication flow. This simple PhP script allows me to see the replication flow starting from master to all slaves connected. You can start from any server but the results will be master first.</p>
<pre class="brush: php;">
&lt;?

#################################
######  Mysql Replication Mapper #########
######     Lee Thompson ##############
######      Version 1.0   ###############
#################################

$user = &quot;&lt;username&gt;&quot;;
$pass = &quot;&lt;password&gt;&quot;;

$masterserver = $_REQUEST['master_server'];

$masterport = $_REQUEST['master_port'];
if ($masterport == '')
$masterport = '3306';

echo &quot;Please enter a master server&quot;;
echo &quot;&lt;form method='post' action='$php_self'&gt;&quot;;
echo &quot;Server: &lt;input type ='text' name='master_server' value='&quot; . $masterserver . &quot;'&gt;&lt;br /&gt;&quot;;
echo &quot;Port: &lt;input type ='text' name='master_port' value='3306'&gt;&lt;br /&gt;&quot;;
echo &quot;&lt;input type ='submit'&gt;&lt;hr /&gt;&quot;;

$serverlist = array();

// fill data
$serverlist = query_slave_server($masterserver.&quot;:&quot;.$masterport);

// print data
print &quot;&lt;b&gt;Master Server: &lt;/b&gt;&quot;.$serverlist.&quot; on Port: &quot;.$masterport . '&lt;br /&gt;';

$array = query_server($serverlist);
print_server($array, 0);

function query_slave_server($server)
{
  global $user, $pass, $masterport, $masterserver;

  $sql =&quot;show processlist&quot;;
        $foundmaster = FALSE;
        while ( $foundmaster === FALSE )
        {
    $con = mysql_connect($server.&quot;:&quot;.$masterport,$user,$pass) or die($server. mysql_error());
                $result = mysql_query($sql,$con);
                while ($row = mysql_fetch_array($result))
                {
                        $slaveuser = $row['User'];
                        if ($slaveuser=='system user')
                        {
                                $sql2 = &quot;show slave status&quot;;
                                $result = mysql_query($sql2,$con);
                                while ($row2 = mysql_fetch_array($result))
                                {
                                        $smasterserver = $row2['Master_Host'];
                                        $smasterport = $row2['Master_Port'];
                                        $server = $row2['Master_Host'];
                                        $masterport = $row2['Master_Port'];
                                }
                                break;
                        }
                }
                if ( $slaveuser != 'system user' )
                {
                        $foundmaster = TRUE;
                }
        }
        return $server;
}

function query_server($mserver)
{
  global $user, $pass, $masterport;

  $sql =&quot;show processlist&quot;;

  $con = @mysql_connect($mserver.&quot;:&quot;.$masterport,$user,$pass) or die(mysql_error());
  $result = @mysql_query($sql,$con);
  while ($row = @mysql_fetch_array($result))
  {
    $slave = $row['Host'];
    $slavehost = ereg_replace(&quot;:.*&quot;, &quot;&quot;, $slave);
    $slavecmd = $row['Command'];
    if ($slavecmd=='Binlog Dump')
    {
      $current[$slavehost] = query_server($slavehost.&quot;:&quot;.$masterport);
    }
  }
  return $current;
}

function print_server($current, $depth)
{
  global $user, $pass;

  foreach ($current as $slave =&gt; $ary)
  {
    $conn2 = mysql_connect($slave,$user,$pass);
    $sql2 = &quot;show slave status&quot;;
    $result2 = mysql_query($sql2, $conn2);
    while ($row2 = mysql_fetch_array($result2))
    {
      $slavecmd = $row2['Slave_IO_Running'];
      $slavecmd1 = $row2['Slave_SQL_Running'];
    }

      if ($slavecmd=='Yes' &amp;&amp; $slavecmd1=='Yes')
      {
        $slaverunning = &quot;Yes&quot;;
      }
      else
      {
        $slaverunning = &quot;&lt;font color ='red'&gt;No&lt;/font&gt;&quot;;
      }
    $local = &quot;localhost&quot;;
    $lconn = mysql_connect($local,$user,$pass);
    $lsql = &quot;select server_name from myman.server where primary_ip = '&quot;.ereg_replace(&quot;:.*&quot;, &quot;&quot;, $slave).&quot;'&quot;;
    $lresult = mysql_query($lsql, $lconn);
    while($lrow = mysql_fetch_array($lresult))
    {
      if ($depth &gt; 0) print str_repeat('&amp;nbsp;',10 * ($depth - 1));
      echo &quot;&amp;#160---&amp;gt;&amp;#160; &quot;;
      echo $lrow[0]. &quot; (&quot; . $slave . &quot;) Slave Running:&quot; . $slaverunning . &quot;(depth: $depth)&lt;br /&gt;\n&quot;;
      if ($ary) print_server($ary, $depth + 1);
    }
  }
}
?&gt;
</pre>
<p>Example Output:</p>
<pre>Master Server: 172.1.1.102 on Port: 3306
 ---&gt; testdb02 (172.1.1.103) Slave Running:Yes(depth: 0)
 ---&gt; testdb03 (172.1.1.104) Slave Running:Yes(depth: 0)
 ---&gt; testdb04 (172.1.1.105) Slave Running:Yes(depth: 0)
     ---&gt; testdb04-02 (172.1.1.106) Slave Running:Yes(depth: 1)
     ---&gt; devdb01 (172.1.1.107) Slave Running:Yes(depth: 1)
       ---&gt; dc2testdb (172.1.1.108) Slave Running:Yes(depth: 2)
	  ---&gt; dctestdb-02 (172.2.2.101) Slave Running:Yes(depth: 3)
	  ---&gt; dctestdb-03 (172.2.2.102) Slave Running:Yes(depth: 3)
       ---&gt; dc3testdb (172.1.1.108) Slave Running:Yes(depth: 2)
	 ---&gt; dctestdb-02 (172.2.2.103) Slave Running:Yes(depth: 3)
---&gt; devdb02 (172.3.3.101) Slave Running:Yes(depth: 0)
---&gt; devdb03 (172.3.4.101) Slave Running:Yes(depth: 0)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.mysqlhow2.com/2010/02/02/following-your-replication-flow/feed/</wfw:commentRss>
		<slash:comments>0</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! -->