#!/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin check_type=$1 # check if input argument if [ "$check_type" == "rw" ] || [ "$check_type" == "ro" ] then : else echo "Please input the argument" echo "rw or ro" exit 1 fi # Check if process exists /usr/bin/killall -0 mysqld # returns 0 if process exists if [ $? -ne 0 ] then echo "mysqld process does not exists, return result 1" exit 1 else # Check if instance connected MYSQL_PWD=repl mysql -sN --connect-timeout=2 --host=127.0.0.1 --port=3306 --user=repl -e "select 1" if [ $? -eq 0 ] then echo "mysqld running and connected" else echo "mysql process exists but instance does not connected, return result 1" exit 1 fi fi # Check if Source/Replica instance v_replica_exists=`MYSQL_PWD=repl mysql -sN --connect-timeout=2 --host=127.0.0.1 --port=3306 --user=repl -e "show replicas"` v_source_exists=`MYSQL_PWD=repl mysql -sN --connect-timeout=2 --host=127.0.0.1 --port=3306 --user=repl -e "show replica status"` v_io_thread_running_check=`MYSQL_PWD=repl mysql --connect-timeout=2 --host=127.0.0.1 --port=3306 --user=repl -se "show replica status\G" | grep "IO_Running:" | awk '{print $2}'` v_sql_thread_running_check=`MYSQL_PWD=repl mysql --connect-timeout=2 --host=127.0.0.1 --port=3306 --user=repl -se "show replica status\G" | grep "SQL_Running:" | awk '{print $2}'` v_read_only=`MYSQL_PWD=repl mysql -sN --connect-timeout=2 --host=127.0.0.1 --port=3306 --user=repl -e "select @@read_only"` if [ "$v_read_only" == "1" ] && [ "$check_type" == "rw" ] then echo "MySQL instance is not Source, return result 1" exit 1 elif [ "$v_read_only" == "0" ] && [ "$check_type" == "rw" ] then echo "MySQL instance is Source, return result 0" exit 0 elif [ "$v_source_exists" == "" ] && [ "$v_read_only" == "1" ] && [ "$check_type" == "ro" ] then echo "Replication has not yet been set up in MySQL Replica, return result 1" exit 1 elif [ "$v_source_exists" != "" ] && [ "$v_read_only" == "1" ] && [ "$check_type" == "ro" ] then echo "MySQL instance is Replica and Replication is configured, return result 0" exit 0 elif [ "$v_source_exists" == "" ] && [ "$v_replica_exists" == "" ] && [ "$v_read_only" == "0" ] && [ "$check_type" == "ro" ] then echo "MySQL instance it not Replica, instance is Source but Replica host does not exists" #echo "May be Replica instance down or Replica instance started but Replication Not start" echo "May be Replica instance down or Replica instance started but Replication is not yet configured" echo "return result 0" exit 0 elif [ "$v_source_exists" == "" ] && [ "$v_read_only" == "0" ] && [ "$check_type" == "ro" ] then echo "MySQL instance it not Replica, instance is Source and return result 1" exit 1 fi