標籤:mysql 監控 服務
問題描述:監控MYSQL服務是否正常啟動,如果未正常啟動,就啟動MYSQL
判斷mysql的方法:
1)連接埠判斷
[[email protected] ~]# netstat -lntup | grep 3306tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2288/mysqld
2)進程判斷
[[email protected] ~]# ps -ef | grep mysqld mysql 2071 1 0 11:32 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usrmysql 2288 2071 0 11:32 ? 00:00:24 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sockroot 10097 6962 0 17:27 pts/1 00:00:00 grep --color=auto mysqld
3)傳回值判斷
[[email protected] ~]# mysql -uroot -proot -e "select version();" &>/dev/null [[email protected] ~]# echo $?0
法一:實現思路是過濾出MYSQL連接埠3306進行判斷:
#!/bin/bashport=`netstat -lnt|grep 3306|wc-l`if [ $port -ne 1 ]then /etc/init.d/mysql startelse echo "MySQL is running."fi執行結果:[[email protected] ~]# sh mysql1.sh MySQL is running.
法二:實現思路是通過MYSQL進程進行判斷:
#!/bin/bashportcess=`ps -ef|grep mysql|grep -v grep|wc -l`if [ $portcess -ne 2 ]then /etc/init.d/mysqld startelse echo "MySQL is running."fi執行結果:[[email protected] ~]# sh mysql1.sh MySQL is running.注意:過濾的字串‘mysql’不要在指令碼名字出現,如果出現則不準
法三:實現思路是通過web串連傳回值判斷:
#!/bin/bashmysql -uroot -proot -e "select version();" &>/dev/nullif [ $? -ne 0 ]then /etc/init.d/mysqld startelse echo "MySQL is running."fi執行結果:[[email protected] ~]# sh mysql1.sh MySQL is running.
小結:web服務監控手段:
連接埠(本地或者遠程)
本地進程
header(httpd code)
URL(wget,curl
本文出自 “小菜鳥” 部落格,請務必保留此出處http://baishuchao.blog.51cto.com/12918589/1942148
監控MYSQL服務是否異常