Production environment monitoring MySQL service status

Source: Internet
Author: User

In a production environment, if an important service is shut down, it can produce unpredictable results, when we want to make a script, this script can monitor the service status, when the service shuts down, can automatically open the service, to ensure the interests of users.

Below I wrote a monitoring MySQL service script, there is no bug found, if there is a big God found the bug can and I say ha, I qq:1090139534, I also just learned, we can add me and I exchange.

#3306是mysqld服务的端口 to see if it is a listen state.

Stat= "' Netstat-lnt|grep 3306 |grep LISTEN '"

#判断 whether the $stat is empty

If [-Z "$stat"];

Then

#如果为空就重启服务

/etc/init.d/mysqld restart

#判断上面的服务是否重启成功, if it fails, output the MySQL service restart fail

[$?-ne 0] && echo "The MySQL service restart fail"

Else

Output the MySQL service is running #如果 $stat not empty

echo "The MySQL service is running"

Fi

Let's take a look at the following.

[[Email protected] shell]# service mysqld status

MYSQLD (PID 53476) is running ...

[[Email protected] shell]# service mysqld stop

Stop mysqld: [OK]

[Email protected] shell]# sh db_check.sh

Stop mysqld: [OK]

Starting mysqld: [OK]

[Email protected] shell]# sh db_check.sh

The MySQL service is running

The above is my own writing, the following is written by the teacher

#!/bin/bash

#查看3306端口有没有开启, and figure out 3306 of the number of rows

portnum= ' Netstat-lnt|grep 3306|wc-l '

#判断行数是否为0

If [$portNum-ne 0];

Then

#如果行数不为0就说明在运行

echo "MySQL is running"

Else

#如果行数为0就说明不在运行, we need to restart the service.

/etc/init.d/mysqld restart

Fi

Here is the effect of running

[[Email protected] shell]# service mysqld stop

Stop mysqld: [OK]

[Email protected] shell]# sh db_check1.sh

Stop mysqld: [OK]

Starting mysqld: [OK]

[Email protected] shell]#

The best is to convert to a number, the teacher is this way, the reason for the name of the script to db_check.sh is because we have a topic, this topic is to query the MySQL process, if your script has MySQL word, may affect the results.

In general, when we write a script, we can first look at the script in the implementation of what is the result, for example, we can first look at the MYSQLD service started normally when the command Ps-aux | The result of grep MySQL |wc-l and command netstat-lnt|grep 3306 |grep Listen is so convenient for us to write scripts.

[[Email protected] shell]# service mysqld status

MYSQLD (PID 54334) is running ...

[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

Here is the script I wrote, this script and the same function, the difference is also to verify that MySQL has no running process, if not, or to restart the service

#!/bin/bash

Stat= "' Netstat-lnt|grep 3306 |grep LISTEN '"

Progress= "' Ps-ef | grep MySQL |wc-l ' "

#3306是mysqld服务的端口 to see if it is a listen state.

If [-Z ' $stat "-a" $progress "-ne 1];

#判断 whether the $stat is empty

Then

/etc/init.d/mysqld restart

#如果为空就重启服务

[$?-ne 0] && echo "The MySQL service restart fail"

#判断上面的服务是否重启成功, if it fails, output the MySQL service restart fail

Else

echo "The MySQL service is running"

Output the MySQL service is running #如果 $stat not empty

Fi

The following is a script in the production environment, you can see

#!/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 #因为有时候我们杀死一个进程可能杀不死, so we're going to keep killing this process.

Do

Killall mysqld >/dev/null 2>&1

#如果这个进程给我们杀死了, when you execute this command, you will be prompted not to kill the process, so that we kill the process of >

The order will not be able to execute, so that $0 is not equal to, this time it means that we have completely killed the process. You can jump out of this >

a loop

[$?-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

There is a more advanced, its judgment is: with a user to log in, see if you can log on, log on to the automatic query version, and then see if the above command is successful, if successful, it means that MySQL is running, the following is my own writing

#!/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 #因为有时候我们杀死一个进程可能杀不死, so we're going to keep killing this process.

Do

Killall mysqld >/dev/null 2>&1

#如果这个进程给我们杀死了, when you execute this command, you will be prompted not to kill the process, so that we kill the process of >

The order will not be able to execute, so that $0 is not equal to, this time it means that we have completely killed the process. You can jump out of this >

a loop

[$?-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

But we found that this is only for the account of our computer password, if it is another account password? At this time we can make a script, this script is dedicated to write account password, more useless, let's experiment.

First write a script to put the account password. Put our account code in.

#!/bin/bash

User=root

password=123456

Let's change the above script to the above user root to variable $user change password 123456 to variable $password as follows

#!/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 #因为有时候我们杀死一个进程可能杀不死, so we're going to keep killing this process.

Do

Killall mysqld >/dev/null 2>&1

#如果这个进程给我们杀死了, when you execute this command, you will be prompted not to kill the process, so that we kill the process of >

The order will not be able to execute, so that $0 is not equal to, this time it means that we have completely killed the process. You can jump out of this >

a loop

[$?-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

Let's go see the effect.

[Email protected] shell]# source database.sh

[Email protected] shell]# source db_check3.sh

Mysqld is running

[Email protected] shell]# service mysqld stop

Stop mysqld: [OK]

[Email protected] shell]# source db_check3.sh

[Email protected] shell]#

Finally, this method is recommended by the teacher, but also the simplest method

This method is to use the Php/java program to monitor MySQL

This method I do not know how to write, the teacher also just mention, the above script has a bug, even if our database is normal, but the user access problems, that is no use, and with Php/java method is not the same, this method is to impersonate the user to visit our website, if the access is normal, This is relatively close to the user.

This article is from "Love Zhou Yu" blog, please be sure to keep this source http://izhouyu.blog.51cto.com/10318932/1891756

Production environment monitoring MySQL service status

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.