在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1)端口判断法==>仅适合数据库本地使用 法1:if条件判断方法 [root@oldboy 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 法2:条件表达式用法 [root@oldboy 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." 法3:使用系统函数显示法 [root@oldboy 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)进程加端口判断法==>仅适合数据库本地使用 [root@oldboy 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 提示:如果不放心还可以同时加上模拟用户访问。 技巧:脚本调试技巧 1)grep过滤的字符串,不要包含在文件名里,例:grep mysql ,文件名为check_mysql.sh 就不太好。比较好的文件名如: 法1:端口检查法==>适合在远端服务器使用(本地也适合) [root@oldboy 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 提示:端口的使用方法还有很多种,包括不限于下面地址的3种方法,http://oldboy.blog.51cto.com/2561410/942530 [root@oldboy 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 技巧: <?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(); } ?> 以上的方法仅仅是简单的判断,那么下面给出一个集判断,处理,及报警于一身的较专业的mysql检查处理脚本。 #!/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" #→全变量定义方式,显得更专业。 $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#→日志也是变量。 sleep 5; $MYSQL_CMD -e "select version();" >/dev/null 2>&1 if [ $? -ne 0 ] then for num in `seq 10`#→通过for循环来杀死mysqld,真正杀死则退出循环或每隔个两秒杀一次,一共杀10次。 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"#→这个逻辑更准确。 echo "MySQL status is $Status" >>$LOG_FILE mail -s "MySQL status is $Status" [email protected] < $LOG_FILE #→把上面的Status作为结果标题传给邮件,当然你可以做短信,语音通话报警。 fi exit |
请发表评论