Go Back   Cloud Computing > Support > MySQL Issues.
 

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 05-18-11, 01:31
Moderator
 
Join Date: Nov 2010
Posts: 476
Default MySql monitoring and automatic restart script

Hi!
If you want to monitor MySQL Server service and will restart automatically then you can follow the script which is given bellow :
Quote:
#!/bin/bash

# MySQL Server root/admin username
MUSER="root"

# MySQL Server admin/root password
MPASS="SET-ROOT-PASSWORD"

# MySQL Server hostname
MHOST="localhost"

#Shell script to start MySQL server i.e. path to MySQL daemon start/stop script.
MSTART="/etc/init.d/mysql start"

# Email ID to send notification
EMAILID="you@your-email.com"

# path to mail program
MAILCMD="$(which mail)"

# path mysqladmin
MADMIN="$(which mysqladmin)"

#### DO NOT CHANGE anything BELOW ####
MAILMESSAGE="/tmp/mysql.fail.$$"

# see if MySQL server is alive or not
# 2&1 could be better but i would like to keep it simple and easy to
# understand stuff
$MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
echo "" >$MAILMESSAGE
echo "Error: MySQL Server is not running/responding ping request">>$MAILMESSAGE
echo "Hostname: $(hostname)" >>$MAILMESSAGE
echo "Date & Time: $(date)" >>$MAILMESSAGE
# try to start mysql
$MSTART>/dev/null
# see if it is started or not
o=$(ps cax | grep -c ' mysqld$')
if [ $o -eq 1 ]; then
sMess="MySQL Server MySQL server successfully restarted"
else
sMess="MySQL server FAILED to restart"
fi
# Email status too
echo "Current Status: $sMess" >>$MAILMESSAGE
echo "" >>$MAILMESSAGE
echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE
# send email
$MAILCMD -s "MySQL server" $EMAILID < $MAILMESSAGE
else # MySQL is running and do nothing
:
fi
# remove file
rm -f $MAILMESSAGE
You just have to do some small modification in this script (mysql password, email ID etc). Then set a cron to run according to your requirement for the above script.
Such as :
Quote:
*/15 * * * * ./(path to script) > /dev/null 2>&1
It will run the script in every 15 mins.
Reply With Quote
  #2 (permalink)  
Old 05-18-11, 02:03
BOD Member
 
Join Date: Apr 2011
Posts: 199
Default

Hi webmaster!
I am not able to monitoring of MySQL Server and httpd with my WHM - do you know if these will send email alerts as well, as your script seems to do?
Reply With Quote
  #3 (permalink)  
Old 05-18-11, 02:22
BOD Member
 
Join Date: Dec 2010
Posts: 142
Default

Hi 66carolin!

No, you will not receive any warn mails as the above mentioned script does.

But, you can also check the "CPU/Memory/MySQL" Usage with the following steps :

WHM >> Server Status >> CPU/Memory/MySQL Usage and apache load from WHM >> Server Status >> Apache Status.
Reply With Quote
  #4 (permalink)  
Old 05-18-11, 03:28
BOD Member
 
Join Date: Apr 2011
Posts: 199
Default

webmaster can you confirm that the script really works?
Reply With Quote
  #5 (permalink)  
Old 05-18-11, 03:49
BOD Member
 
Join Date: Dec 2010
Posts: 170
Default

may I know, will that SQL Server question work on a database which is hosted by trap17,or will I have to edit it in another process?
Reply With Quote
  #6 (permalink)  
Old 05-18-11, 04:10
BOD Member
 
Join Date: Dec 2010
Posts: 227
Default

Hi,

This is a great script and good to monitor the mysql service. But, you require to make two modification in the script in order to work.

o=$(ps cax | grep -c ' mysqld$')
if [ $o -eq 1 ]; then

Edit these two lines to the below.

o=$(ps cax | grep -c ' mysqld')
if [ $o -ge 1 ]; then

It will work as expected.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump


All times are GMT -6. The time now is 00:50.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright © 1999-2012, BODHost Ltd. All rights reserved.