mysql/flush_query_cache.sh

73 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/mysql_credetials.conf"
#LOGGING=true
LOGGING=false
mysql=`realpath $(which mysql) 2>/dev/null`
if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
else
echo
echo -e "\t[ Error ]: \"mysql\" not found !!!"
echo
exit
fi
fi
# Get current version
#
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
CURRENT_VERSION="$(echo $_MYSQLD_VERSION | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?" | head -n 1)"
MYSQL_MAIN_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f1,2`
MYSQL_MAJOR_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f1`
MYSQL_MINOR_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f2`
MYSQL_PATCH_LEVEL=`echo $CURRENT_VERSION | cut -d '.' -f3`
if [[ "$MYSQL_MAJOR_VERSION" -gt 8 ]] \
|| ( [[ "$MYSQL_MAJOR_VERSION" -eq 8 ]] && [[ "$MYSQL_MINOR_VERSION" -gt 0 ]] ) \
|| ( [[ "$MYSQL_MAJOR_VERSION" -eq 8 ]] && [[ "$MYSQL_MINOR_VERSION" -eq 0 ]] \
&& [[ $MYSQL_PATCH_LEVEL -ge 3 ]] ); then
echo ""
echo -e "\t[ Error ]: Query cache is no longer supported since (MySQL 8.0.3)"
echo ""
fi
exit
#---------------------------------------
#-----------------------------
# Read Configurations from $conf_file
#-----------------------------
#---------------------------------------
if [[ -f "$conf_file" ]]; then
source "$conf_file"
fi
[[ -z "$mysql_credential_args" ]] && mysql_credential_args="--login-path=local"
if $LOGGING ;then
echo -e "\n[ `date` ] Going to flush query cache.."
fi
if [ -n "$mysql_credential_args" ] ; then
$mysql $mysql_credential_args -N -s -e "FLUSH QUERY CACHE"
else
$mysql -u$mysql_user -p$mysql_password -N -s -e "FLUSH QUERY CACHE"
fi
[[ $? -gt 0 ]] && echo -e "\t[ Error ]: Flushing query cache failed !!"
if $LOGGING ;then
echo -e "\n[ `date` ] End flushing query cache"
fi
exit 0