Monitors the script of the MySQL process, restarts if the fault occurs, and sends an Email notification if the MySQL process cannot be started.

Source: Internet
Author: User

I have contributed a script to monitor the MYSQL process. I hope it will be helpful to you.

Function:

Monitors the MYSQL process and restarts upon failure. If the MYSQL process fails to be started, an Email notification is sent.

The Code is as follows:

  1. #! /Bin/bash
  2. #/Usr/bin/nmap localhost | grep3306
  3. # Lsof-I:3306
  4. MYSQLPORT = 'netstat-na | grep"LISTEN"| Grep"3306"| Awk-F [:""] +'{Print $5 }''
  5. Function checkMysqlStatus (){
  6. /Usr/bin/mysql-uroot-p11111 -- connect_timeout =5-E"Show databases ;"&>/Dev/Null 2> &1
  7. If[$? -Ne0]
  8. Then
  9. RestartMysqlService
  10. If["$ MYSQLPORT"="3306"]; Then
  11. Echo"Mysql restart successful ......"
  12. Else
  13. Echo"Mysql restart failure ......"
  14. Echo"Server: $ MYSQLIP mysql is down, please try to restart mysql by manual! ">/Var/log/mysqlerr
  15. # Mail-s"WARN! Server: $ MYSQLIP mysql is down"Admin@ Yourdomain. Com </var/log/mysqlerr
  16. Fi
  17. Else
  18. Echo"Mysql is running ..."
  19. Fi
  20. }
  21. Function restartMysqlService (){
  22. Echo"Try to restart the mysql service ......"
  23. /Bin/ps aux | grep mysql | grep-v grep | awk'{Print $2 }'| Xargs kill-9
  24. Service mysql start
  25. }
  26. If["$ MYSQLPORT"="3306"]
  27. Then
  28. CheckMysqlStatus
  29. Else
  30. RestartMysqlService
  31. Fi

We recommend that you run the task every 10 minutes.
*/10 * root/bin/sh/root/mysql_status.sh

Principle:

1) Check whether port 3306 of MYSQL is normal;

2) use the account to connect to the database and execute the show databases command;

3) if the above two points work properly, the database is running normally.

In addition, you can monitor the MYAQL port in three ways:

1)/usr/bin/nmap localhost | grep 3306

2) lsof-I: 3306

3) netstat-na | grep "LISTEN" | grep "3306" | awk-F [: ""] + '{print $5 }'

The third type is used here. Note: The operating systems '{print $5}' may be different. Please test them on your own. If you use the first method, you need to install nmap.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.