{"id":200,"date":"2009-11-11T15:21:25","date_gmt":"2009-11-11T15:21:25","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=200"},"modified":"2014-06-24T18:58:57","modified_gmt":"2014-06-24T18:58:57","slug":"monitor-mysql-replication-using-php","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/monitor-mysql-replication-using-php","title":{"rendered":"Monitor multiple mysql replication using php: Updated"},"content":{"rendered":"<p>Monitoring a replication is an important aspect. As replication includes multiple nodes, it is essential to track activity and status across all mysql servers involved in replication.<\/p>\n<p>To monitor replication we know commands like:<\/p>\n<p><span style=\"color: #999999;\">Show slave status;<\/span><\/p>\n<p><span style=\"color: #999999;\">Show master status;<\/span><\/p>\n<p>Refer: http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/sql-syntax-replication.html<\/p>\n<p>But when it comes to non-gui interface, it becomes little tedious.<\/p>\n<p>With some efforts, I managed to make a php script to monitor mysql servers.<\/p>\n<p>It can:<\/p>\n<ul>\n<li>Get per second status of slaves.<\/li>\n<li>Start \/ Stop slave from any of the mysql servers in replication.<\/li>\n<li>Display error number along with description in case of error in replication.<\/li>\n<li>Shows detailed information of both the server\u2019s global status variables.<\/li>\n<\/ul>\n<p>Well there is no fancy UI, my bad, but it works for me. I&#8217;ll try to make it better, may be you can suggest.<\/p>\n<p><strong>Code: <\/strong><\/p>\n<p>[ Download here: <a href=\"http:\/\/kedar.nitty-witty.com\/wp-content\/uploads\/2009\/11\/monitor-replication.txt\" target=\"_blank\">monitor-replication.txt<\/a>] ( ** Check Updated Monitoring Script. )<\/p>\n<p><span style=\"color: #999999;\">&lt;?php<\/span><\/p>\n<p><span style=\"color: #999999;\"> #Resets warning messages<\/span><\/p>\n<p><span style=\"color: #999999;\">error_reporting(E_ALL ^ E_NOTICE);<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">#Function to display slave status.<\/span><\/p>\n<p><span style=\"color: #999999;\">function slavestatus($host,$username,$password,$slaveport=3306)<\/span><\/p>\n<p><span style=\"color: #999999;\">{<\/span><\/p>\n<p><span style=\"color: #999999;\">$con=mysql_connect(&#8220;$host:$slaveport&#8221;,$username,$password)<\/span><\/p>\n<p><span style=\"color: #999999;\">or die(&#8220;can&#8217;t connect server&#8221;);<\/span><\/p>\n<p><span style=\"color: #999999;\">$result=mysql_query(&#8220;show slave status&#8221;);<\/span><\/p>\n<p><span style=\"color: #999999;\">$timeResult=mysql_query(&#8220;select now()&#8221;);<\/span><\/p>\n<p><span style=\"color: #999999;\">$time=mysql_result($timeResult,0,0);<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">while($status = mysql_fetch_array($result))<\/span><\/p>\n<p><span style=\"color: #999999;\">{<\/span><\/p>\n<p><span style=\"color: #999999;\">$file=$status[5];<\/span><\/p>\n<p><span style=\"color: #999999;\">$position=$status[6];<\/span><\/p>\n<p><span style=\"color: #999999;\">$sql_run=$status[10];<\/span><\/p>\n<p><span style=\"color: #999999;\">$io_run=$status[11];<\/span><\/p>\n<p><span style=\"color: #999999;\">$errorNum=$status[18];<\/span><\/p>\n<p><span style=\"color: #999999;\">$errorMeg=$status[19];<\/span><\/p>\n<p><span style=\"color: #999999;\">}<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">print &#8220;&lt;tr align=center&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;td&gt; $time &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;td&gt; $host : $slaveport &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;td&gt; $file &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;td&gt; $position &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;td&gt; $sql_run &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;td&gt; $io_run &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;td&gt; $errorNum &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;td&gt; $errorMeg &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;td&gt; &lt;form name=&#8217;form&#8217; action=&#8217;#&#8217; method=&#8217;POST&#8217;&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;input type=&#8217;submit&#8217; name=&#8217;stop&#8217;\u00a0 id=&#8217;stop&#8217;\u00a0 value=\\&#8221;STOP $host:$slaveport\\&#8221;&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;input type=&#8217;submit&#8217; name=&#8217;start&#8217;\u00a0 id=&#8217;start&#8217;\u00a0 value=\\&#8221;START $host:$slaveport\\&#8221;&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;\/form&gt; &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;td&gt; &lt;a href=&#8217;?detail=$host:$slaveport&#8217;&gt; detailed &lt;\/a&gt;&lt;\/td&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;\/tr&gt;&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">}<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">#Function for starting and stopping mysql server.<\/span><\/p>\n<p><span style=\"color: #999999;\">function start_stop($machine,$username,$password,$task=&#8217;no&#8217;)<\/span><\/p>\n<p><span style=\"color: #999999;\">{<\/span><\/p>\n<p><span style=\"color: #999999;\">if($task==&#8217;no&#8217;) exit;<\/span><\/p>\n<p><span style=\"color: #999999;\">$con=mysql_connect($machine,$username,$password) or die(&#8220;can&#8217;t connect server&#8221;);<\/span><\/p>\n<p><span style=\"color: #999999;\">$sql= $task . &#8221; slave&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">$result=mysql_query($sql);<\/span><\/p>\n<p><span style=\"color: #999999;\">$_POST = array();<\/span><\/p>\n<p><span style=\"color: #999999;\">$task=&#8221;no&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">$VAR=$_SERVER[&#8220;REQUEST_URI&#8221;];<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">#Refreshes page, updates server status in table.<\/span><\/p>\n<p><span style=\"color: #999999;\">header(&#8216;refresh: 1; url=&#8217;.$VAR);<\/span><\/p>\n<p><span style=\"color: #999999;\">}<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">#Function for showing detailed status<\/span><\/p>\n<p><span style=\"color: #999999;\">function get_status($machine,$username,$password)<\/span><\/p>\n<p><span style=\"color: #999999;\">{<\/span><\/p>\n<p><span style=\"color: #999999;\">$hostname = $_SERVER[&#8216;SCRIPT_NAME&#8217;];<\/span><\/p>\n<p><span style=\"color: #999999;\">echo &#8220;&lt;center&gt;&lt;a href=\\&#8221;$hostname\\&#8221;&gt;Reset&lt;\/a&gt;&lt;\/center&gt;&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">if($task==&#8217;no&#8217;) exit;<\/span><\/p>\n<p><span style=\"color: #999999;\">$con=mysql_connect($machine,$username,$password) or die(&#8220;can&#8217;t connect server&#8221;);<\/span><\/p>\n<p><span style=\"color: #999999;\">$sql=&#8221;show global status&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">echo &#8220;&lt;table border=1 align=center&gt;&lt;tr&gt;&lt;th&gt;Variable&lt;\/th&gt;&lt;th&gt;Value&lt;\/th&gt;&lt;\/tr&gt;&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">$res=mysql_query($sql);<\/span><\/p>\n<p><span style=\"color: #999999;\">while($row = mysql_fetch_assoc($res))<\/span><\/p>\n<p><span style=\"color: #999999;\">{<\/span><\/p>\n<p><span style=\"color: #999999;\">echo &#8216;&lt;tr align=center&gt;&lt;td&gt;&#8217;.$row[&#8216;Variable_name&#8217;].<\/span><\/p>\n<p><span style=\"color: #999999;\">&#8216;&lt;\/td&gt;&lt;td&gt;&#8217;.$row[&#8216;Value&#8217;].'&lt;\/td&gt;&lt;\/tr&gt;&#8217;;<\/span><\/p>\n<p><span style=\"color: #999999;\">}<\/span><\/p>\n<p><span style=\"color: #999999;\">echo &#8220;&lt;\/table&gt;&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">#$_POST = array();<\/span><\/p>\n<p><span style=\"color: #999999;\">$task=&#8221;no&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">}<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">echo &#8216;&lt;html&gt;&#8217;;<\/span><\/p>\n<p><span style=\"color: #999999;\">if(!isset($_GET[&#8216;detail&#8217;]))<\/span><\/p>\n<p><span style=\"color: #999999;\">{<\/span><\/p>\n<p><span style=\"color: #999999;\">#Auto refreshing page for continuous status<\/span><\/p>\n<p><span style=\"color: #999999;\">#content=&#8221;3;&#8221; page refreshes after 3 seconds<\/span><\/p>\n<p><span style=\"color: #999999;\">echo &#8216;&lt;meta http-equiv=&#8221;refresh&#8221; content=&#8221;3;&#8221;&gt;&#8217;;<\/span><\/p>\n<p><span style=\"color: #999999;\">}<\/span><\/p>\n<p><span style=\"color: #999999;\">echo'&lt;body&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;h1&gt;&lt;font face=&#8221;verdana&#8221; size=10&gt;&lt;small&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">MySQL Replication Monitor&lt;\/small&gt;&lt;\/font&gt;&lt;\/h1&gt;&#8217;;<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">#host-name &amp; ports for replication servers<\/span><\/p>\n<p><span style=\"color: #999999;\">$slave = &#8220;localhost&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">$master = &#8220;localhost&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">$slaveport=3306;<\/span><\/p>\n<p><span style=\"color: #999999;\">$masterport=3307;<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">#considered a common username password for accessing both servers<\/span><\/p>\n<p><span style=\"color: #999999;\">$username=&#8221;root&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">$password=&#8221;kedar&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">print &#8220;&lt;table border=1&gt;&lt;tr&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;th&gt; time &lt;\/th&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;th&gt; host : port &lt;\/th&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;th&gt; file &lt;\/th&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;th&gt; position &lt;\/th&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;th&gt; io run &lt;\/th&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;th&gt; sql run &lt;\/th&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;th&gt; errorNum &lt;\/th&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;th&gt; errorMeg &lt;\/th&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;th&gt; Stop \/ Start &lt;\/th&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;th&gt; Extra &lt;\/th&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;\/tr&gt;&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">slavestatus($slave,$username,$password,$slaveport);<\/span><\/p>\n<p><span style=\"color: #999999;\">slavestatus($master,$username,$password,$masterport);<\/span><\/p>\n<p><span style=\"color: #999999;\">print &#8220;&lt;\/table&gt;&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">if (isset($_POST[&#8216;start&#8217;])) {<\/span><\/p>\n<p><span style=\"color: #999999;\">list($task,$machine)=split(&#8221; &#8220;,$_POST[&#8216;start&#8217;]);<\/span><\/p>\n<p><span style=\"color: #999999;\">start_stop($machine,$username,$password,$task);<\/span><\/p>\n<p><span style=\"color: #999999;\">}<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">if (isset($_POST[&#8216;stop&#8217;])) {<\/span><\/p>\n<p><span style=\"color: #999999;\">list($task,$machine)=split(&#8221; &#8220;,$_POST[&#8216;stop&#8217;]);<\/span><\/p>\n<p><span style=\"color: #999999;\">start_stop($machine,$username,$password,$task);<\/span><\/p>\n<p><span style=\"color: #999999;\">}<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">if (isset($_GET[&#8216;detail&#8217;])) {<\/span><\/p>\n<p><span style=\"color: #999999;\">echo &#8220;&lt;br&gt;&lt;center&gt;&lt;b&gt;Showing global status for: &#8220;.<\/span><\/p>\n<p><span style=\"color: #999999;\">$_GET[&#8216;detail&#8217;] . &#8220;&lt;\/b&gt;&lt;\/center&gt;&lt;br&gt;&#8221;;<\/span><\/p>\n<p><span style=\"color: #999999;\">get_status($_GET[&#8216;detail&#8217;],$username,$password);<\/span><\/p>\n<p><span style=\"color: #999999;\">}<\/span><\/p>\n<p><span style=\"color: #999999;\"> <\/span><\/p>\n<p><span style=\"color: #999999;\">echo &#8216;&lt;div align=right&gt;&lt;font face=&#8221;verdana&#8221; color=&#8221;gray&#8221; size=&#8221;2px&#8221;&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">Kedar.&lt;\/div&gt;&lt;\/div&gt;&lt;\/body&gt;<\/span><\/p>\n<p><span style=\"color: #999999;\">&lt;\/html&gt;&#8217;;<\/span><\/p>\n<p><span style=\"color: #999999;\">?&gt;<\/span><\/p>\n<p><strong><big>Update: <\/big> Above script with little changes for monitoring more mysql servers using php script with easiest possible configuration.<\/strong><br \/>\n<strong>Download: <\/strong> <a href=\"http:\/\/kedar.nitty-witty.com\/wp-content\/uploads\/2009\/11\/MySQL-replication-monitoring-PHP-Script.zip\">MySQL-replication-monitoring-PHP-Script<\/a>.\u00a0 ( ** Check Updated Monitoring Script. )<\/p>\n<p>The script give reads server configurations from a config file &#8216;replmon_conf.txt&#8217; and thus facilitate to monitor simultaneously any number of mysql database servers.<\/p>\n<p>You have to specify connection parameters in replmon_conf.txt as follows:<br \/>\nhostname,username,password,port<\/p>\n<p>To monitor more than one mysql database servers you just need to add one more line of connection parameters.<br \/>\nHope this helps.<\/p>\n<p>Update 2:\u00a0 Tookout some time and modified the script for monitoring mysql replication using php. <span style=\"color: #00ff00;\"> <\/span><\/p>\n<h2><span style=\"color: #00ff00;\">Download mysql monitor using php: <a href=\"http:\/\/kedar.nitty-witty.com\/wp-content\/uploads\/2009\/11\/mysql-replication-monitor-php.zip\">mysql replication monitor php<\/a><\/span><\/h2>\n","protected":false},"excerpt":{"rendered":"Monitoring a replication is an important aspect. As replication includes multiple nodes, it is essential to track activity and status across all mysql servers involved in replication. To monitor replication&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,378],"tags":[70,264,427,263,85,100,265],"class_list":{"0":"post-200","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-mysql-scripts-mysql","8":"tag-monitor","9":"tag-monitor-replication","10":"tag-mysql","11":"tag-mysql-replication-monitor","12":"tag-php","13":"tag-replication","14":"tag-replication-monitor-in-php"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=200"}],"version-history":[{"count":8,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":1973,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/200\/revisions\/1973"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}