The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Script Needed to Monitor MySQL and restart it when it is Down

Discussion in 'General Discussion' started by faisikhan, Dec 31, 2011.

  1. faisikhan

    faisikhan Well-Known Member

    Joined:
    Dec 12, 2011
    Messages:
    88
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Islamabad, Pakistan
    cPanel Access Level:
    Root Administrator
    Hi Guys :)

    I am in a hunt for a script added to the cron job to restart the MySQL service when it is down. So any ideas or link for it will be appreciated.

    Thank you!
     
  2. Brian

    Brian Well-Known Member

    Joined:
    Dec 1, 2010
    Messages:
    117
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    Texas
    cPanel Access Level:
    Root Administrator
    Such a feature already exists within cPanel * WHM.

    Documentation: Service Manager

    Simply go to: WHM -> Service Manager

    Then, make sure both "Enabled" and "Monitor" are checked for "mysql". This will instruct cPanel's chkservd mechanism (which runs onc per 5 minutes) to check on the status of MySQL and restart as necessary. If a restart is necessary, it will also email you about it if you have your email contact setup in WHM for the server.

    However, if MySQL is crashing/unavailable that often that you're specifically asking for an auto-restart script, I would be more inclined to suggest you identify the cause of the outages. Applying a bandaid fix by auto-restarting MySQL is fine for the short-term, but you'll want to find out why MySQL is crashing in the first place so that you can prevent that from happening.

    What does the MySQL log say during the time of the crash?

    Feel free to open a ticket (see my signature for links) if you'd like assistance in attempting to identify the cause of MySQL crashing on your server.
     
  3. faisikhan

    faisikhan Well-Known Member

    Joined:
    Dec 12, 2011
    Messages:
    88
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Islamabad, Pakistan
    cPanel Access Level:
    Root Administrator
    Hi Brian,

    Thanks for your assistance, but actually I need a script that I can run on the servers where there is no cpanel, I have few servers that are not running any cpanel so I need a written script so that I can monitor the mysql daemon over there with the help of that.

    So any ideas or clues for it?
     
  4. NixTree

    NixTree Well-Known Member

    Joined:
    Aug 19, 2010
    Messages:
    386
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Gods Own Country
    cPanel Access Level:
    Root Administrator
    Hello,

    It is easy to achieve this through a small shell script; something like following perhaps :)

    #!/bin/bash

    #Checking whether MySQL is alive or not

    if mysqladmin ping | grep -q "alive"; then

    echo "MySQL is up"

    else

    /etc/init.d/mysqld restart

    fi

    Please note that above script will check whether MySQL is up or not. If you need to monitor anything else ( like total queries, ect ), you can enhance the above script :)

    Additionall you can run it a deamon to check MySQL every second ( if you want ). For that you just need to put the above script in an infinite loop and give one second intervel ( sleep 1 ) for the above check. Please note, it may create an extra overhead ( but not much ) for checking mysql every second.

    Thank you,
    Nibin.
     
  5. cPanelTristan

    cPanelTristan Quality Assurance Analyst
    Staff Member

    Joined:
    Oct 2, 2010
    Messages:
    7,623
    Likes Received:
    21
    Trophy Points:
    38
    Location:
    somewhere over the rainbow
    cPanel Access Level:
    Root Administrator
    If this is a non-cPanel machine, you might consider using SIM at System Integrity Monitor | R-fx Networks location. This is the same company that provides APF firewall and PRM for resource monitoring. SIM will check services to see if they are online and restart those that are not.
     
  6. NixTree

    NixTree Well-Known Member

    Joined:
    Aug 19, 2010
    Messages:
    386
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Gods Own Country
    cPanel Access Level:
    Root Administrator
    Great link Tristan....; thanks for that :)
     
  7. faisikhan

    faisikhan Well-Known Member

    Joined:
    Dec 12, 2011
    Messages:
    88
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Islamabad, Pakistan
    cPanel Access Level:
    Root Administrator
    Hi Guys,

    Thanks a lot for your help and definitely the above link is perfect :)
     
Loading...

Share This Page