<?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>[LINICKX].com &#187; mysql</title>
	<atom:link href="http://www.linickx.com/tag/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.linickx.com</link>
	<description>Moments of Genius followed by Trash.</description>
	<lastBuildDate>Thu, 17 May 2012 10:23:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Service Recovery Scripts &amp; Error Page Tips.</title>
		<link>http://www.linickx.com/267/service-recovery-scripts-error-page-tips</link>
		<comments>http://www.linickx.com/267/service-recovery-scripts-error-page-tips#comments</comments>
		<pubDate>Tue, 06 Mar 2007 13:32:55 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Enterprise Linux]]></category>
		<category><![CDATA[Fedora]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[httpd]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[recovery]]></category>

		<guid isPermaLink="false">http://www.linickx.com/blog/archives/267/service-recovery-scripts-error-page-tips/</guid>
		<description><![CDATA[A couple of weeks ago, I was proper ill with flu; the problem with looking after your own server is that only you can fix it &#8211; it&#8217;s well and good having monitoring systems (nagios) telling you about faults, but &#8230; <a href="http://www.linickx.com/267/service-recovery-scripts-error-page-tips">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago, I was proper ill with flu; the problem with looking after your own server is that only you can fix it &#8211; it&#8217;s well and good having monitoring systems (<em>nagios</em>) telling you about faults, but if you can&#8217;t read or see the alerts the fault won&#8217;t get resolved.</p>
<p>During this time I was ill, for an unknown reason the mySQL process on my server died, as such my website (<em>and others I look after</em>) were down for 8 hours. The fix was simple, one command, restart the service and normal service was resumed (<em>excuse the pun</em>).</p>
<p>This led to me to the conclusion that there must be a way to get the server to fix it&#8217;s self. after all, why do a job when you can get a computer to do it for you !  Fortunately I had a <abbr title="short moment of genuis">light bulb moment</abbr> and realised that I could use the init scripts that are provided by redhat, the below code will restart apache (<em>httpd</em>) and mySQL on a redhat based system in the event that the service was not stopped cleanly. (<em>In-fact this config has only be tested on CentOS, your mileage may vary on anything else</em>)</p>
<pre>
#!/bin/bash

# taken from redhast default scripts - /etc/rc.d/init.d/functions

# Set up a default search path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
export PATH

status() {
        local base=${1##*/}
        local pid

        # Test syntax.
        if [ "$#" = 0 ] ; then
                echo $"Usage: status {program}"
                return 1
        fi

        # First try "pidof"
        pid=`pidof -o $$ -o $PPID -o %PPID -x $1 ||
             pidof -o $$ -o $PPID -o %PPID -x ${base}`
        if [ -n "$pid" ]; then
# Uncomment this if you want OK messages
#               echo $"${base} (pid $pid) is running..."
                return 0
        fi

        # Next try "/var/run/*.pid" files
        if [ -f /var/run/${base}.pid ] ; then
                read pid &lt; /var/run/${base}.pid
                if [ -n "$pid" ]; then
                        echo $"${base} dead but pid file exists"
                        /etc/init.d/${base} restart
                        return 1
                fi
        fi
        # See if /var/lock/subsys/${base} exists
        if [ -f /var/lock/subsys/${base} ]; then
                echo $"${base} dead but subsys locked"
                /etc/init.d/${base} restart
                return 2
        fi
        echo $"${base} is stopped"
        return 3
}

# found in /etc/init.d/httpd
httpd=${HTTPD-/usr/sbin/httpd}

status mysqld
status $httpd</pre>
<p>If you save this, as <strong>/etc/cron.hourly/auto_recovery.sh</strong> , then do <strong>chmod +x /etc/cron.hourly/auto_recovery.sh</strong> , assuming you&#8217;ve not changed the default cron setup, every hour mySQL &amp; httpd will be checked, if they have died the&#8217;ll be restarted and root will get an e-mail about what happened.</p>
<p>Cool eh !</p>
<p><strong>A final finishing touch:</strong> I wanted to change the default &#8220;Database Down&#8221; error messages on my two most popular applications.</p>
<ul>
<li> <a href="http://allforces.com/2006/06/18/custom-wordpress-errors/">Melvin Rivera has written a tutorial on how to customize the wordpress error page</a>, note that it involves editing a file outside of wp-content, that means you&#8217;ll have to re-do this &#8220;hack&#8221; every time you upgrade wordpress.</li>
<li> PHPBB: Setting a custom error page on that is really easy, first create a php page displaying your message. Then at the bottom of /path/to/phpbb-install/includes/db.php you&#8217;ll see
<pre>
// Make the database connection.
$db = new sql_db($dbhost, $dbuser, $dbpasswd, $dbname, false);
if(!$db-&gt;db_connect_id)
{
message_die(CRITICAL_ERROR, "Could not connect to the database");
}</pre>
<p>change it to</p>
<pre>
 // Make the database connection.
$db = new sql_db($dbhost, $dbuser, $dbpasswd, $dbname, false);
if(!$db-&gt;db_connect_id)
{
 include("/path/to/my-custom-error-page.php");
        die();
}</pre>
</li>
</ul>
<p>Now if you database dies, for the time it&#8217;s down (<em>before cron fixes it</em>) wordpress &amp; phpbb sites would get a much prettier error message. Obviously there&#8217;s no solution for apache as there&#8217;s nothing to serve the pages, but hopefully this kind of thing doesn&#8217;t happen to often <img src='http://www.linickx.com/wp/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<img src="http://www.linickx.com/wp/wp-content/themes/linickx_v2/images/nick_sig_bggrey.png" alt="Nick" /> <hr/>Copyright &copy; 2012 <strong><a href="http://www.linickx.com">[LINICKX].com</a></strong>. This Feed is for personal non-commercial use only. Please check my <a href="http://www.linickx.com/?page_id=63">Site Terms and Conditions</a> for full details on copyrights. If you have any concerns with the content of this feed you may <a href="http://www.linickx.com/contact">contact me here</a>.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">WP Copyright Plugin</a></span>]]></content:encoded>
			<wfw:commentRss>http://www.linickx.com/267/service-recovery-scripts-error-page-tips/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

