分析 tuningprimer.sh 源码

昨天才第一次知道还有tuning-primer.sh这么个参数调优的脚本,然后和小周进行了分享,早上收到邮件他说这个脚本的数据不是很准确。于是就查看了它的代码,解析如下:
1:运行这个脚本可以看到花花绿绿的文字颜色的,就是在这里定义的

export black='\033[0m'
export boldblack='\033[1;0m'
export red='\033[31m'
。。。。。。。。

2:打印报告信息的头。

  1. print_banner ()
    

    3:检查socket,如果第一次配置后保存到了~/my.cnf,那么就直接去~/my.cnf取,要么就用/var/lib/mysql/mysql.sock 或者/var/run/mysqld/mysqld.sock或者/tmp/mysql.sock。如果都没有,那就报个错出来

check_for_socket ()

4:检查用户密码

check_for_plesk_passwords ()

5:用户登录

check_mysql_login ()

6:中间还有几个函数,不说了,下面看下2个关键函数

#########################################################################
# #
# Function to pull MySQL status variable #
# #
# Call using : #
# mysql_status \'Mysql_status_variable\' bash_dest_variable #
# #
#########################################################################


mysql_status () {
local status=$($mysql -Bse "show /*!50000 global */ status like $1" | awk '{ print $2 }')
export "$2"=$status
}


#########################################################################
# #
# Function to pull MySQL server runtime variable #
# #
# Call using : #
# mysql_variable \'Mysql_server_variable\' bash_dest_variable #
# - OR - #
# mysql_variableTSV \'Mysql_server_variable\' bash_dest_variable #
# #
#########################################################################


mysql_variable () {
local variable=$($mysql -Bse "show /*!50000 global */ variables like $1" | awk '{ print $2 }')
export "$2"=$variable
}

mysql_status:获取全局状态的函数。mysql_variable:获取全局变量的函数。
7:获取到的状态和参数值,应用在每个参数分析里面,下面举个完整的例子

check_used_connections () {


## -- Used Connections -- ##
 
## --获取参数值和状态值 

mysql_variable \'max_connections\' max_connections
mysql_status \'Max_used_connections\' max_used_connections
mysql_status \'Threads_connected\' threads_connected

## --计算使用率了

connections_ratio=$(($max_used_connections*100/$max_connections))


## --打印当前参数设置的值和状态值

cecho "MAX CONNECTIONS" boldblue
cecho "Current max_connections = $max_connections"
cecho "Current threads_connected = $threads_connected"
cecho "Historic max_used_connections = $max_used_connections"
cechon "The number of used connections is "

## 在这里进行分析了,算法就就在这里,我们也可以自己改,什么值显示什么颜色啥的。

if [ $connections_ratio -ge 85 ] ; then
txt_color=red
error=1
elif [ $connections_ratio -le 10 ] ; then
txt_color=red
error=2
else
txt_color=green
error=0
fi

## --打印分析结果了

# cechon "$max_used_connections " $txt_color
# cechon "which is "
cechon "$connections_ratio% " $txt_color
cecho "of the configured maximum."

##  打印建议信息

if [ $error -eq 1 ] ; then
cecho "You should raise max_connections" $txt_color
elif [ $error -eq 2 ] ; then
cecho "You are using less than 10% of your configured max_connections." $txt_color
cecho "Lowering max_connections could help to avoid an over-allocation of memory" $txt_color
cecho "See \"MEMORY USAGE\" section to make sure you are not over-allocating" $txt_color
else 
cecho "Your max_connections variable seems to be fine." $txt_color
fi
unset txt_color
}

check_used_connections只是个典型的例子,其他参数的处理方式都一样。
从上面的分析可以看出来,这个脚本的计算是准确的。它给出的建议信息,应该也是比较符合大众的,如果有特性要求,可以修改对应的算法。
有这么个东西就不需要一个个参数去计算了。VERY GOOD。

原文地址:https://www.cnblogs.com/zuoxingyu/p/3010024.html