-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuning-mysql.sh
1621 lines (1402 loc) · 63.2 KB
/
tuning-mysql.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# vim: sw=2:et
#########################################################################
# #
# MySQL performance tuning primer script #
# Writen by: Matthew Montgomery and Dan Reif #
# Report bugs to: https://github.com/BMDan/tuning-primer.sh/issues#
# Inspired by: MySQLARd (http://gert.sos.be/demo/mysqlar/) #
# Version: 1.99 Released: 2018-06-10 #
# Licenced under GPLv2 #
# #
#########################################################################
#########################################################################
# #
# Usage: ./tuning-primer.sh [ mode ] #
# #
# Available Modes: #
# all : perform all checks (default) #
# prompt : prompt for login credentials and socket #
# and execution mode #
# mem, memory : run checks for tunable options which #
# affect memory usage #
# disk, file : run checks for options which affect #
# i/o performance or file handle limits #
# innodb : run InnoDB checks /* to be improved */ #
# misc : run checks that don't fit categories #
# well Slow Queries, Binary logs, #
# Used Connections and Worker Threads #
#########################################################################
# #
# Set this socket variable ONLY if you have multiple instances running #
# or we are unable to find your socket, and you don't want to to be #
# prompted for input each time you run this script. #
# #
#########################################################################
socket=
function colorize() {
# As an explicit special case, "" yields sgr0 (i.e., black).
local originalcolor="${1}"
local basecolor="${originalcolor}"
basecolor="${basecolor#bold}"
basecolor="${basecolor#so}"
case "$basecolor" in
'red')
tput setaf 1 ;;
'green')
tput setaf 2 ;;
'yellow')
tput setaf 3 ;;
'blue')
tput setaf 4 ;;
'magenta')
tput setaf 5 ;;
'cyan')
tput setaf 6 ;;
'white')
tput setaf 7 ;;
*) # Also "black":
[ "$basecolor" != "black" ] && [ -n "$basecolor" ] && echo "No such color '$basecolor'." >&2
tput sgr0 ;;
esac
# bold, etc.
if [ "$basecolor" != "$originalcolor" ]; then
case $originalcolor in
bold*)
tput bold ;;
so*)
tput smso ;;
*)
echo "No such color modifier '${originalcolor%$basecolor}'." >&2 ;;
esac
fi
}
function cecho()
{
if [ -z "${1-}" ]; then
cecho "No message passed." "${2-}"
return $?
fi
cechon "$1"$'\n' "${2-}"
return $?
}
function cechon()
{
## -- Function to easily print colored text -- ##
# Color-echo.
# Argument $1 = message
# Argument $2 = color
local default_msg="No message passed."
message=${1:-$default_msg} # Defaults to default message.
#change it for fun
#We use pure names
color=${2:-black} # Defaults to black, if not specified.
colorize "$color"
printf "%s" "$message"
colorize "" # Reset to normal.
return
}
function write_mycnf() {
# $1: Path to .my.cnf
# $2: Path to socket (optional)
# $3: username
# $4: password
local socketcomment=""
[ -z "$2" ] && socketcomment="#"
local cleanpassword="${4//\\/\\\\}"
cleanpassword="${cleanpassword//\"/\\\"}"
cat > "${1}" <<EOF
[client]
${socketcomment}socket=$2
user=$3
password="${cleanpassword}"
EOF
}
function print_banner()
{
cecho " -- MYSQL PERFORMANCE TUNING PRIMER --" boldblue
cecho " - By: Matthew Montgomery -" black
}
## -- Find the location of the mysql.sock file -- ##
function check_for_socket()
{
if [ -n "$socket" ] && [ ! -S "$socket" ]; then
# If you specify a socket but that socket doesn't exist, we exit.
cecho "No valid socket file at '$socket'!" boldred
cecho "You've explicitly specified a socket location, but that location either" red
cecho "doesn't exist, or isn't a socket." red
exit 1
fi
# Otherwise, try to find the socket. Failure is now OK; we can always
# try another way to connect.
if [ -z "$socket" ] ; then
# Use ~/.my.cnf version
if [ -f ~/.my.cnf ] ; then
# Use the last one we find in the file. We could be smarter here and
# parse section headers and so forth, but meh.
cnf_socket="$(awk -F '=' '$1 == "socket" { s=$2 } END { print s }' ~/.my.cnf)"
fi
if [ -S "$cnf_socket" ] ; then
socket=$cnf_socket
elif [ -S /var/lib/mysql/mysql.sock ] ; then
socket=/var/lib/mysql/mysql.sock
elif [ -S /var/run/mysqld/mysqld.sock ] ; then
socket=/var/run/mysqld/mysqld.sock
elif [ -S /tmp/mysql.sock ] ; then
socket=/tmp/mysql.sock
else
if [ -S "$ps_socket" ] ; then
socket=$ps_socket
fi
fi
fi
if [ -S "$socket" ] ; then
export MYSQL_COMMAND="${MYSQL_COMMAND} -S ${socket}"
return 0
fi
return 1
}
check_for_plesk_passwords () {
## -- Check for the existance of plesk and login using its credentials -- ##
if [ -f /etc/psa/.psa.shadow ] ; then
MYSQL_COMMAND="mysql -S $socket -u admin -p$(cat /etc/psa/.psa.shadow)"
MYSQLADMIN_COMMAND="mysqladmin -S $socket -u admin -p$(cat /etc/psa/.psa.shadow)"
fi
}
check_mysql_login () {
## -- Try just connecting (i.e., via .my.cnf defaults, in practice) -- ##
local is_up=$($MYSQLADMIN_COMMAND ping 2>&1)
local print_defaults=$($MYSQLADMIN_COMMAND --print-defaults 2>/dev/null)
if [ "$is_up" = "mysqld is alive" ]; then
if [ "${print_defaults//*--host=*}" = "" ] &&
! [ "${print_defaults//*--host=127\.0\.0\.1*}" = "" ] &&
! [ "${print_defaults//*--host=::1*}" = "" ] &&
! [ "${print_defaults//*--host=localhost*}" = "" ]; then
cecho "WARNING: you might be connecting to a remote server. If so, some" boldred
cecho "results may be based on incorrect assumptions. See #13 on Github." boldred
fi
return 0
fi
printf "\n"
cecho "Using login values from ~/.my.cnf"
cecho "- INITIAL LOGIN ATTEMPT FAILED -" boldred
if [ -z $prompted ] ; then
find_webmin_passwords
fi
return 1
}
final_login_attempt () {
is_up=$($MYSQLADMIN_COMMAND ping 2>&1)
if [ "$is_up" = "mysqld is alive" ] ; then
return 0
else
cecho "- FINAL LOGIN ATTEMPT FAILED -" boldred
cecho "Unable to log into socket: $socket" boldred
exit 1
fi
}
## -- create a ~/.my.cnf and exit when all else fails -- ##
function second_login_failed()
{
cecho "Could not auto detect login info!"
cecho "Found potential sockets: $(xargs <<< "$found_socks")"
if [ -z "$socket" ]; then
cecho " Will use client's default socket (this is normally correct)." bold
else
cecho " Choosing: $socket" red
fi
read -rp "Would you like to override my socket choice?: [y/N] " REPLY
case $REPLY in
yes | y | Y | YES)
read -rp "Socket: " socket
;;
esac
read -rp "Do you have your login handy ? [y/N] : " REPLY
case $REPLY in
yes | y | Y | YES)
answer1='yes'
read -rp "User: " user
read -rsp "Password: " pass
export MYSQL_COMMAND="mysql"
export MYSQLADMIN_COMMAND="mysqladmin"
;;
*)
cecho "Please create a valid login to MySQL"
cecho "Or, set correct values for 'user=' and 'password=' in ~/.my.cnf"
;;
esac
cecho " "
echo "Would you like me to create a ~/.my.cnf file for you? If you answer 'N',"
read -p "then I'll create a secure, temporary one instead. [y/N] : " REPLY
case $REPLY in
yes | y | Y | YES)
answer2='yes'
if [ ! -f ~/.my.cnf ] ; then
umask 077
write_mycnf "${HOME}/.my.cnf" "$socket" "$user" "$pass"
if [ "$answer1" != 'yes' ] ; then
exit 1
else
final_login_attempt
return 0
fi
else
printf "\n"
cecho "~/.my.cnf already exists!" boldred
printf "\n"
read -p "Replace ? [y/N] : " REPLY
if [ "$REPLY" = 'y' ] || [ "$REPLY" = 'Y' ] ; then
write_mycnf "${HOME}/.my.cnf" "$socket" "$user" "$pass"
if [ "$answer1" != 'yes' ] ; then
exit 1
else
final_login_attempt
return 0
fi
else
cecho "Please set the 'user=' and 'password=' and 'socket=' values in ~/.my.cnf"
exit 1
fi
fi
;;
*)
if [ "$answer1" != 'yes' ] ; then
exit 1
else
local tempmycnf
tempmycnf="$(mktemp)"
write_mycnf "$tempmycnf" "$socket" "$user" "$pass"
export MYSQL_COMMAND="mysql --defaults-extra-file=$tempmycnf $MYSQL_COMMAND_PARAMS"
export MYSQLADMIN_COMMAND="mysqladmin --defaults-extra-file=$tempmycnf $MYSQL_COMMAND_PARAMS"
final_login_attempt
return 0
fi
;;
esac
}
find_webmin_passwords () {
## -- populate the .my.cnf file using values harvested from Webmin -- ##
cecho "Testing for stored webmin passwords:"
if [ -f /etc/webmin/mysql/config ] ; then
user=$(grep ^login= /etc/webmin/mysql/config | cut -d "=" -f 2)
pass=$(grep ^pass= /etc/webmin/mysql/config | cut -d "=" -f 2)
if [ $user ] && [ $pass ] && [ ! -f ~/.my.cnf ] ; then
cecho "Setting login info as User: $user Password: $pass"
touch ~/.my.cnf
chmod 600 ~/.my.cnf
write_mycnf "${HOME}/.my.cnf" "" "$user" "$pass"
cecho "Retrying login"
is_up=$($MYSQLADMIN_COMMAND ping 2>&1)
if [ "$is_up" = "mysqld is alive" ] ; then
echo UP > /dev/null
else
second_login_failed
fi
echo
else
second_login_failed
echo
fi
else
cecho " None Found" boldred
second_login_failed
fi
}
#########################################################################
# #
# Function to pull MySQL status variable #
# #
# Call using : #
# mysql_status \'Mysql_status_variable\' bash_dest_variable #
# #
#########################################################################
mysql_status () {
local status=$($MYSQL_COMMAND -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_COMMAND -Bse "show /*!50000 global */ variables like $1" | awk '{ print $2 }')
export "$2"=$variable
}
mysql_variableTSV () {
local variable=$($MYSQL_COMMAND -Bse "show /*!50000 global */ variables like $1" | awk -F '\t' '{ print $2 }')
export "$2"=$variable
}
# -- Divide two integers -- #
function divide()
{
usage="$0 dividend divisor '$variable' scale"
if [ $1 -ge 1 ] ; then
dividend=$1
else
cecho "Invalid Dividend" red
echo "$usage"
exit 1
fi
if [ $2 -ge 1 ] ; then
divisor=$2
else
cecho "Invalid Divisor" red
echo "$usage"
exit 1
fi
if [ ! -n $3 ] ; then
cecho "Invalid variable name" red
echo "$usage"
exit 1
fi
if [ -z $4 ] ; then
scale=2
elif [ $4 -ge 0 ] ; then
scale=$4
else
cecho "Invalid scale" red
echo "$usage"
exit 1
fi
export $3=$(echo "scale=$scale; $dividend / $divisor" | bc -l)
}
human_readable () {
#########################################################################
# #
# Convert a value in to human readable size and populate a variable #
# with the result. #
# #
# Call using: #
# human_readable $value 'variable name' [ places of precision] #
# #
#########################################################################
## value=$1
## variable=$2
scale=$3
if [ $1 -ge 1073741824 ] ; then
if [ -z $3 ] ; then
scale=2
fi
divide $1 1073741824 "$2" $scale
unit="G"
elif [ $1 -ge 1048576 ] ; then
if [ -z $3 ] ; then
scale=0
fi
divide $1 1048576 "$2" $scale
unit="M"
elif [ $1 -ge 1024 ] ; then
if [ -z $3 ] ; then
scale=0
fi
divide $1 1024 "$2" $scale
unit="K"
else
export "$2"=$1
unit="bytes"
fi
# let "$2"=$HR
}
function human_readable_time()
{
# Produce human readable time from a duration in seconds.
# Remove and save any fractional component
local secs="${1%.*}"
local subsecs="${1#$secs}"
if [ -z $1 ] || [ -z $2 ] ; then
cecho "${FUNCNAME[0]} seconds 'variable'" red
exit 1
fi
export $2="$((secs/86400)) days $((secs/3600%24)) hrs $((secs/60%60)) min $((secs%60))$subsecs sec"
}
check_mysql_version () {
## -- Print Version Info -- ##
mysql_variable \'version\' mysql_version
mysql_variable \'version_compile_machine\' mysql_version_compile_machine
if [ "$mysql_version_num" -lt 050000 ]; then
cecho "MySQL Version $mysql_version $mysql_version_compile_machine is EOL please upgrade to MySQL 4.1 or later" boldred
else
cecho "MySQL Version $mysql_version $mysql_version_compile_machine"
fi
}
post_uptime_warning () {
#########################################################################
# #
# Present a reminder that mysql must run for a couple of days to #
# build up good numbers in server status variables before these tuning #
# suggestions should be used. #
# #
#########################################################################
mysql_status \'Uptime\' uptime
mysql_status \'Threads_connected\' threads
queries_per_sec="$(($questions/$uptime)).$(printf '%02d' $((100*$questions/$uptime%100)))"
human_readable_time $uptime uptimeHR
cecho "Uptime = $uptimeHR"
cecho "Avg. qps = $queries_per_sec"
cecho "Total Questions = $questions"
cecho "Threads Connected = $threads"
echo
if [ $uptime -gt 172800 ] ; then
cecho "Server has been running for over 48hrs."
cecho "It should be safe to follow these recommendations"
else
cechon "Warning: " boldred
cecho "Server has not been running for at least 48hrs." boldred
cecho "It may not be safe to use these recommendations" boldred
fi
echo ""
cecho "To find out more information on how each of these" red
cecho "runtime variables effects performance visit:" red
if [ "$major_version" = '3.23' ] || [ "$major_version" = '4.0' ] || [ "$major_version" = '4.1' ] ; then
cecho "http://dev.mysql.com/doc/refman/4.1/en/server-system-variables.html" boldblue
elif [ "$major_version" = '5.0' ] || [ "$mysql_version_num" -gt '050100' ]; then
cecho "http://dev.mysql.com/doc/refman/$major_version/en/server-system-variables.html" boldblue
else
cecho "UNSUPPORTED MYSQL VERSION" boldred
exit 1
fi
echo ""
cecho "Visit https://github.com/BMDan/tuning-primer.sh for the latest version of" boldblue
cecho "this script, or to suggest improvements." boldblue
}
check_slow_queries () {
## -- Slow Queries -- ##
cecho "SLOW QUERIES" boldblue
mysql_status \'Slow_queries\' slow_queries
mysql_variable \'long_query_time\' long_query_time
mysql_variable \'log%queries\' log_slow_queries
mysql_variable \'slow_query_log\' slow_query_log
PREFERRED_QUERY_TIME=5
if [ -z "$log_slow_queries" ] ; then
log_slow_queries="$slow_query_log"
fi
if [ "$log_slow_queries" = 'ON' ] ; then
cecho "The slow query log is enabled."
elif [ "$log_slow_queries" = 'OFF' ] || [ -z "$log_slow_queries" ] ; then
cechon "The slow query log is "
cechon "NOT" boldred
cecho " enabled."
return
else
cecho "Slow query log check failed; error(s): $log_slow_queries/$slow_query_log" boldred
fi
cecho "Current long_query_time = $long_query_time sec."
cechon "Since startup, "
cechon "$slow_queries" boldred
cechon " out of "
cechon "$questions" boldred
cecho " queries have taken longer than <long_query_time-when-they-were-executed> to complete."
if [ "${long_query_time%%.*}" -ge $PREFERRED_QUERY_TIME ] ; then
cecho "Your long_query_time may be too high, I typically set this under $PREFERRED_QUERY_TIME sec." red
elif [ "${long_query_time/.}" -eq 0 ] ; then
cechon "Your long_query_time is set to "
cechon "zero" boldred
cechon ", which will cause "
cechon "ALL queries to be logged" red
cecho "!"
cecho "If you actually WANT to log all queries, use the query log, not the slow query log."
else
cecho "Your long_query_time seems reasonable." green
fi
}
check_binary_log () {
## -- Binary Log -- ##
cecho "BINARY UPDATE LOG" boldblue
mysql_variable \'log_bin\' log_bin
mysql_variable \'max_binlog_size\' max_binlog_size
mysql_variable \'expire_logs_days\' expire_logs_days
mysql_variable \'sync_binlog\' sync_binlog
# mysql_variable \'max_binlog_cache_size\' max_binlog_cache_size
if [ "$log_bin" = 'ON' ] ; then
cecho "The binary update log is enabled"
if [ -z "$max_binlog_size" ] ; then
cecho "The max_binlog_size is not set. The binary log will rotate when it reaches 1GB." red
fi
if [ "${expire_logs_days//.}" -eq 0 ] ; then # Turns 0.000 -> 0000.
cecho "The expire_logs_days is not set." boldred
cechon "The mysqld will retain the entire binary log until " red
cecho "RESET MASTER or PURGE MASTER LOGS commands are run manually" red
cecho "Setting expire_logs_days will allow you to remove old binary logs automatically" yellow
cecho "See http://dev.mysql.com/doc/refman/$major_version/en/purge-master-logs.html" yellow
fi
if [ "$sync_binlog" = 0 ] ; then
cecho "Binlog sync is not enabled, you could lose binlog records during a server crash" red
fi
else
cechon "The binary update log is "
cechon "NOT " boldred
cecho "enabled."
cecho "You will not be able to do point in time recovery" red
cecho "See http://dev.mysql.com/doc/refman/$major_version/en/point-in-time-recovery.html" yellow
fi
}
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_threads() {
## -- Worker Threads -- ##
cecho "WORKER THREADS" boldblue
mysql_status \'Threads_created\' threads_created1
sleep 1
mysql_status \'Threads_created\' threads_created2
mysql_status \'Threads_cached\' threads_cached
mysql_status \'Uptime\' uptime
mysql_variable \'thread_cache_size\' thread_cache_size
historic_threads_per_sec=$(($threads_created1/$uptime))
current_threads_per_sec=$(($threads_created2-$threads_created1))
cecho "Current thread_cache_size = $thread_cache_size"
cecho "Current threads_cached = $threads_cached"
cecho "Current threads_per_sec = $current_threads_per_sec"
cecho "Historic threads_per_sec = $historic_threads_per_sec"
if [ $historic_threads_per_sec -ge 2 ] && [ $threads_cached -le 1 ] ; then
cecho "Threads created per/sec are overrunning threads cached" red
cecho "You should raise thread_cache_size" red
elif [ $current_threads_per_sec -ge 2 ] ; then
cecho "Threads created per/sec are overrunning threads cached" red
cecho "You should raise thread_cache_size" red
else
cecho "Your thread_cache_size is fine" green
fi
}
check_key_buffer_size () {
## -- Key buffer Size -- ##
cecho "KEY BUFFER" boldblue
mysql_status \'Key_read_requests\' key_read_requests
mysql_status \'Key_reads\' key_reads
mysql_status \'Key_blocks_used\' key_blocks_used
mysql_status \'Key_blocks_unused\' key_blocks_unused
mysql_variable \'key_cache_block_size\' key_cache_block_size
mysql_variable \'key_buffer_size\' key_buffer_size
mysql_variable \'datadir\' datadir
mysql_variable \'version_compile_machine\' mysql_version_compile_machine
myisam_indexes=$($MYSQL_COMMAND -Bse "/*!50000 SELECT IFNULL(SUM(INDEX_LENGTH),0) from information_schema.TABLES where ENGINE='MyISAM' */")
if [ -z $myisam_indexes ] ; then
myisam_indexes=$(find $datadir -name '*.MYI' -exec du $duflags '{}' \; 2>&1 | awk '{ s += $1 } END { printf("%.0f\n", s )}')
fi
if [ $key_reads -eq 0 ] ; then
cecho "No key reads. If you aren't using MyISAM, this is normal. If you are" yellow
cecho "using MyISAM, this is very, very bad." yellow
key_cache_miss_rate=0
key_buffer_free=$(echo "$key_blocks_unused * $key_cache_block_size / $key_buffer_size * 100" | bc -l )
key_buffer_freeRND=$(echo "scale=0; $key_buffer_free / 1" | bc -l)
else
key_cache_miss_rate=$(($key_read_requests/$key_reads))
if [ ! -z $key_blocks_unused ] ; then
key_buffer_free=$(echo "$key_blocks_unused * $key_cache_block_size / $key_buffer_size * 100" | bc -l )
key_buffer_freeRND=$(echo "scale=0; $key_buffer_free / 1" | bc -l)
else
key_buffer_free='Unknown'
key_buffer_freeRND=75
fi
fi
human_readable $myisam_indexes myisam_indexesHR
cecho "Current MyISAM index space = $myisam_indexesHR $unit"
human_readable $key_buffer_size key_buffer_sizeHR
cecho "Current key_buffer_size = $key_buffer_sizeHR $unit"
cecho "Key cache miss rate is 1 : $key_cache_miss_rate"
cecho "Key buffer free ratio = $key_buffer_freeRND %"
if [ "$major_version" = '5.1' ] && [ $mysql_version_num -lt 050123 ] ; then
if [ $key_buffer_size -ge 4294967296 ] && ( echo "x86_64 ppc64 ia64 sparc64 i686" | grep -q $mysql_version_compile_machine ) ; then
cecho "Using key_buffer_size > 4GB will cause instability in versions prior to 5.1.23 " boldred
cecho "See Bug#5731, Bug#29419, Bug#29446" boldred
fi
fi
if [ "$major_version" = '5.0' ] && [ $mysql_version_num -lt 050052 ] ; then
if [ $key_buffer_size -ge 4294967296 ] && ( echo "x86_64 ppc64 ia64 sparc64 i686" | grep -q $mysql_version_compile_machine ) ; then
cecho "Using key_buffer_size > 4GB will cause instability in versions prior to 5.0.52 " boldred
cecho "See Bug#5731, Bug#29419, Bug#29446" boldred
fi
fi
if [ "$major_version" = '4.1' -o "$major_version" = '4.0' ] && [ $key_buffer_size -ge 4294967296 ] && ( echo "x86_64 ppc64 ia64 sparc64 i686" | grep -q $mysql_version_compile_machine ) ; then
cecho "Using key_buffer_size > 4GB will cause instability in versions prior to 5.0.52 " boldred
cecho "Reduce key_buffer_size to a safe value" boldred
cecho "See Bug#5731, Bug#29419, Bug#29446" boldred
fi
if [ $key_cache_miss_rate -le 100 ] && [ $key_cache_miss_rate -gt 0 ] && [ $key_buffer_freeRND -le 20 ]; then
cecho "You could increase key_buffer_size" boldred
cecho "It is safe to raise this up to 1/4 of total system memory;"
cecho "assuming this is a dedicated database server."
elif [ $key_buffer_freeRND -le 20 ] && [ $key_buffer_size -le $myisam_indexes ] ; then
cecho "You could increase key_buffer_size" boldred
cecho "It is safe to raise this up to 1/4 of total system memory;"
cecho "assuming this is a dedicated database server."
elif [ $key_cache_miss_rate -ge 10000 ] || [ $key_buffer_freeRND -le 50 ] ; then
cecho "Your key_buffer_size seems to be too high." red
cecho "Perhaps you can use these resources elsewhere" red
else
cecho "Your key_buffer_size seems to be fine" green
fi
}
check_query_cache () {
cecho "QUERY CACHE" boldblue
mysql_variable \'version\' mysql_version
mysql_variable \'query_cache_size\' query_cache_size
mysql_variable \'query_cache_limit\' query_cache_limit
mysql_variable \'query_cache_min_res_unit\' query_cache_min_res_unit
mysql_status \'Qcache_free_memory\' qcache_free_memory
mysql_status \'Qcache_total_blocks\' qcache_total_blocks
mysql_status \'Qcache_free_blocks\' qcache_free_blocks
mysql_status \'Qcache_lowmem_prunes\' qcache_lowmem_prunes
if [ -z $query_cache_size ] ; then
cecho "Your server does not support the query cache. That's probably a good thing." green
elif [ $query_cache_size -eq 0 ] ; then
cecho "Query cache is supported, but not enabled." yellow
cecho "Determine if enabling cache is advisable given your load characteristics," yellow
cecho "daemon version, and SMP (multiprocessor) status." black
else
qcache_used_memory=$(($query_cache_size-$qcache_free_memory))
qcache_mem_fill_ratio=$(echo "scale=2; $qcache_used_memory * 100 / $query_cache_size" | bc -l)
qcache_mem_fill_ratioHR=$(echo "scale=0; $qcache_mem_fill_ratio / 1" | bc -l)
cecho "You have query cache enabled. With many versions of the server, you may see" yellow
cecho "query cache lock contention, especially if you have more than one core." yellow
human_readable $query_cache_size query_cache_sizeHR
cecho "Current query_cache_size = $query_cache_sizeHR $unit"
human_readable $qcache_used_memory qcache_used_memoryHR
cecho "Current query_cache_used = $qcache_used_memoryHR $unit"
human_readable $query_cache_limit query_cache_limitHR
cecho "Current query_cache_limit = $query_cache_limitHR $unit"
cecho "Current Query cache Memory fill ratio = $qcache_mem_fill_ratio %"
if [ -z $query_cache_min_res_unit ] ; then
cecho "No query_cache_min_res_unit is defined. Using MySQL < 4.1 cache fragmentation can be inpredictable" yellow
else
human_readable $query_cache_min_res_unit query_cache_min_res_unitHR
cecho "Current query_cache_min_res_unit = $query_cache_min_res_unitHR $unit"
fi
if [ $qcache_free_blocks -gt 2 ] && [ $qcache_total_blocks -gt 0 ] ; then
qcache_percent_fragmented=$(echo "scale=2; $qcache_free_blocks * 100 / $qcache_total_blocks" | bc -l)
qcache_percent_fragmentedHR=$(echo "scale=0; $qcache_percent_fragmented / 1" | bc -l)
if [ $qcache_percent_fragmentedHR -gt 20 ] ; then
cecho "Query Cache is $qcache_percent_fragmentedHR % fragmented" red
cecho "Run \"FLUSH QUERY CACHE\" periodically to defragment the query cache memory" red
cecho "If you have many small queries lower 'query_cache_min_res_unit' to reduce fragmentation." red
fi
fi
if [ $qcache_mem_fill_ratioHR -le 25 ] ; then
cecho "Your query_cache_size seems to be too high." red
cecho "Perhaps you can use these resources elsewhere" red
fi
if [ $qcache_lowmem_prunes -ge 50 ] && [ $qcache_mem_fill_ratioHR -ge 80 ]; then
cechon "However, "
cechon "$qcache_lowmem_prunes " boldred
cecho "queries have been removed from the query cache due to lack of memory"
cecho "Perhaps you should raise query_cache_size" boldred
fi
cecho "MySQL won't cache query results that are larger than query_cache_limit in size" yellow
fi
}
check_sort_operations () {
## -- Sort Operations -- ##
cecho "SORT OPERATIONS" boldblue
mysql_status \'Sort_merge_passes\' sort_merge_passes
mysql_status \'Sort_scan\' sort_scan
mysql_status \'Sort_range\' sort_range
mysql_variable \'sort_buffer_size\' sort_buffer_size
mysql_variable \'read_rnd_buffer_size\' read_rnd_buffer_size
total_sorts=$(($sort_scan+$sort_range))
if [ -z $read_rnd_buffer_size ] ; then
mysql_variable \'record_buffer\' read_rnd_buffer_size
fi
## Correct for rounding error in mysqld where 512K != 524288 ##
sort_buffer_size=$(($sort_buffer_size+8))
read_rnd_buffer_size=$(($read_rnd_buffer_size+8))
human_readable $sort_buffer_size sort_buffer_sizeHR
cecho "Current sort_buffer_size = $sort_buffer_sizeHR $unit"
human_readable $read_rnd_buffer_size read_rnd_buffer_sizeHR
cechon "Current "
if [ "$major_version" = '3.23' ] ; then
cechon "record_rnd_buffer "
else
cechon "read_rnd_buffer_size "
fi
cecho "= $read_rnd_buffer_sizeHR $unit"
if [ $total_sorts -eq 0 ] ; then
cecho "No sort operations have been performed"
passes_per_sort=0
fi
if [ $sort_merge_passes -ne 0 ] ; then
passes_per_sort=$(($sort_merge_passes/$total_sorts))
else
passes_per_sort=0
fi
if [ $passes_per_sort -ge 2 ] ; then
cechon "On average "
cechon "$passes_per_sort " boldred
cecho "sort merge passes are made per sort operation"
cecho "You should raise your sort_buffer_size"
cechon "You should also raise your "
if [ "$major_version" = '3.23' ] ; then
cecho "record_rnd_buffer_size"
else
cecho "read_rnd_buffer_size"
fi
else
cecho "Sort buffer seems to be fine" green
fi
}
check_join_operations () {
## -- Joins -- ##
cecho "JOINS" boldblue
mysql_status \'Select_full_join\' select_full_join
mysql_status \'Select_range_check\' select_range_check
mysql_variable \'join_buffer_size\' join_buffer_size
## Some 4K is dropped from join_buffer_size adding it back to make sane ##
## handling of human-readable conversion ##
join_buffer_size=$(($join_buffer_size+4096))
human_readable $join_buffer_size join_buffer_sizeHR 2
cecho "Current join_buffer_size = $join_buffer_sizeHR $unit"
cecho "You have had $select_full_join queries where a join could not use an index properly"
if [ $select_range_check -eq 0 ] && [ $select_full_join -eq 0 ] ; then
cecho "Your joins seem to be using indexes properly" green
fi
if [ $select_full_join -gt 0 ] ; then
print_error='true'
raise_buffer='true'
fi
if [ $select_range_check -gt 0 ] ; then
cecho "You have had $select_range_check joins without keys that check for key usage after each row" red
print_error='true'
raise_buffer='true'
fi
## For Debuging ##
# print_error='true'
if [ $join_buffer_size -ge 4194304 ] ; then
cecho "join_buffer_size >= 4 M" boldred
cecho "This is not advised" boldred
raise_buffer=
fi
if [ $print_error ] ; then
if [ "$major_version" = '3.23' ] || [ "$major_version" = '4.0' ] ; then
cecho "You should enable \"log-long-format\" "
elif [ "$mysql_version_num" -gt 040100 ]; then
cecho "You should enable \"log-queries-not-using-indexes\""
fi
cecho "Then look for non indexed joins in the slow query log."
if [ $raise_buffer ] ; then
cecho "If you are unable to optimize your queries you may want to increase your"
cecho "join_buffer_size to accommodate larger joins in one pass."
printf "\n"
cecho "Note! This script will still suggest raising the join_buffer_size when" boldred
cecho "ANY joins not using indexes are found." boldred
fi
fi
# XXX Add better tests for join_buffer_size pending mysql bug #15088 XXX #
}
check_tmp_tables () {
## -- Temp Tables -- ##
cecho "TEMP TABLES" boldblue
mysql_status \'Created_tmp_tables\' created_tmp_tables
mysql_status \'Created_tmp_disk_tables\' created_tmp_disk_tables
mysql_variable \'tmp_table_size\' tmp_table_size
mysql_variable \'max_heap_table_size\' max_heap_table_size
if [ $created_tmp_tables -eq 0 ] ; then
tmp_disk_tables=0
else
tmp_disk_tables=$((created_tmp_disk_tables*100/(created_tmp_tables+created_tmp_disk_tables)))
fi
human_readable $max_heap_table_size max_heap_table_sizeHR
cecho "Current max_heap_table_size = $max_heap_table_sizeHR $unit"
human_readable $tmp_table_size tmp_table_sizeHR
cecho "Current tmp_table_size = $tmp_table_sizeHR $unit"
cecho "Of $created_tmp_tables temp tables, $tmp_disk_tables% were created on disk"
if [ $tmp_table_size -gt $max_heap_table_size ] ; then
cecho "Effective in-memory tmp_table_size is limited to max_heap_table_size." yellow
fi
if [ $tmp_disk_tables -ge 25 ] ; then
cecho "Perhaps you should increase your tmp_table_size and/or max_heap_table_size" boldred
cecho "to reduce the number of disk-based temporary tables" boldred
cecho "Note! BLOB and TEXT columns are not allowed in memory tables." yellow
cecho "If you are using these columns raising these values might not impact your " yellow
cecho "ratio of on disk temp tables." yellow
else
cecho "Created disk tmp tables ratio seems fine" green
fi
}
check_open_files () {
## -- Open Files Limit -- ##
cecho "OPEN FILES LIMIT" boldblue
mysql_variable \'open_files_limit\' open_files_limit
mysql_status \'Open_files\' open_files
if [ -z $open_files_limit ] || [ $open_files_limit -eq 0 ] ; then
open_files_limit=$(ulimit -n)
cant_override=1
else
cant_override=0
fi
cecho "Current open_files_limit = $open_files_limit files"