Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement monitoring MySQL service shell script

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to implement shell scripts for monitoring MySQL services. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1) Port judgment method = > is only suitable for local use of the database

Method of 1:if condition judgment

[root@oldboy scripts] # cat checkpoint db01. Then echo mysqld is running. "else echo" mysqld is stoped. "fi." wc-l`if [$PortNum-eq 1] then echo "mysqld is running."

Method 2: conditional expression usage

[root@oldboy scripts] # cat checkpoint db01mm 1.shroke engine by oldboy#mail:oldboy521@gmail.com PortNum= `netstat-lnt | grep 3306 | wc-l` # PortNum= `nmap 10.0.0.189-p 3306 | grep open | wc-l` [$PortNum-eq 1] & & echo "mysqld is running." | | echo "mysqld is stoped."

Method 3: using the system function display method

[root@oldboy scripts] # cat checkpoints db01. 2. Shroud cards are bound to by oldboy#mail:oldboy521@gmail.com#load functions [- f / etc/init.d/functions] & &. / etc/init.d/functionsPortNum= `netstat-lnt | grep 3306 | wc-l`if [$PortNum-eq 1] then action "mysqld is running." / bin/trueelse action "mysqld is stoped." / bin/falsefi

2) the process plus port judgment method is only suitable for local use of the database.

[root@oldboy scripts] # cat checkpoint db02.shroke by oldboy#mail:oldboy521@gmail.com PortNum= `netstat-lnt | grep 3306 | wc-l`ProcessNum = `ps-ef | grep mysql | grep-v grep | wc-l` # echo $PortNum $ProcessNum#exit 1 if [$PortNum-eq 1-a $ProcessNum-eq 2] then echo "mysqld is running."else echo" mysqld is stoped. "fi

Tip: if you are not assured, you can also add simulated user access at the same time.

Tips: script debugging skills

1) the string filtered by grep should not be included in the file name, for example: grep mysql, the file name check_mysql.sh is not good. Better file names such as:

-rw-r--r-- 1 root root 236 Sep 9 22:19 check_db01.sh

-rw-r--r-- 1 root root 293 Sep 9 22:34 check_db02.sh

-rw-r--r-- 1 root root 212 Sep 9 22:49 check_db03.sh

2) when there is no problem with the syntax but the result is incorrect, you can use the following methods to troubleshoot:

A) print the variable result after the variable definition to see if it is consistent with the actual result, such as:

Echo $PortNum $ProcessNum

Exit 1

B) the way sh-x check_db02.sh tracks the execution of the script. For more debugging solutions, see the SHELL script debugging detail below.

3) the method of judging whether the remote mysql service is normal or not

Method 1: Port checking method = > suitable for use on remote servers (also suitable locally)

[root@oldboy scripts] # cat checkpoint db03.sh mysqld is running mysqld is stoped. "grep open | wc-l`if [$PortNum-eq 1] then echo" mysqld is running. "else echo" mysqld is stoped. "fi.

Tip: there are many ways to use ports, including three methods that are not limited to the following address, http://oldboy.blog.51cto.com/2561410/942530

For example, you can also use nagios's check_tcp plug-in and so on.

Method 2: simulated user (including web server) access method = > suitable for use on remote servers (also suitable locally)

[root@oldboy scripts] # cat checkpoint db04. Then echo then echo "mysqld is running." else echo "mysqld is stoped." fi.

Tips:

1) use web to connect to the account to simulate access.

Method 3: simulate the way the user uses the program URL to judge

The above method is only a simple judgment, then the following is a set of judgment, processing, and alarm in a more professional mysql check processing script.

4) the solution of more professional mysql check and processing script

#! / bin/bash # created by oldboy QQ 49000448 # date:20100918 MYUSER=root MYPASS= "oldboy" MYSOCK=/data/3306/mysql.sock MySQL_STARTUP= "/ data/3306/mysql" LOG_PATH=/tmp LOG_FILE=$ {LOG_PATH} / mysqllogs_ `date +% F `. Log MYSQL_PATH=/usr/local/mysql/bin MYSQL_CMD= "$MYSQL_PATH/mysql-u$MYUSER-p$MYPASS-S $MYSOCK" # → full variable definition is more professional. $MYSQL_CMD-e "select version ();" > / dev/null 2 > & 1 if [$?-eq 0] then echo "MySQL is running!" Exit 0 else $MySQL_STARTUP start > $LOG_FILE# → log is also a variable. Sleep 5; $MYSQL_CMD-e "select version ();" > / dev/null 2 > & 1 if [$?-ne 0] then for num in `seq 10` # → kills mysqld through the for loop, and exits the loop or kills every two seconds for a total of 10 times. Do killall mysqld > / dev/null 2 > & 1 [$?-ne 0] & & break; sleep 2 done $MySQL_STARTUP start > > $LOG_FILE fi $MYSQL_CMD-e "select version ();" > / dev/null 2 > & 1 & & Status= "restarted" | | Status= "unknown" # → is more accurate. Echo "MySQL status is $Status" > > $LOG_FILE mail-s "MySQL status is $Status" 31333741@qq.com < $LOG_FILE # → send the above Status as the result title to the email, of course, you can do SMS, voice call alarm. Fi exit, thank you for your reading! This is the end of this article on "how to monitor MySQL service shell scripts". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report