Skip to content

Instantly share code, notes, and snippets.

@jaygooby
Created August 29, 2018 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaygooby/27961d6e7f7afa539d433991431de223 to your computer and use it in GitHub Desktop.
Save jaygooby/27961d6e7f7afa539d433991431de223 to your computer and use it in GitHub Desktop.
Very basic mysql health check
# A very basic mysql health check that can be used by e.g.
# Consul's External Service Monitor
#
# Cobbled together by @jaygooby from a combination of:
# Unai Rodriguez's and Alex Williams'
# https://gist.github.com/aw/1071144
# and George Chilumbu's
# https://georgechilumbu.wordpress.com/2017/07/27/setup-a-consul-client/
#
# This script checks if a mysql server is healthy running on $MYSQL_HOST.
# It will exit with 0 if successful or with an error if not.
#
# It is recommended that a low-privileged-mysql user is created to be used by
# this script. Something like this:
#
# mysql> GRANT SELECT on mysql.* TO 'mysqlhealthcheck'@'localhost' \
# -> IDENTIFIED BY 'supersecretpassword' WITH GRANT OPTION;
# mysql> flush privileges;
#
MYSQL_HOST=${MYSQL_HOST:-127.0.0.1}
MYSQL_PORT=${MYSQL_PORT:-3306}
MYSQL_USER=${MYSQL_USER:-mysqlhealthcheck}
MYSQL_PASS=${MYSQL_PASS:-supersecretpassword}
status=$(mysql -u "$MYSQL_USER" -p"$MYSQL_PASS" --host "$MYSQL_HOST" -e "select 1\G" | grep "1: 1");
if [ "$?" -ne 0 ]; then
echo "Cannot connect to mysql."
exit 2
elif [[ "$status" != *"1: 1"* ]]; then
echo "MySQL is running, but cannot connect."
exit 2
else
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment