生產環境監控mysql服務狀態

來源:互聯網
上載者:User

標籤:mysql shell 監控

在生產環境中,如果某個重要的服務關閉了,可能會產生不可預料的結果,這時候,我們要作一個指令碼,這個指令碼可以監控服務狀態,在服務關閉的時候,可以自動開啟服務,確保使用者的利益。

下面我自己寫了一個監控Mysql服務的指令碼,目前還沒有發現bug,如果有大神發現bug的話可以和我說一下哈,本人QQ:1090139534,我也是剛剛學習的,大家可以加我和我交流。

#3306是mysqld服務的連接埠,看看是否是LISTEN狀態。

stat="`netstat -lnt|grep 3306 |grep  LISTEN`" 

#判斷$stat是否為空白

if [ -z "$stat" ]; 

then

#如果為空白就重啟服務

/etc/init.d/mysqld restart 

#判斷上面的服務是否重啟成功,如果失敗就輸出the mysql service restart fail

[ $? -ne 0 ] && echo "the mysql service restart fail" 

else

#如果$stat不為空白就輸出the mysql service is running

echo "the mysql service is running" 

fi

下面我們來實現一下。

[[email protected] shell]# service mysqld status

mysqld (pid  53476) 正在運行...

[[email protected] shell]# service mysqld stop

停止 mysqld:                                              [確定]

[[email protected] shell]# sh db_check.sh 

停止 mysqld:                                              [確定]

正在啟動 mysqld:                                           [確定]

[[email protected] shell]# sh db_check.sh 

the mysql service is running

以上是我自己寫的,下面這個是老師寫的

#!/bin/bash

#查看3306連接埠有沒有開啟,並算出3306的行數

portNum=`netstat -lnt|grep 3306|wc -l`

#判斷行數是否為0

if [ $portNum -ne 0 ];

then

#如果行數不為0就說明在運行

echo "mysql is running"

else

#如果行數為0就說明不在運行,就要重啟服務

/etc/init.d/mysqld restart

fi

下面是運行效果

[[email protected] shell]# service mysqld stop

停止 mysqld:                                              [確定]

[[email protected] shell]# sh db_check1.sh    

停止 mysqld:                                              [確定]

正在啟動 mysqld:                                          [確定]

[[email protected] shell]# 

最好就是轉化為數字,老師就是這樣弄的,上面之所以把指令碼的名字改為db_check.sh是因為我們還有一個題目,這個題目是要查詢mysql進程的,如果你的指令碼裡面有mysql的字眼,可能會影響結果。

一般我們寫指令碼的時候,可以先看看在指令碼裡面執行的話是什麼結果,比如說,我們可以先看看mysqld服務正常啟動的時候,命令ps -aux | grep mysql |wc -l和命令netstat -lnt|grep 3306 |grep  LISTEN的結果是什麼,這樣方便我們寫指令碼。

[[email protected] shell]# service mysqld status

mysqld (pid  54334) 正在運行...

[[email protected] shell]# ps -ef | grep mysql |wc -l

3

[[email protected] shell]# netstat -lnt|grep 3306 |grep  LISTEN

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN 

下面是我寫的指令碼,這個指令碼和上面一樣的功能,不同的是還要驗證Mysql有沒有運行進程,如果沒有,還是要重啟服務

#!/bin/bash

stat="`netstat -lnt|grep 3306 |grep  LISTEN`"

progress="`ps -ef | grep mysql |wc -l`" 

#3306是mysqld服務的連接埠,看看是否是LISTEN狀態。

if [ -z "$stat" -a "$progress" -ne 1 ];

#判斷$stat是否為空白

then

/etc/init.d/mysqld restart

#如果為空白就重啟服務

[ $? -ne 0 ] && echo "the mysql service restart fail" 

#判斷上面的服務是否重啟成功,如果失敗就輸出the mysql service restart fail

else

echo "the mysql service is running" 

#如果$stat不為空白就輸出the mysql service is running

fi

下面這個是在生產環境中指令碼,可以看看

#!/bin/bash

MYSQL=/etc/init.d/mysqld

LogPath=/tmp/mysqld.log

portNum=`netstat -lnt|grep 3306|wc -l`

mysqlProcessNum=`ps -ef|grep mysqld|grep -v grep|wc -l`

if [[ $portNum -eq 1 && $mysqlProcessNum -eq 2  ]];then

        echo "mysqld is running"

