#!/bin/bash PROG_OUT="/root/checkmysql-group.log" # 输出日志到至此文件,建议配置绝对路径 #PROG_OUT="/dev/null" #如果不想输出日志,则打开注释 MYSQL_IP=127.0.0.1 MYSQL_PORT=3306 MYSQL_USER=root MYSQL_PASSWORD=kWYM6%pnbVnvsR4K MYSQL_PATH=/usr/bin/mysql echo "">>$PROG_OUT echo $(date "+%Y-%m-%d %H:%M:%S") check start >>$PROG_OUT if netstat -lntp | grep -q ':'$MYSQL_PORT' ';then echo port $MYSQL_PORT exist >>$PROG_OUT else echo port $MYSQL_PORT not exist >>$PROG_OUT echo $(date "+%Y-%m-%d %H:%M:%S") check end >>$PROG_OUT exist 1 fi mysql_master_host_name=`$MYSQL_PATH -h$MYSQL_IP -u$MYSQL_USER -p$MYSQL_PASSWORD -P$MYSQL_PORT -e "SELECT * FROM performance_schema.replication_group_members WHERE MEMBER_ID = (SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member')" |awk 'NR==2{print}'|awk -F" " '{print $3}'` echo mysql_master_host_name:$mysql_master_host_name>>$PROG_OUT mysql_machine_host_name=`hostname` echo mysql_machine_host_name:$mysql_machine_host_name>>$PROG_OUT if [ "$mysql_machine_host_name"X = "$mysql_master_host_name"X ];then echo this is Master >>$PROG_OUT echo $(date "+%Y-%m-%d %H:%M:%S") check end >>$PROG_OUT exit 0 else echo this is not Master >>$PROG_OUT echo $(date "+%Y-%m-%d %H:%M:%S") check end >>$PROG_OUT exit 1 fi