Because heartbeat itself can use dopd to determine if the heartbeat line is down, automatic switching can be realized. However, when some services are down, automatic switching is impossible. Common scenarios include mysqld service, sometimes the mysqld service cannot be connected. For example, when the maximum number of connections is exceeded, there are other scenarios, therefore, I wrote a script to automatically monitor the mysqld service status to switch the heartbeat service status. For more information about heartbeat + mysqld + shared storage, see the previous blog, click "Practical Heartbeat + mysql + shared storage for High Availability".
Next, let's take a look at the script:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1AU55531-0.jpg "title =" lw.jpg "/>
The script content is as follows:
#!/bin/bash#:Title:monitor heartbeat's mysql resource.#:Synopsis:#:Data:2013-08-20 13:36:32#:Version:1.1##################################### 2013.08 by LuoWei <luoweiro@126.com># Copyright (C) 2005 by Baison####################################trap 'echo PROGRAM INTERRUPTED; exit 1' INT#The user/password for connection mysql.username=rootpassword=n=0mysqlmon_log=/var/log/mysqlmon.logmysql_conmand="/usr/local/mysql/bin/mysql -u$username -e "#mysql_conmand="/usr/local/mysql/bin/mysql -u$username -p$password -e "echo >$mysqlmon_log#mail listfunction_add_maillist(){cat >/tmp/userlist<<EOF282127408@qq.comluoweiro@126.comEOF}[ -f /tmp/userlist ] && echo "The file has." || touch /tmp/userlist ;function_add_maillistfunction_mail(){ IFS=$'\n' for LINE in `cat /tmp/userlist`;do echo "Heartbeat has changed,please see see." | mail -s "Heartbeat standby" $LINE done}function_retry(){ echo `date +"%Y-%m-%d %H:%M:%S"` mysqld cannot be connected! >> $mysqlmon_log n=$[n + 1] service mysqld restart >/dev/null 2>&1}function_ha_standby(){ service heartbeat standby >/dev/null 2>&1 echo `date +"%Y-%m-%d %H:%M:%S"` mysqld switched to backup! >> $mysqlmon_log service heartbeat start >/dev/null 2>&1}while true;do ifconfig | grep eth0:0 >/dev/null 2>&1 if [[ $? -eq 0 ]];then if $mysql_conmand "use openshop;" >&/dev/null;then n=0 else function_retry if [ $n -eq 3 ] ; then function_ha_standby function_mail break fi fi fi sleep 10done
Since the heartbeat I used is two nodes, the index can be executed on both nodes.
Next, start the test:
1. Stop the mysqld service on the HA master node:
As the master node is now on HA2, as shown in:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1AU5D93-1.png "title =" ha2.1.png "/>
Now I am simulating to manually stop the mysqld service:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1AU54127-2.png "title =" ha2.2.png "/>
View the log records in the script:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1AU533D-3.png "title =" ha2.4.png "/>
Wait for a moment and check the resource status on HA2:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1AU55048-4.png "title =" ha2.3.png "/>
As you can see, the mysqld service is automatically started.
2. Damage to the mysqld service:
Since the cluster will automatically start the mysqld service, I will make the heartbeat startup unable to find the mysqld resource. Therefore, I will rename the mysqld service startup script and check the effect.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1AU55B2-5.png "title =" ha2.5.png "/>
Then check the status on the slave node:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1AU51Y8-6.png "title =" ha1.1.png "/>
The resource has been switched to ha1.
Next, view the cluster switch log:
A. First view the script log:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1AU52209-7.png "title =" ha12.png "/>
B. view the HA2 log:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1AU535S-8.png "title =" ha222.png "/>
C. View logs on HA1:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1AU55959-9.png "title =" ha1111.png "/>
All right, the script test is complete, and there are emails to notify you of cluster node switching.
Good luck!
This article is from the "Ro NLP blog" and will not be reposted!