else

        $MYSQL start >$LogPath

        portNum=`netstat -lnt|grep 3306|wc -l`

        mysqlProcessNum=`ps -ef|grep mysqld|grep -v grep|wc -l`

        if [[ $portNum -ne 1 && $mysqlProcessNum -ne 2 ]];then

          while true #因為有時候我們殺死一個進程可能殺不死,所以我們要不斷的殺這個進程

          do

                killall mysqld >/dev/null 2>&1

#如果這個進程給我們殺死了,再執行這個命令時就會提示沒有可以殺的進程,這樣,我們殺死進程的>

命令就會執行不了了,這樣$?就不等於0,這時候就說明我們已經完全殺了這個進程啦。就可以跳出這>

個迴圈

                [ $? -ne 0 ] && break

                sleep 1

          done

                $MYSQL start >>$LogPath && status="successful"||status="failure"

                mail -s "mysql startup status is $status" [email protected] <$LogPath

        fi

fi

還有一種就是比較進階的,它的判斷是:用一個使用者去登入,看看能不能登入上,登入上了還要自動的去查詢版本,再看看上面的命令有沒有成功,如果成功了就說明mysql在運行,下面是我自己寫的

#!/bin/bash

MYSQL=/etc/init.d/mysqld

LogPath=/tmp/mysqld.log

mysql -uroot -p123456 -e "select version();" >&/dev/null

if [ $? -eq 0  ];then

        echo "mysqld is running"

else

        $MYSQL start >$LogPath

        mysql -uroot -p123456 -e "select version();" >&/dev/null

        if [ $? -ne 0  ];then

          while true #因為有時候我們殺死一個進程可能殺不死,所以我們要不斷的殺這個進程

          do

                killall mysqld >/dev/null 2>&1

#如果這個進程給我們殺死了,再執行這個命令時就會提示沒有可以殺的進程,這樣,我們殺死進程的>

命令就會執行不了了,這樣$?就不等於0,這時候就說明我們已經完全殺了這個進程啦。就可以跳出這>

個迴圈

                [ $? -ne 0 ] && break

                sleep 1

          done

                $MYSQL start >>$LogPath && status="successful"||status="failure"

                mail -s "mysql startup status is $status" [email protected] <$LogPath

        fi

fi

可是我們發現,這個只是針對我們這台機的帳號密碼,如果是別的帳號密碼呢?這時候我們可以作一個指令碼,這個指令碼是專門用來寫帳號密碼的,多說無益,我們來實驗一下吧

首先先寫一個放帳號密碼的指令碼。把我們的帳號密碼放進去

#!/bin/bash

user=root

password=123456

下面我們對上面的那個指令碼改造一下,把上面的使用者root改為變數$user把密碼123456改為變數$password如下

#!/bin/bash

MYSQL=/etc/init.d/mysqld

LogPath=/tmp/mysqld.log

mysql -u$user -p$password -e "select version();" >&/dev/null

if [ $? -eq 0  ];then

        echo "mysqld is running"

else

        $MYSQL start >$LogPath

        mysql -u$user -p$password -e "select version();" >&/dev/null

        if [ $? -ne 0  ];then

          while true #因為有時候我們殺死一個進程可能殺不死,所以我們要不斷的殺這個進程

          do

                killall mysqld >/dev/null 2>&1

#如果這個進程給我們殺死了,再執行這個命令時就會提示沒有可以殺的進程,這樣,我們殺死進程的>

命令就會執行不了了,這樣$?就不等於0,這時候就說明我們已經完全殺了這個進程啦。就可以跳出這>

個迴圈

                [ $? -ne 0 ] && break

                sleep 1

          done

                $MYSQL start >>$LogPath && status="successful"||status="failure"

                mail -s "mysql startup status is $status" [email protected] <$LogPath

        fi

fi

我們去看看效果

[[email protected] shell]# source database.sh 

[[email protected] shell]# source db_check3.sh 

mysqld is running

[email protected] shell]# service mysqld stop

停止 mysqld:                                              [確定]

[[email protected] shell]# source db_check3.sh

[[email protected] shell]# 

最後這種方法是老師推薦的,也是最簡單的一種方法

這個方法是用php/java程式監控mysql

這個方法我不知道怎麼寫,老師也只是提一下,上面的指令碼都有一個Bug,就算我們的資料庫正常,可是使用者訪問出問題了,那也沒有用呀,而用php/java的方法就不一樣,這個方法是類比使用者去訪問我們的網站,如果訪問正常就正常,這樣比較貼近使用者。

本文出自 “愛周瑜” 部落格,請務必保留此出處http://izhouyu.blog.51cto.com/10318932/1891756

生產環境監控mysql服務狀態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.