问题描述:
监控MySQL服务是否启动正常,如果未正常启动,就启动MySQL服务。
问题分析:
1)监控MySQL数据库服务的常见方法:
端口监控 | netstat ss lsof telnet nmap nc |
监控服务进程或者进程数 | ps -ef |grep mysql|wc -l ps -f |grep nginx |wc -l |
在客户端模拟用户访问 | wget curl |
登录mysql数据库进行判断 | mysql -uroo -pchang123 -e "select version();" &>/dev/null; echo $? |
2)在本机准备MySQL的测试环境:
yum install -y mariadb mariadb-server
mysqladmin -uroot password "chang123"
说明:设置下mysql的root密码。
1)使用端口监控的方式:
在服务器本地监控端口的命令有netstat、ss和lsof。
[root@abc ~]# netstat -lntup |grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 21270/mysqld
[root@abc ~]# netstat -lntup |grep mysql|wc -l
1
[root@abc ~]# lsof -i:3306 |wc -l
2
[root@abc ~]# ss -lntup |grep mysql | wc -l
1
在远端监控服务器端口的方法有:telnet、nmap和nc。
yum install -y telnet nmap nc
nmap工具的使用:
[root@abc ~]# nmap 127.0.0.1 -p 3306
Starting Nmap 6.40 ( http://nmap.org ) at 2023-08-01 22:09 CST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000086s latency).
PORT STATE SERVICE
3306/tcp open mysql
Nmap done: 1 IP address (1 host up) scanned in 0.24 seconds
[root@abc ~]# nmap 127.0.0.1 -p 3306 |grep open |wc -l
1
telnet工具的使用:
[root@abc ~]# echo -e "\n" | telnet 127.0.0.1 3306 2>/dev/null |grep Connected |wc -l
1
telnet是常用来检测远端服务器端口是否畅通的一个好用的命令,在非交互时需要采用特殊的方法才行,过滤的关键字是Connected,返回1,说明有Connected,表示3306端口是通的。
nc工具:
nc 127.0.0.1 3306 &>/dev/null
对服务进程和进程数进行监控(适合本地服务器)
[root@abc ~]# ps -ef |grep mysql |grep -v grep |wc -l
2
通过wget或者curl命令访问URL地址来测试:
[root@abc ~]# wget --spider --timeout=10 --tries=2 www.baidu.com &>/dev/null
[root@abc ~]# echo $?
0
[root@abc ~]# wget -T 10 -q --spider www.badiu.com >& /dev/null
[root@abc ~]# echo $?
0
[root@abc ~]# curl -s -o /dev/null http://www.baidu.com
[root@abc ~]#
[root@abc ~]# echo $?
0
直接登录mysql数据库进行判断:
[root@abc ~]# mysql -uroot -pchang123 -e "select version();" &>/dev/null
[root@abc ~]# echo $?
0
开发监控MySQL数据库的脚本:
脚本1:
[root@abc ~]# cat mysql_monitor1.sh
#!/bin/bash
#
if [ `netstat -lntup |grep mysqld | wc -l` -gt 0 ]
then
echo "Mariadb is running."
else
echo "Mariadb is stopped."
systemctl start mariadb
fi
[root@abc ~]# sh mysql_monitor1.sh
Mariadb is running.
#停止Mariadb
systemctl start mariadb
[root@abc scripts]# sh mysql_monitor1.sh
Mariadb is stopped.
[root@abc scripts]#
[root@abc scripts]# sh mysql_monitor1.sh
Mariadb is running.
[root@abc scripts]# cat mysql_monitor2.sh
#!/bin/bash
#
if [ `lsof -i tcp:3306 | wc -l` -gt 0 ]
then
echo "Mariadb is running."
else
echo "Mariadb is stopped."
systemctl start mariadb
fi
[root@abc scripts]# sh mysql_monitor2.sh
Mariadb is running.
[root@abc scripts]#
[root@abc scripts]# systemctl stop mariadb
[root@abc scripts]# sh mysql_monitor2.sh
Mariadb is stopped.
[root@abc scripts]# sh mysql_monitor2.sh
Mariadb is running.
另外:如果使用nmap和nc等命令,我们可以在脚本的开头加上这种内容:
[root@abc scripts]# cat mysql_monitor3.sh
#!/bin/bash
#
[ `rpm -qa nmap |wc -l` -lt 1 ] && yum install nmap -y &>/dev/null
if [ `nmap 127.0.0.1 -p 3306 2>/dev/null |grep open | wc -l` -gt 0 ]
then
echo "Mariadb is running."
else
echo "Mariadb is stopped."
systemctl start mariadb
fi
[root@abc scripts]# sh mysql_monitor3.sh
Mariadb is running.
[root@abc scripts]#
[root@abc scripts]# systemctl stop mariadb
[root@abc scripts]#
[root@abc scripts]# sh mysql_monitor3.sh
Mariadb is stopped.
[root@abc scripts]#
[root@abc scripts]# sh mysql_monitor3.sh
Mariadb is running.
另外,为了让监控监本更准确,可以同时在脚本中监控进程数和监控端口两个。
直接在命令行中输入for循环的方法。
[root@abc scripts]# for n in `ls *mysql*`;do echo "cat $n";cat $n;done
cat mysql_monitor1.sh
#!/bin/bash
#
if [ `netstat -lntup |grep mysqld | wc -l` -gt 0 ]
then
echo "Mariadb is running."
else
echo "Mariadb is stopped."
systemctl start mariadb
fi
cat mysql_monitor2.sh
#!/bin/bash
#
if [ `lsof -i tcp:3306 | wc -l` -gt 0 ]
then
echo "Mariadb is running."
else
echo "Mariadb is stopped."
systemctl start mariadb
fi
cat mysql_monitor3.sh
#!/bin/bash
#
[ `rpm -qa nmap |wc -l` -lt 1 ] && yum install nmap -y &>/dev/null
if [ `nmap 127.0.0.1 -p 3306 2>/dev/null |grep open | wc -l` -gt 0 ]
then
echo "Mariadb is running."
else
echo "Mariadb is stopped."
systemctl start mariadb
fi