Shell script: Monitor MySQL service is normal

Source: Internet
Author: User

Monitoring the MySQL service is normal, the general idea is : check whether the 3306 port is started, PS to see if the mysqld process starts, command line login MySQL execution statement return results, PHP or JSP program detection (Need developer development Program) and so on;


Method 1: Listen for 3306 ports

#!/bin/bash#written by [email protected]port= ' netstat-nlt|grep 3306|wc-l ' if [$port-ne 1]then/etc/init.d/mysqld start else echo "MySQL is running" fi


Method 2: View the MYSQLD process

Note: If you use process filtering, script name if it contains MySQL, script execution has a pit, remember!!! Because the script will also be grep once, resulting in inaccurate results;

[email protected] baby]# cat Check_mysql.sh#!/bin/bash#written by [email protected]process= ' Ps-ef |grep mysql|grep-v gr EP |wc-l ' If [$process-ne 2]then/etc/init.d/mysqld startelse echo "MySQL is running" fi

The results of the implementation are as follows:

[Email protected] baby]# sh check_mysql.sh

Starting MySQL success!

Renaming is performed after the normal result:

[Email protected] baby]# mv check_mysql.sh check_db.sh

[Email protected] baby]# sh check_db.sh

MySQL is running


Method 3: Double insurance, process and port success count MySQL service OK

#!/bin/bash#written by [email protected]port= ' netstat-nlt|grep 3306|wc-l ' process= ' ps-ef |grep mysql|grep-v grep |wc-l ' If [$port-eq 1] && [$process-eq 2]then echo "MySQL is running" Else/etc/init.d/mysqld Startfi


4: Use the client to log in to MySQL to execute the command to see if the test service is started, theoretically this method is the most reliable.

[email protected] baby]# cat Check_db_client.sh#!/bin/bash#written by [email protected]mysql-uroot-p123456-e "Select V Ersion (); "&>/dev/nullif [$?-ne 0]then/etc/init.d/mysqld startelse echo" MySQL is running "fi

The results of the implementation are as follows:

[Email protected] baby]# sh check_db_client.sh

MySQL is running



This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://mofansheng.blog.51cto.com/8792265/1703285

Shell script: Monitor MySQL service is normal

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.