#! /bin/bash . /usr/src/k_framework/main.sh # ---- VARS ----- VERSION="1.0.4" SCRIPT_NAME="MySQL Replication Check" # ---- FUNCTIONS ----- # ---- MAIN ----- k_start k_config_details k_mysql_version if [[ $DB_PRESENT == 0 ]]; then k_exit 0 fi replication_ip=$(MYSQL_PWD=$DB_PASSWORD /usr/bin/mysql -h $DB_HOST -u $DB_USERNAME $P_OPT $DB_NAME -e "show slave status\G" | grep -w 'Master_Host' | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}') if [ "$replication_ip" == "" ]; then report "MySQL Replication not found" 3 k_exit 0 else report "Master_Host: $replication_ip" 3 fi if [ "${REPLICATION_M}" == "0" ] && [ "${REPLICATION_S}" == "0" ]; then report "Replication is active but REPLICATION_M/REPLICATION_S are set to 0 in the conf file" 1 report " Fix these values manualy" fix EXIT_CODE=1 fi if [[ "${REPLICATION_M}" == "" || "${REPLICATION_S}" == "" ]]; then report "Replication is active but REPLICATION_M/REPLICATION_S are not set properly in the conf file" 1 report " Fix these values manualy" fix EXIT_CODE=1 fi # check slave-skip-errors/replica-skip-errors variable if [[ $MYSQL_VERSION1 == 8 ]]; then if grep -Eq "(replica|slave)[-_]skip[-_]errors[[:space:]]*=[[:space:]]*1061,1062,1213,1060,1032,1396" /etc/my.cnf; then report "MySQL replication variable replica-skip-error/slave-skip-errors is configured correctly" 0 else report " Set replica-skip-errors=1061,1062,1213,1060,1032,1396 in /etc/my.cnf" fix report " service mysqld restart" fix fi else if grep -Eq "(replica|slave)[-_]skip[-_]errors[[:space:]]*=[[:space:]]*1061,1062,1213,1060,1032" /etc/my.cnf; then report "MySQL replication variable replica-skip-error/slave-skip-errors is configured correctly" 0 else report " Set slave-skip-errors=1061,1062,1213,1060,1032,1396 in /etc/my.cnf" fix report " service mysqld restart" fix fi fi # -------- my.cnf check ------------ report "Checking /etc/my.cnf" 3 server_id=`cat /etc/my.cnf | awk -F "#" '{print $1}' | grep server-id | awk -F "=" '{print $2}' | xargs` offset=`cat /etc/my.cnf | awk -F "#" '{print $1}' | grep auto_increment_offset | awk -F "=" '{print $2}' | xargs` match=0 if [[ $server_id == 10 ]] && [[ $offset == 1 ]]; then match=1 fi if [[ $server_id == 20 ]] && [[ $offset == 2 ]]; then match=1 fi if [[ $match == 1 ]];then report "server-id [$server_id] and auto_increment_offset [$offset] match" 0 else report "server-id [$server_id] and auto_increment_offset [$offset] does not match" 1 EXIT_CODE=1 fi slave_server_id=$(MYSQL_PWD=$DB_PASSWORD /usr/bin/mysql -h $DB_HOST -u $DB_USERNAME $P_OPT $DB_NAME -e "show slave status\G" | grep Master_Server_Id | awk -F ":" '{print $2}' | xargs) if [[ $server_id == $slave_server_id ]]; then report "server-id [$server_id] same as Slave server-id [$slave_server_id]" 1 report " Fix server-id, they should not match. Also check/fix auto_increment_offset" fix else report "server-id [$server_id] different from Slave server-id [$slave_server_id]" 0 fi k_exit $EXIT_CODE