1 #!/bin/bash
2
5 pass="PASSWORD"
6 user="USER"
#建议使用一个只能本机登陆的账号
#grant all on . to user@localhost identified by 'password'
#flush privileges
7 port=netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $4}'|awk '{print $1}'#获取当前mysql的port
8 ip=ifconfig em1|grep "inet addr" | awk -F[:" "]+ '{print $4}' #获取当前主机的IP
9 array=($(/home/mysql/bin/mysql -u$user -p$pass -e "show slave status\G"|grep "Running" |awk '{print $ 2}')) #数组的方式实现查看SQL,IO,slave的线程
10 if [ "$port" == "3306" ] #如果mysql在线
11 then
12 if [ "${array[0]}" == "Yes" ] || [ "${array[1]}" == "Yes" ]
13 then
14 echo "slave is OK"
15 else
16 echo "$ip Slave is not running" | mail -s "请查看主从模式是否出错" [email protected]
18
19 fi
20 else
21 echo "$ip mysqld 可能停止运行 请查看" | mail -s "mysql 停止运行" [email protected]
22
23 fi
~
升级版:参考:http://blog.chinaunix.net/uid-7589639-id-3018209.html

#!/bin/bash
2
5 pass="PASSWORD"
6 user="USER"
#建议使用一个只能本机登陆的账号
#grant all on . to user@localhost identified by 'password'
#flush privileges
7 port=netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $4}'|awk '{pint $1}'#获取当前mysql的port
8 ip=ifconfig em1|grep "inet addr" | awk -F[:" "]+ '{print $4}' #获取当前主机的IP
9 array=($(mysql -u$user -p$pass -e "show slave status\G"|grep "Running" |awk '{print $2}')) #数组的方式实现查看SQL,IO,slave的线程
10 if [ "$port" == "3306" ] #如果mysql在线
11 then
12 if [ "${array[0]}" == "Yes" ] || [ "${array[1]}" == "Yes" ]
13 then
14 echo "slave is OK"
15 else
16 echo "$ip Slave is not running" | mail -s "请查看主从模式是否出错" [email protected]
echo "*****"
echo "Now Starting replication with Master Mysql!"

post=/home/mysql/bin/mysql -u $user -p$pass -e "show master status\G"|grep "Pos"|awk '{print $2}'`
/home/mysql/bin/mysql -u $user -p$pass -e "slave stop;change master to master_host='主-mysqlIP',master_port=3306,master_user='$user',master_password='$psss',master_log_file='$file',master_log_pos=$pos;slave start;"
sleep 2
/home/mysql/bin/mysql -u $user -p$pass -e "show slave status\G;"|grep Running
18
19 fi
20 else
21 echo "$ip mysqld 可能停止运行 请查看" | mail -s "mysql 停止运行" [email protected]
service mysqld restart
22
23 fi
~