Skip to content

Instantly share code, notes, and snippets.

@kevinclcn
Forked from lefred/check_async_mysql.sh
Created March 9, 2018 05:29
Show Gist options
  • Save kevinclcn/920541efb6b176b2279cb24849bfa1c6 to your computer and use it in GitHub Desktop.
Save kevinclcn/920541efb6b176b2279cb24849bfa1c6 to your computer and use it in GitHub Desktop.
Consul Asynchronous Slave Check
# script used in consul to check if mysql is primary master and asynchronous slave
# v.0.1 - lefred 2018-02-16
SLAVEOFDC="dc2"
SLAVEUSER="async_repl"
SLAVEPWD="asyncpwd"
# check if we are the primary one
ROLE=$(mysql -h 127.0.0.1 -BNe "select MEMBER_ROLE from performance_schema.replication_group_members where MEMBER_HOST=@@hostname")
if [[ "${ROLE}" == "PRIMARY" ]]
then
# let's set the kv of the primary
curl -s --request PUT --data "$(hostname)" http://localhost:8500/v1/kv/primary
# check if replication is setup and running
read -r current_master io_running sql_running <<<$(mysql -h 127.0.0.1 -P 3306 -Be 'show slave status\G' | grep '_Running:\|_Host:' | cut -d':' -f2)
otherdcmaster=$(curl -s "http://localhost:8500/v1/kv/primary?raw&dc=${SLAVEOFDC}")
echo "Current master: ${current_master} ${SLAVEOFDC} Primary: ${otherdcmaster}"
echo "IO: ${io_running} SQL: ${sql_running}"
if [[ "${current_master}" != "${otherdcmaster}" ]]
then
mysql -h 127.0.0.1 -P 3306 -e "STOP SLAVE; CHANGE MASTER TO MASTER_HOST='${otherdcmaster}'; START SLAVE"
sleep 5
fi
if [[ "${io_running}" == "Yes" ]] && [[ "${sql_running}" == "Yes" ]]
then
echo "This is the Primary Master and Async replication is running"
exit 0
elif [[ "${io_running}" == "Yes" ]] && [[ "${sql_running}" == "No" ]]
then
echo "This is the Primary Master but Async replication SQL thread not running... starting it"
mysql -h 127.0.0.1 -P 3306 -e "START SLAVE SQL_THREAD"
elif [[ "${io_running}" == "No" ]] && [[ "${sql_running}" == "Yes" ]]
then
echo "This is the Primary Master but Async replication IO thread not running... starting it"
mysql -h 127.0.0.1 -P 3306 -e "START SLAVE IO_THREAD"
elif [[ "${io_running}" == "No" ]] && [[ "${sql_running}" == "No" ]]
then
echo "This is the Primary Master but Async replication is not running... starting it"
mysql -h 127.0.0.1 -P 3306 -e "START SLAVE"
else
echo "This was not yet an async slave, setting it up"
mysql -h 127.0.0.1 -P 3306 -e "STOP SLAVE;CHANGE MASTER TO MASTER_HOST='${otherdcmaster}',
MASTER_PORT=3306, MASTER_USER='$SLAVEUSER', MASTER_PASSWORD='$SLAVEPWD',
MASTER_AUTO_POSITION=1; START SLAVE"
exit 2
fi
exit 1
else
# check if replication is running
read -r io_running sql_running <<<$(mysql -h 127.0.0.1 -P 3306 -Be 'show slave status\G' | grep '_Running:' | cut -d':' -f2)
if [[ "${io_running}" == "Yes" ]] || [[ "${sql_running}" == "Yes" ]]
then
echo "Replication should not be working, let's stop it"
mysql -h 127.0.0.1 -P 3306 -e "STOP SLAVE"
fi
fi
echo "Not Primary and not Async Slave"
exit 0# v.0.1 - lefred 2018-02-16
SLAVEOFDC="dc2"
SLAVEUSER="async_repl"
SLAVEPWD="asyncpwd"
# check if we are the primary one
ROLE=$(mysql -h 127.0.0.1 -BNe "select MEMBER_ROLE from performance_schema.replication_group_members where MEMBER_HOST=@@hostname")
if [[ "${ROLE}" == "PRIMARY" ]]
then
# let's set the kv of the primary
curl -s --request PUT --data "$(hostname)" http://localhost:8500/v1/kv/primary
# check if replication is setup and running
read -r current_master io_running sql_running <<<$(mysql -h 127.0.0.1 -P 3306 -Be 'show slave status\G' | grep '_Running:\|_Host:' | cut -d':' -f2)
otherdcmaster=$(curl -s "http://localhost:8500/v1/kv/primary?raw&dc=${SLAVEOFDC}")
echo "Current master: ${current_master} ${SLAVEOFDC} Primary: ${otherdcmaster}"
echo "IO: ${io_running} SQL: ${sql_running}"
if [[ "${current_master}" != "${otherdcmaster}" ]]
then
mysql -h 127.0.0.1 -P 3306 -e "STOP SLAVE; CHANGE MASTER TO MASTER_HOST='${otherdcmaster}'; START SLAVE"
sleep 5
fi
if [[ "${io_running}" == "Yes" ]] && [[ "${sql_running}" == "Yes" ]]
then
echo "This is the Primary Master and Async replication is running"
exit 0
elif [[ "${io_running}" == "Yes" ]] && [[ "${sql_running}" == "No" ]]
then
echo "This is the Primary Master but Async replication SQL thread not running... starting it"
mysql -h 127.0.0.1 -P 3306 -e "START SLAVE SQL_THREAD"
elif [[ "${io_running}" == "No" ]] && [[ "${sql_running}" == "Yes" ]]
then
echo "This is the Primary Master but Async replication IO thread not running... starting it"
mysql -h 127.0.0.1 -P 3306 -e "START SLAVE IO_THREAD"
elif [[ "${io_running}" == "No" ]] && [[ "${sql_running}" == "No" ]]
then
echo "This is the Primary Master but Async replication is not running... starting it"
mysql -h 127.0.0.1 -P 3306 -e "START SLAVE"
else
echo "This was not yet an async slave, settting it up"
mysql -h 127.0.0.1 -P 3306 -e "CHANGE MASTER TO MASTER_HOST='${otherdcmaster}',
MASTER_PORT=3306, MASTER_USER='$SLAVEUSER', MASTER_PASSWORD='$SLAVEPWD',
MASTER_AUTO_POSITION=1; START SLAVE"
exit 2
fi
exit 1
else
# check if replication is running
read -r io_running sql_running <<<$(mysql -h 127.0.0.1 -P 3306 -Be 'show slave status\G' | grep '_Running:' | cut -d':' -f2)
if [[ "${io_running}" == "Yes" ]] || [[ "${sql_running}" == "Yes" ]]
then
echo "Replication should not be working, let's stop it"
mysql -h 127.0.0.1 -P 3306 -e "STOP SLAVE"
fi
fi
echo "Not Primary and not Async Slave"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment