Summary of MySQL service shell script for actual combat simulation monitoring

Time:2021-7-20

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

Method 1: if condition judgment method


[[email protected] scripts]# cat check_db01.sh
#!/bin/sh
#created by oldboy
#mail:[email protected]
PortNum=`netstat -lnt|grep 3306|wc -l`
if [ $PortNum -eq 1 ]
then
 echo "mysqld is running."
else
 echo "mysqld is stoped."
fi

Method 2: conditional expression usage


[[email protected] scripts]# cat check_db01_1.sh
#!/bin/sh
#created by oldboy
#mail:[email protected]
 
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: use system function display method


[[email protected] scripts]# cat check_db01_2.sh
#!/bin/sh
#created by oldboy
#mail:[email protected]
#load functions
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
PortNum=`netstat -lnt|grep 3306|wc -l`
if [ $PortNum -eq 1 ]
then
 action "mysqld is running." /bin/true
else
 action "mysqld is stoped." /bin/false
fi

2) Process plus port judgment method = = > only suitable for local use of database


[[email protected] scripts]# cat check_db02.sh
#!/bin/sh
#created by oldboy
#mail:[email protected]
 
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 at ease, you can also add simulated user access at the same time.

Skills: script debugging skills

1) Grep filter string, do not include in the file name, for example: grep mysql, the file name is check_ Mysql.sh is not very good. Better file names are as follows:
-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 syntax problem but the result is incorrect, you can use the following method to check:
   a) Print the variable result after the variable definition to see if it is consistent with the actual result
     echo $PortNum $ProcessNum
     exit 1
   b) sh -x check_ Db02.sh to track the script execution process. For more debugging solutions, see the shell script debugging details later.
3) The method of judging whether the remote MySQL service is normal or not

Method 1: port check method = = > suitable for remote server (local as well)


[[email protected] scripts]# cat check_db03.sh
#!/bin/sh
#created by oldboy
#mail:[email protected]
PortNum=`nmap 10.0.0.189 -p 80|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 the port, including but not limited to the following three methods, http://oldboy.blog.51cto.com/2561410/942530
For example, you can also use Nagios check_ TCP plug-in and so on.
Method 2: access method of simulated user (including web server) = = > suitable for remote server (local as well)


[[email protected] scripts]# cat check_db04.sh
#!/bin/sh
#created by oldboy
#mail:[email protected]
 
mysql -S /data/3306/mysql.sock -e "select version();" >/dev/null 2>&1
if [ $? -eq 0 ]
then
 echo "mysqld is running."
else
 echo "mysqld is stoped."
fi

skill:
1) Use web connection account to simulate access.
Method 3: simulate the way users use the program URL


<?php
/*
#this scripts is created by oldboy
#oldboy QQ:31333741
#site:http://www.etiantian.org
#blog:http://oldboy.blog.51cto.com
#oldboy trainning QQ group: 208160987 226199307 44246017
*/
  $link_id=mysql_connect('10.0.0.4','oldboy','oldboy123') or mysql_error();
  if($link_id){
   echo "mysql successful by oldboy !";
  }else{
   echo mysql_error();
  }
?>

The above method is only a simple judgment, so the following is a set of judgment, processing, and alarm in a more professional MySQL check processing script.
4) The solution of professional MySQL checking 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" 
#It is more professional to define all variables. 
$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' # → kill mysqld through the for loop. If you really kill mysqld, exit the loop or kill every two seconds, 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();" >/ This logic is more accurate. 
echo "MySQL status is $Status" >>$LOG_FILE 
mail -s "MySQL status is $Status" [email protected] < $LOG_FILE 
#Send the status above as the result title to the email. Of course, you can make SMS and voice call alarm. 
fi 
exit

Recommended Today

Web development using spring boot + mybatis + FreeMarker + ehcache

Original text: https://tlanyan.pp.ua/spring-boot-mybatis-freemarker-ehcache-web-development/ Previous Java projects were taken over from others, and they had not developed independently. I’ve taken time to study spring boot for web development these days. This article is used as a record and reference. preparation Install intelij idea; Intelij idea is basically a necessary tool for Java and Android development, and […]