SoFunction
Updated on 2025-03-02

Summary of the script for practical simulation monitoring MySQL service shell

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

Method 1: if condition judgment method

[root@oldboy scripts]# cat check_db01.sh
#!/bin/sh
#created by oldboy
#mail:oldboy521@
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

[root@oldboy scripts]# cat check_db01_1.sh
#!/bin/sh
#created by oldboy
#mail:oldboy521@
 
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

[root@oldboy scripts]# cat check_db01_2.sh
#!/bin/sh
#created by oldboy
#mail:oldboy521@
#load functions
[ -f /etc//functions ] && . /etc//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 database use

[root@oldboy scripts]# cat check_db02.sh
#!/bin/sh
#created by oldboy
#mail:oldboy521@
 
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 satisfied, 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 is check_mysql.sh is not good. Better file names are:
-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 grammar but the result is incorrect, you can use the following method 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 method of sh -x check_db02.sh tracks the script execution process. For more debugging solutions, see the SHELL script debugging details in the following SHELL script debugging.
3) Methods to determine whether the remote MySQL service is normal

Method 1: Port Checking method ==> Suitable for use on remote servers (also suitable for local)

[root@oldboy scripts]# cat check_db03.sh
#!/bin/sh
#created by oldboy
#mail:oldboy521@
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 three methods that are not limited to the following address, ./2561410/942530
For example: you can also use the check_tcp plug-in of nagios.
Method 2: Simulate user (including web server) access method ==> Suitable for use on remote servers (also suitable for local)

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

Skill:
1) Use the web to connect the account to simulate access.
Method 3: Simulate the way users use program URL to judge

<?php
/*
#this scripts is created by oldboy
#oldboy QQ:31333741
#site:
#blog:.
#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 just a simple judgment. Here is a more professional mysql inspection and processing script that integrates judgment, processing, and alarm.
4) More professional solutions for mysql checking and processing scripts

#!/bin/bash 
#created by oldboy QQ 49000448 
#date:20100918 
MYUSER=root 
MYPASS="oldboy" 
MYSOCK=/data/3306/ 
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" 
#→The way to define all variables seems more professional.$MYSQL_CMD -e "select version();" &gt;/dev/null 2&gt;&amp;1 
if [ $? -eq 0 ] 
then 
echo "MySQL is running! " 
exit 0 
else 
$MySQL_STARTUP start &gt;$LOG_FILE#→Logins are also variables.sleep 5; 
$MYSQL_CMD -e "select version();" &gt;/dev/null 2&gt;&amp;1 
if [ $? -ne 0 ] 
then 
for num in `seq 10`#→ Kill mysqld through the for loop. If you really kill, exit the loop or kill every two seconds, a total of 10 times.do 
killall mysqld&gt;/dev/null 2&gt;&amp;1 
[ $? -ne 0 ] &amp;&amp; break; 
sleep 2 
done 
$MySQL_STARTUP start &gt;&gt;$LOG_FILE 
fi 
$MYSQL_CMD -e "select version();" &gt;/dev/null 2&gt;&amp;1 &amp;&amp; Status="restarted" || Status="unknown"#→This logic is more accurate.echo "MySQL status is $Status" &gt;&gt;$LOG_FILE 
mail -s "MySQL status is $Status" 31333741@ &lt; $LOG_FILE 
#→Send the Status above as the result title to the email, of course you can make text messages and voice calls to alarm.fi 
exit