mysql/max_memory_limit_mysql.sh

284 lines
11 KiB
Bash
Executable File

#!/usr/bin/env bash
working_dir="$(dirname $(realpath $0))"
log_dir="${working_dir}/log"
conf_file="${working_dir}/conf/mysql_credetials.conf"
tmp_log_file="$(mktemp)"
# -------------
# - Variable settings
# -------------
#DEFAULT_ACTION='create'
DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local"
if [[ -f "$conf_file" ]]; then
source "$conf_file" > $tmp_log_file 2>&1
fi
[[ -n "$mysql_credential_args" ]] || mysql_credential_args="$DEFAULT_MYSQL_CREDENTIAL_ARGS"
declare -i key_buffer_size
declare -i query_cache_size
declare -i tmp_table_size
declare -i innodb_buffer_pool_size
declare -i innodb_additional_mem_pool_size
declare -i innodb_log_buffer_size
declare -i max_connections
declare -i sort_buffer_size
declare -i read_buffer_size
declare -i read_rnd_buffer_size
declare -i join_buffer_size
declare -i thread_stack
declare -i binlog_cache_size
key_buffer_size=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'key_buffer_size'" | awk '{print$2}'`
query_cache_size=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'query_cache_size'" | awk '{print$2}'`
tmp_table_size=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'tmp_table_size'" | awk '{print$2}'`
innodb_buffer_pool_size=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'innodb_buffer_pool_size'" | awk '{print$2}'`
innodb_additional_mem_pool_size=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'innodb_additional_mem_pool_size'" | awk '{print$2}'`
innodb_log_buffer_size=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'innodb_log_buffer_size'" | awk '{print$2}'`
max_connections=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'max_connections'" | awk '{print$2}'`
sort_buffer_size=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'sort_buffer_size'" | awk '{print$2}'`
read_buffer_size=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'read_buffer_size'" | awk '{print$2}'`
read_rnd_buffer_size=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'read_rnd_buffer_size'" | awk '{print$2}'`
join_buffer_size=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'join_buffer_size'" | awk '{print$2}'`
thread_stack=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'thread_stack'" | awk '{print$2}'`
binlog_cache_size=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'binlog_cache_size'" | awk '{print$2}'`
mysql_version=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'version'" | awk '{print$2}' | sed -e's/-.*$//'`
mysql_version_compile_machine=`mysql $mysql_credential_args -N -s -e \
"SHOW VARIABLES LIKE 'version_compile_machine'" | awk '{print$2}'`
declare -i max_used_connections
declare -i uptime
declare -i threads_connected
max_used_connections=`mysql $mysql_credential_args -N -s -e \
"SHOW STATUS LIKE 'Max_used_connections'" | awk '{print$2}'`
threads_connected=`mysql $mysql_credential_args -N -s -e \
"SHOW STATUS LIKE 'Threads_connected'" | awk '{print$2}'`
uptime=`mysql $mysql_credential_args -N -s -e \
"SHOW STATUS LIKE 'Uptime'" | awk '{print$2}'`
_now=`date +%s`
_timestamp_start=`expr $_now - $uptime`
starttime=`date -d \@$_timestamp_start +%c`
days=`echo "scale=0 ; $uptime / 86400" | bc -l`
days_rest=`echo "scale=0 ; $uptime % 86400" | bc -l`
hours=`echo "scale=0 ; $days_rest / 3600" | bc -l`
hours_rest=`echo "scale=0 ; $days_rest % 3600" | bc -l`
minutes=`echo "scale=0 ; $hours_rest / 60" | bc -l`
seconds=`echo "scale=0 ; $hours_rest % 60" | bc -l`
uptime_string="$days days $hours hrs $minutes min $seconds sec"
## -
#declare -i myisam_sort_buffer_size=`mysql --login-path=local -N -s -e \
# "SHOW VARIABLES LIKE 'myisam_sort_buffer_size'" | awk '{print$2}'`
echo ""
_key_buffer_size=""
if [ $key_buffer_size -gt 1048576 ]; then
_key_buffer_size=" (`expr $key_buffer_size / 1024 / 1024`M)"
elif [ $key_buffer_size -gt 1024 ]; then
_key_buffer_size=" (`expr $key_buffer_size / 1024`K)"
fi
_query_cache_size=""
if [ $query_cache_size -gt 1048576 ]; then
_query_cache_size=" (`expr $query_cache_size / 1024 / 1024`M)"
elif [ $key_buffer_size -gt 1024 ]; then
_query_cache_size=" (`expr $query_cache_size / 1024`K)"
fi
_tmp_table_size=""
if [ $tmp_table_size -gt 1048576 ]; then
_tmp_table_size=" (`expr $tmp_table_size / 1024 / 1024`M)"
elif [ $tmp_table_size -gt 1024 ]; then
_tmp_table_size=" (`expr $tmp_table_size / 1024`K)"
fi
_innodb_buffer_pool_size=""
if [ $innodb_buffer_pool_size -gt 1048576 ]; then
_innodb_buffer_pool_size=" (`expr $innodb_buffer_pool_size / 1024 / 1024`M)"
elif [ $innodb_buffer_pool_size -gt 1024 ]; then
_innodb_buffer_pool_size=" (`expr $innodb_buffer_pool_size / 1024`K)"
fi
_innodb_additional_mem_pool_size=""
if [ $innodb_additional_mem_pool_size -gt 1048576 ]; then
_innodb_additional_mem_pool_size=" (`expr $innodb_additional_mem_pool_size / 1024 / 1024`M)"
elif [ $innodb_additional_mem_pool_size -gt 1024 ]; then
_innodb_additional_mem_pool_size=" (`expr $innodb_additional_mem_pool_size / 1024`K)"
fi
_innodb_log_buffer_size=""
if [ $innodb_log_buffer_size -gt 1048576 ]; then
_innodb_log_buffer_size=" (`expr $innodb_log_buffer_size / 1024 / 1024`M)"
elif [ $innodb_log_buffer_size -gt 1024 ]; then
_innodb_log_buffer_size=" (`expr $innodb_log_buffer_size / 1024`K)"
fi
_sort_buffer_size=""
if [ $sort_buffer_size -gt 1048576 ]; then
_sort_buffer_size=" (`expr $sort_buffer_size / 1024 / 1024`M)"
elif [ $sort_buffer_size -gt 1024 ]; then
_sort_buffer_size=" (`expr $sort_buffer_size / 1024`K)"
fi
_read_buffer_size=""
if [ $read_buffer_size -gt 1048576 ]; then
_read_buffer_size=" (`expr $read_buffer_size / 1024 / 1024`M)"
elif [ $read_buffer_size -gt 1024 ]; then
_read_buffer_size=" (`expr $read_buffer_size / 1024`K)"
fi
_read_rnd_buffer_size=""
if [ $read_rnd_buffer_size -gt 1048576 ]; then
_read_rnd_buffer_size=" (`expr $read_rnd_buffer_size / 1024 / 1024`M)"
elif [ $read_rnd_buffer_size -gt 1024 ]; then
_read_rnd_buffer_size=" (`expr $read_rnd_buffer_size / 1024`K)"
fi
_join_buffer_size=""
if [ $join_buffer_size -gt 1048576 ]; then
_join_buffer_size=" (`expr $join_buffer_size / 1024 / 1024`M)"
elif [ $join_buffer_size -gt 1024 ]; then
_join_buffer_size=" (`expr $join_buffer_size / 1024`K)"
fi
_thread_stack=""
if [ $thread_stack -gt 1048576 ]; then
_thread_stack=" (`expr $thread_stack / 1024 / 1024`M)"
elif [ $thread_stack -gt 1024 ]; then
_thread_stack=" (`expr $thread_stack / 1024`K)"
fi
_binlog_cache_size=""
if [ $binlog_cache_size -gt 1048576 ]; then
_binlog_cache_size=" (`expr $binlog_cache_size / 1024 / 1024`M)"
elif [ $binlog_cache_size -gt 1024 ]; then
_binlog_cache_size=" (`expr $binlog_cache_size / 1024`K)"
fi
echo -e "\tGlobal Buffers"
echo -e "\t--------------"
echo -e "\tkey_buffer_size...................: $key_buffer_size $_key_buffer_size"
echo -e "\tquery_cache_size..................: $query_cache_size $_query_cache_size"
echo -e "\ttmp_table_size....................: $tmp_table_size $_tmp_table_size"
echo -e "\tinnodb_buffer_pool_size...........: $innodb_buffer_pool_size $_innodb_buffer_pool_size"
echo -e "\tinnodb_additional_mem_pool_size...: $innodb_additional_mem_pool_size $_innodb_additional_mem_pool_size"
echo -e "\tinnodb_log_buffer_size............: $innodb_log_buffer_size $_innodb_log_buffer_size"
echo ""
echo -e "\tmax_connections...................: $max_connections"
echo ""
echo -e "\tPer Thread Buffers"
echo -e "\t------------------"
echo -e "\tsort_buffer_size..................: $sort_buffer_size $_sort_buffer_size"
echo -e "\tread_buffer_size..................: $read_buffer_size $_read_buffer_size"
echo -e "\tread_rnd_buffer_size..............: $read_rnd_buffer_size $_read_rnd_buffer_size"
echo -e "\tjoin_buffer_size..................: $join_buffer_size $_join_buffer_size"
echo -e "\tthread_stack......................: $thread_stack $_thread_stack"
echo -e "\tbinlog_cache_size.................: $binlog_cache_size $_binlog_cache_size"
declare -i max_memory_usage
max_memory_usage=`expr $key_buffer_size \
+ $query_cache_size \
+ $tmp_table_size \
+ $innodb_buffer_pool_size \
+ $innodb_additional_mem_pool_size \
+ $innodb_log_buffer_size \
+ $max_connections \* \( $sort_buffer_size \
+ $read_buffer_size \
+ $read_rnd_buffer_size \
+ $join_buffer_size \
+ $thread_stack \
+ $binlog_cache_size \)`
max_memory_usage_mb=`expr $max_memory_usage / 1024 / 1024`
#max_memory_usage_mb=`echo "scale=3; $max_memory_usage/1024/1024" | bc -l`
declare -i max_memory_allocated
max_memory_allocated=`expr $key_buffer_size \
+ $query_cache_size \
+ $tmp_table_size \
+ $innodb_buffer_pool_size \
+ $innodb_additional_mem_pool_size \
+ $innodb_log_buffer_size \
+ $max_used_connections \* \( $sort_buffer_size \
+ $read_buffer_size \
+ $read_rnd_buffer_size \
+ $join_buffer_size \
+ $thread_stack \
+ $binlog_cache_size \)`
max_memory_allocated_mb=`expr $max_memory_allocated / 1024 / 1024`
cur_memory_usage=`ps -ylC mysqld | grep mysqld | awk '{print$8}'`
cur_memory_usage_mb=`echo "scale=0; $cur_memory_usage/1024+1" | bc -l`
echo ""
echo ""
echo -e "\tSUM(Global Buffers) + max_connections * SUM(Per Thread Buffers)"
echo -e "\t==============================================================="
echo ""
if [ $max_memory_usage_mb -gt 1023 ]; then
max_memory_usage_gb=`echo "scale=3; $max_memory_usage_mb/1024" | bc -l`
echo -e "\tMax Memory Limit..................: $max_memory_usage_mb MB ($max_memory_usage_gb GB)"
else
echo -e "\tMax Memory Limit..................: $max_memory_usage_mb MB"
fi
echo ""
echo ""
echo "----- (Since last MySQL start)"
echo ""
echo -e "\tStart Time........................: $starttime"
echo -e "\tUptime............................: $uptime_string"
echo ""
echo -e "\tMax Used Connections..............: $max_used_connections"
if [ $max_memory_allocated_mb -gt 1023 ]; then
max_memory_allocated_gb=`echo "scale=3; $max_memory_allocated_mb/1024" | bc -l`
echo -e "\tMax Memory Allocated..............: $max_memory_allocated_mb MB ($max_memory_allocated_gb GB)"
else
echo -e "\tMax Memory Allocated..............: $max_memory_allocated_mb MB"
fi
echo ""
echo ""
echo "----- Current Status"
echo ""
echo -e "\tMySQL Version.....................: $mysql_version ($mysql_version_compile_machine)"
echo ""
if [ $cur_memory_usage_mb -gt 1023 ]; then
cur_memory_usage_gb=`echo "scale=3; $cur_memory_usage_mb/1024" | bc -l`
echo -e "\tCurrent Memory Usage..............: $cur_memory_usage_mb MB ($cur_memory_usage_gb GB)"
else
echo -e "\tCurrent Memory Usage..............: $cur_memory_usage_mb MB"
fi
echo -e "\tTreads Connected..................: $threads_connected"
echo ""
exit