-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmysqltuner.pl.tdy
More file actions
executable file
·10079 lines (9162 loc) · 341 KB
/
mysqltuner.pl.tdy
File metadata and controls
executable file
·10079 lines (9162 loc) · 341 KB
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
#!/usr/bin/env perl
# mysqltuner.pl - Version 2.8.37
# High Performance MySQL Tuning Script
# Copyright (C) 2015-2026 Jean-Marie Renouard - [email protected]
# Copyright (C) 2006-2026 Major Hayden - [email protected]
# For the latest updates, please visit http://mysqltuner.pl/
# Git repository available at https://github.com/jmrenouard/MySQLTuner-perl/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# This project would not be possible without help from:
# Matthew Montgomery Paul Kehrer Dave Burgess
# Jonathan Hinds Mike Jackson Nils Breunese
# Shawn Ashlee Luuk Vosslamber Ville Skytta
# Trent Hornibrook Jason Gill Mark Imbriaco
# Greg Eden Aubin Galinotti Giovanni Bechis
# Bill Bradford Ryan Novosielski Michael Scheidell
# Blair Christensen Hans du Plooy Victor Trac
# Everett Barnes Tom Krouper Gary Barrueto
# Simon Greenaway Adam Stein Isart Montane
# Baptiste M. Cole Turner Major Hayden
# Joe Ashcraft Jean-Marie Renouard Christian Loos
# Julien Francoz Daniel Black Long Radix
#
# Inspired by Matthew Montgomery's tuning-primer.sh script:
# http://www.day32.com/MySQL/
#
package main;
use 5.005;
use strict;
use warnings;
use diagnostics;
use POSIX;
use File::Spec;
use Getopt::Long;
use Pod::Usage;
use Sys::Hostname;
use File::Basename;
use Cwd 'abs_path';
# Subroutine declarations
sub show_help;
sub subheaderprint;
sub execute_system_command;
#use Data::Dumper;
#$Data::Dumper::Pair = " : ";
# for which()
#use Env;
our $is_win = $^O eq 'MSWin32';
# Set up a few variables for use in the script
our $tunerversion = "2.8.37";
our ( @adjvars, @generalrec, @modeling, @sysrec, @secrec );
# Set defaults
# Central metadata for CLI options
# Categories: CONNECTION, PERFORMANCE, OUTPUT, CLOUD, MISC
our %CLI_METADATA = (
# Connection and Authentication
'host' => {
type => '=s',
default => '0',
desc => 'Connect to a remote host to perform tests',
placeholder => '<host>',
cat => 'CONNECTION'
},
'socket' => {
type => '=s',
default => '0',
desc => 'Use a different socket for a local connection',
placeholder => '<path>',
cat => 'CONNECTION'
},
'pipe' => {
type => '!',
default => 0,
desc => 'Connect to a local Windows database using named pipes',
cat => 'CONNECTION'
},
'pipe_name' => {
type => '=s',
default => '0',
desc => 'Use a different pipe name for a local connection',
placeholder => '<name>',
cat => 'CONNECTION'
},
'port' => {
type => '=i',
default => 3306,
desc => 'Port to use for connection',
placeholder => '<port>',
cat => 'CONNECTION',
validate => qr/^\d+$/
},
'user|u' => {
type => '=s',
default => '0',
desc => 'Username to use for authentication',
placeholder => '<user>',
cat => 'CONNECTION'
},
'pass|p|password' => {
type => '=s',
default => '0',
desc => 'Password to use for authentication',
placeholder => '<pass>',
cat => 'CONNECTION'
},
'userenv' => {
type => '=s',
default => '0',
desc => 'Env variable name for username',
placeholder => '<envvar>',
cat => 'CONNECTION'
},
'passenv' => {
type => '=s',
default => '0',
desc => 'Env variable name for password',
placeholder => '<envvar>',
cat => 'CONNECTION'
},
'ssl-ca' => {
type => '=s',
default => '0',
desc => 'Path to public key (SSL CA)',
placeholder => '<path>',
cat => 'CONNECTION'
},
'mysqladmin' => {
type => '=s',
default => '0',
desc => 'Path to a custom mysqladmin executable',
placeholder => '<path>',
cat => 'CONNECTION'
},
'mysqlcmd' => {
type => '=s',
default => '0',
desc => 'Path to a custom mysql executable',
placeholder => '<path>',
cat => 'CONNECTION'
},
'defaults-file' => {
type => '=s',
default => '0',
desc => 'Path to a custom .my.cnf',
placeholder => '<path>',
cat => 'CONNECTION'
},
'defaults-extra-file' => {
type => '=s',
default => '0',
desc => 'Path to an extra custom config file',
placeholder => '<path>',
cat => 'CONNECTION'
},
'protocol' => {
type => '=s',
default => '0',
desc => 'Force TCP connection instead of socket',
placeholder => 'tcp',
cat => 'CONNECTION'
},
'server-log' => {
type => '=s',
default => '0',
desc => 'Path to explicit log file (error_log)',
placeholder => '<path>',
cat => 'CONNECTION'
},
# Performance and Reporting
'skipsize' => {
type => '!',
default => 0,
desc => "Don't enumerate tables and their sizes",
cat => 'PERFORMANCE'
},
'checkversion' => {
type => '!',
default => 1,
desc => 'Check for updates to MySQLTuner',
cat => 'PERFORMANCE'
},
'updateversion' => {
type => '!',
default => 0,
desc => 'Update MySQLTuner if newer version is available',
cat => 'PERFORMANCE'
},
'forcemem' => {
type => '=i',
default => 0,
desc => 'Amount of RAM installed in megabytes',
placeholder => '<size>',
cat => 'PERFORMANCE',
validate => qr/^\d+$/
},
'forceswap' => {
type => '=i',
default => 0,
desc => 'Amount of swap memory configured in megabytes',
placeholder => '<size>',
cat => 'PERFORMANCE',
validate => qr/^\d+$/
},
'buffers' => {
type => '!',
default => 0,
desc => 'Print global and per-thread buffer values',
cat => 'PERFORMANCE'
},
'passwordfile' => {
type => '=s',
default => '0',
desc => 'Path to a password file list',
placeholder => '<path>',
cat => 'PERFORMANCE'
},
'cvefile' => {
type => '=s',
default => '0',
desc => 'CVE File for vulnerability checks',
placeholder => '<path>',
cat => 'PERFORMANCE'
},
'outputfile' => {
type => '=s',
default => '0',
desc => 'Path to a output txt file',
placeholder => '<path>',
cat => 'PERFORMANCE'
},
'reportfile' => {
type => '=s',
default => '0',
desc => 'Path to a report txt file',
placeholder => '<path>',
cat => 'PERFORMANCE'
},
'template' => {
type => '=s',
default => '0',
desc => 'Path to a template file',
placeholder => '<path>',
cat => 'PERFORMANCE'
},
'json' => {
type => '!',
default => 0,
desc => 'Print result as JSON string',
cat => 'PERFORMANCE'
},
'prettyjson' => {
type => '!',
default => 0,
desc => 'Print result as JSON formatted string',
cat => 'PERFORMANCE'
},
'dumpdir' => {
type => '=s',
default => '0',
desc => 'Path to a directory where to dump information files',
placeholder => '<path>',
cat => 'PERFORMANCE'
},
'schemadir' => {
type => '=s',
default => '0',
desc =>
'Path to a directory where to dump one markdown file per schema',
placeholder => '<path>',
cat => 'PERFORMANCE'
},
'feature' => {
type => '=s',
default => '0',
desc => 'Run a specific feature',
placeholder => '<feature>',
cat => 'PERFORMANCE',
implies => { verbose => 1 }
},
'skippassword' => {
type => '!',
default => 0,
desc => "Don't perform checks on user passwords",
cat => 'PERFORMANCE'
},
# Output Options
'silent' => {
type => '!',
default => 0,
desc => "Don't output anything on screen",
cat => 'OUTPUT'
},
'verbose|v' => {
type => '!',
default => 0,
desc => 'Print out all options',
cat => 'OUTPUT',
implies => {
dbstat => 1,
tbstat => 1,
idxstat => 1,
sysstat => 1,
buffers => 1,
pfstat => 1,
structstat => 1,
myisamstat => 1,
plugininfo => 1
}
},
'color!' => {
type => '!',
default => ( -t STDOUT ? 1 : 0 ),
desc => 'Print output in color',
cat => 'OUTPUT'
},
'nobad' => {
type => '!',
default => 0,
desc => 'Remove negative/suggestion responses',
cat => 'OUTPUT'
},
'nogood' => {
type => '!',
default => 0,
desc => 'Remove OK responses',
cat => 'OUTPUT'
},
'noinfo' => {
type => '!',
default => 0,
desc => 'Remove informational responses',
cat => 'OUTPUT'
},
'debug' => {
type => '!',
default => 0,
desc => 'Print debug information',
cat => 'OUTPUT'
},
'dbgpattern' => {
type => '=s',
default => '',
desc => 'Debug pattern (regex)',
placeholder => '<regex>',
cat => 'OUTPUT'
},
'noprettyicon' => {
type => '!',
default => 0,
desc => 'Print output with legacy tags',
cat => 'OUTPUT'
},
'experimental' => {
type => '!',
default => 0,
desc => 'Print experimental analysis',
cat => 'OUTPUT'
},
'nondedicated' => {
type => '!',
default => 0,
desc => 'Consider server is not dedicated to DB',
cat => 'OUTPUT'
},
'noprocess' => {
type => '!',
default => 0,
desc => 'Consider no other process is running',
cat => 'OUTPUT'
},
# Stats / Reporting flags
'dbstat!' => {
type => '!',
default => 0,
desc => 'Print database information',
cat => 'OUTPUT'
},
'tbstat!' => {
type => '!',
default => 0,
desc => 'Print table information',
cat => 'OUTPUT'
},
'colstat!' => {
type => '!',
default => 0,
desc => 'Print column information',
cat => 'OUTPUT'
},
'idxstat!' => {
type => '!',
default => 0,
desc => 'Print index information',
cat => 'OUTPUT'
},
'sysstat!' => {
type => '!',
default => 0,
desc => 'Print system stats',
cat => 'OUTPUT'
},
'pfstat!' => {
type => '!',
default => 0,
desc => 'Print Performance schema info',
cat => 'OUTPUT'
},
'plugininfo!' => {
type => '!',
default => 0,
desc => 'Print plugin information',
cat => 'OUTPUT'
},
'myisamstat!' => {
type => '!',
default => 0,
desc => 'Print MyISAM stats',
cat => 'OUTPUT'
},
'structstat!' => {
type => '!',
default => 0,
desc => 'Print table structures',
cat => 'OUTPUT'
},
# Cloud and Containers
'cloud' => {
type => '!',
default => 0,
desc => 'Enable cloud mode',
cat => 'CLOUD'
},
'azure' => {
type => '!',
default => 0,
desc => 'Enable Azure-specific support',
cat => 'CLOUD'
},
'ssh-host' => {
type => '=s',
default => '0',
desc => 'The SSH host for cloud connections',
placeholder => '<host>',
cat => 'CLOUD'
},
'ssh-user' => {
type => '=s',
default => '0',
desc => 'The SSH user for cloud connections',
placeholder => '<user>',
cat => 'CLOUD'
},
'ssh-password' => {
type => '=s',
default => '0',
desc => 'The SSH password for cloud connections',
placeholder => '<pass>',
cat => 'CLOUD'
},
'ssh-identity-file' => {
type => '=s',
default => '0',
desc => 'The path to the SSH identity file',
placeholder => '<path>',
cat => 'CLOUD'
},
'container' => {
type => '=s',
default => '0',
desc => 'Enable container mode with ID or name',
placeholder => '<id>',
cat => 'CLOUD'
},
'server-log' => {
type => '=s',
default => '0',
desc => 'Path to explicit log file (error_log)',
placeholder => '<path>',
cat => 'PERFORMANCE'
},
# Misc
'max-password-checks' => {
type => '=i',
default => 100,
desc => 'Max password checks from dictionary',
placeholder => '<n>',
cat => 'MISC'
},
'ignore-tables' => {
type => '=s',
default => '0',
desc => 'Tables to ignore (comma separated)',
placeholder => '<t>',
cat => 'MISC'
},
'bannedports' => {
type => '=s',
default => '0',
desc => 'Ports banned separated by comma',
placeholder => '<p>',
cat => 'MISC'
},
'maxportallowed' => {
type => '=i',
default => 0,
desc => 'Number of open ports allowable',
placeholder => '<n>',
cat => 'MISC'
},
'defaultarch' => {
type => '=i',
default => 64,
desc => 'Default architecture (32 or 64)',
placeholder => '<32|64>',
cat => 'MISC',
validate => sub { $_[0] == 32 || $_[0] == 64 }
},
'noask' => {
type => '!',
default => 0,
desc => "Don't ask for confirmation",
cat => 'MISC'
},
'help|?' => {
type => '',
default => 0,
desc => 'Show this help message',
cat => 'MISC'
},
);
# Initialize %opt from metadata
our %opt = map {
my ($primary) = split /\|/, $_;
$primary =~ s/[!+=:].*$//; # Strip modifiers (Getopt::Long compatibility)
$primary => $CLI_METADATA{$_}->{default}
} keys %CLI_METADATA;
# Declare shared variables at top level
our (
$devnull, $basic_password_files, $outputfile,
$fh, $me, $good,
$bad, $info, $deb,
$cmd, $end, $maxlines,
$mysqlvermajor, $mysqlverminor, $mysqlvermicro,
@banned_ports, @dblist, %result
);
# Gather the options from the command line
sub parse_cli_args {
# Build GetOptions arguments dynamically
my @getopt_args;
Getopt::Long::Configure( "no_auto_abbrev", "no_ignore_case" );
foreach my $opt_spec ( sort keys %CLI_METADATA ) {
my $type = $CLI_METADATA{$opt_spec}->{type} // '';
my ($primary) = split /\|/, $opt_spec;
$primary =~ s/[!+=:].*$//; # Strip modifiers
my $final_spec = $opt_spec;
# Only append type if it's not already part of the specification key
if ( $type && $opt_spec !~ /\Q$type\E$/ ) {
$final_spec .= $type;
}
push @getopt_args, $final_spec => \$opt{$primary};
}
GetOptions(@getopt_args)
or pod2usage(
-exitval => 1,
-verbose => 99,
-sections =>
[ "NAME", "IMPORTANT USAGE GUIDELINES", "OPTIONS", "VERSION" ]
);
# Apply metadata-driven rules (Implications and Validation)
foreach my $opt_spec ( keys %CLI_METADATA ) {
my ($primary) = split /\|/, $opt_spec;
my $meta = $CLI_METADATA{$opt_spec};
# Implications (e.g., --feature implies --verbose)
if ( ( $opt{$primary} // '0' ) ne '0' && $meta->{implies} ) {
while ( my ( $target, $value ) = each %{ $meta->{implies} } ) {
$opt{$target} = $value;
}
}
# Validation (regex or coderef)
if ( defined $opt{$primary}
&& ( $opt{$primary} // '0' ) ne '0'
&& $meta->{validate} )
{
my $val = $opt{$primary};
my $is_valid = 1;
if ( ref $meta->{validate} eq 'Regexp' ) {
$is_valid = ( $val =~ $meta->{validate} );
}
elsif ( ref $meta->{validate} eq 'CODE' ) {
$is_valid = $meta->{validate}->($val);
}
unless ($is_valid) {
print "Error: Invalid value for --$primary: $val\n";
exit 1;
}
}
}
}
sub setup_environment {
if ( defined $opt{'help'} && $opt{'help'} == 1 ) {
show_help();
}
if ( defined $opt{'version'} && $opt{'version'} == 1 ) {
subheaderprint("MySQLTuner $tunerversion");
exit(0);
}
$devnull = File::Spec->devnull();
$basic_password_files =
( $opt{passwordfile} eq "0" )
? abs_path( dirname(__FILE__) ) . "/basic_passwords.txt"
: abs_path( $opt{passwordfile} );
# Username from envvar
if ( exists $opt{userenv} && exists $ENV{ $opt{userenv} // '' } ) {
$opt{user} = $ENV{ $opt{userenv} // '' };
}
# Related to password option
if ( exists $opt{passenv} && exists $ENV{ $opt{userenv} // '' } ) {
$opt{pass} = $ENV{ $opt{userenv} // '' };
}
$opt{pass} = $opt{password}
if ( ( $opt{pass} // '0' ) eq '0' and ( $opt{password} // '0' ) ne '0' );
# for RPM distributions
$basic_password_files = "/usr/share/mysqltuner/basic_passwords.txt"
unless -f "$basic_password_files";
$opt{dbgpattern} = '.*' if ( ( $opt{dbgpattern} // '' ) eq '' );
# check if we need to enable verbose mode
$opt{noprettyicon} = 0 if ( $opt{noprettyicon} // 0 ) != 1;
$opt{nocolor} = 1 if defined( $opt{outputfile} );
$opt{noprocess} = 0
if ( ( $opt{noprocess} // 0 ) == 1 ); # Don't print process information
$opt{structstat} = 0
if ( ( $opt{nostructstat} // 0 ) == 1 )
; # Don't print table struct information
$opt{myisamstat} = 1
if ( not defined( $opt{myisamstat} ) );
$opt{myisamstat} = 0
if ( ( $opt{nomyisamstat} // 0 ) == 1 )
; # Don't print MyISAM table information
# Handle cvefile if it was not passed but exists locally
$opt{cvefile} = './vulnerabilities.csv'
if ( ( !defined( $opt{cvefile} ) || $opt{cvefile} eq '' )
&& -f './vulnerabilities.csv' );
$opt{'bannedports'} = '' unless defined( $opt{'bannedports'} );
@banned_ports = split ',', $opt{'bannedports'};
$outputfile = undef;
$outputfile = abs_path( $opt{outputfile} ) unless $opt{outputfile} eq "0";
$fh = undef;
open( $fh, '>', $outputfile )
or die("Fail opening $outputfile")
if defined($outputfile);
$opt{nocolor} = 1 if defined($outputfile);
$opt{nocolor} = 1 unless ( -t STDOUT );
$opt{nocolor} = 0 if ( ( $opt{color} // 0 ) == 1 );
# Setting up the colors for the print styles
$me = ( getpwuid($<) )[0] // $ENV{USER} // $ENV{USERNAME} // 'unknown';
if ($is_win) { $opt{nocolor} = 1; }
$good = ( $opt{nocolor} == 0 ) ? "[\e[0;32mOK\e[0m]" : "[OK]";
$bad = ( $opt{nocolor} == 0 ) ? "[\e[0;31m!!\e[0m]" : "[!!]";
$info = ( $opt{nocolor} == 0 ) ? "[\e[0;34m--\e[0m]" : "[--]";
$deb = ( $opt{nocolor} == 0 ) ? "[\e[0;31mDG\e[0m]" : "[DG]";
$cmd = ( $opt{nocolor} == 0 ) ? "\e[1;32m[CMD]($me)" : "[CMD]($me)";
$end = ( $opt{nocolor} == 0 ) ? "\e[0m" : "";
if ( ( not $is_win ) and ( $opt{noprettyicon} == 0 ) ) {
$good = ( $opt{nocolor} == 0 ) ? "\e[0;32m✔\e[0m " : "✔ ";
$bad = ( $opt{nocolor} == 0 ) ? "\e[0;31m✘\e[0m " : "✘ ";
$info = ( $opt{nocolor} == 0 ) ? "\e[0;34mℹ\e[0m " : "ℹ ";
$deb = ( $opt{nocolor} == 0 ) ? "\e[0;31m⚙\e[0m " : "⚙ ";
$cmd = ( $opt{nocolor} == 0 ) ? "\e[1;32m⌨️($me)" : "⌨️($me)";
$end = ( $opt{nocolor} == 0 ) ? "\e[0m " : " ";
}
# Maximum lines of log output to read from end
$maxlines = 30000;
# Super structure containing all information
%result = ();
$result{'MySQLTuner'}{'version'} = $tunerversion;
$result{'MySQLTuner'}{'datetime'} = scalar localtime;
$result{'MySQLTuner'}{'options'} = \%opt;
}
# Functions that handle the print styles
sub show_help {
my %categories = (
'CONNECTION' => 'CONNECTION AND AUTHENTICATION',
'CLOUD' => 'CLOUD SUPPORT',
'PERFORMANCE' => 'PERFORMANCE AND REPORTING OPTIONS',
'OUTPUT' => 'OUTPUT OPTIONS',
'MISC' => 'MISCELLANEOUS OPTIONS',
);
print "MySQLTuner $tunerversion - MySQL High Performance Tuning Script\n\n";
print "Usage: ./mysqltuner.pl [options]\n\n";
foreach my $cat (qw(CONNECTION CLOUD PERFORMANCE OUTPUT MISC)) {
print "$categories{$cat}:\n";
foreach my $opt_spec ( sort keys %CLI_METADATA ) {
next unless $CLI_METADATA{$opt_spec}->{cat} eq $cat;
my $meta = $CLI_METADATA{$opt_spec};
my ($primary) = split /\|/, $opt_spec;
my $display = "--$primary";
# Handle negatable options (trailing !)
my $is_negatable =
( $opt_spec =~ /!$/ || ( $meta->{type} // '' ) eq '!' );
if ($is_negatable) {
# Remove ! from display if present
$display =~ s/!$//;
}
$display .= " " . $meta->{placeholder} if $meta->{placeholder};
# Handle aliases
my @parts = split /\|/, $opt_spec;
shift @parts; # remove primary
my @display_parts;
if ($is_negatable) {
push @display_parts, "no-$primary";
}
push @display_parts, @parts;
if (@display_parts) {
# Format aliases: length 1 -> -x, length > 1 -> --xx
$display .= " (" . join(
", ",
map {
my $p = $_;
$p =~ s/!$//;
length($p) == 1 ? "-$p" : "--$p"
} @display_parts
) . ")";
}
printf " %-32s %s", $display, $meta->{desc};
# Special case for defaults: Don't print if 0 or empty string unless meaningful
# For booleans (type !), default 0 is expected and silent.
if ( defined $meta->{default}
&& $meta->{default} ne '0'
&& $meta->{default} ne ''
&& $meta->{default} ne "0" )
{
# For string/numeric defaults, use ne and != appropriately or just string compare since it's for display
print " (default: $meta->{default})";
}
print "\n";
}
print "\n";
}
exit 0;
}
sub prettyprint {
print $_[0] . "\n" unless ( $opt{'silent'} or $opt{'json'} );
print $fh $_[0] . "\n" if defined($fh);
}
sub goodprint {
prettyprint $good. " " . $_[0] unless ( $opt{nogood} == 1 );
}
sub infoprint {
prettyprint $info. " " . $_[0] unless ( $opt{noinfo} == 1 );
}
sub badprint {
prettyprint $bad. " " . $_[0] unless ( $opt{nobad} == 1 );
}
sub debugprint {
prettyprint $deb. " " . $_[0] unless ( $opt{debug} == 0 );
}
sub redwrap {
return ( $opt{nocolor} == 0 ) ? "\e[0;31m" . $_[0] . "\e[0m" : $_[0];
}
sub greenwrap {
return ( $opt{nocolor} == 0 ) ? "\e[0;32m" . $_[0] . "\e[0m" : $_[0];
}
sub cmdprint {
prettyprint $cmd. " " . $_[0] . $end;
}
sub push_recommendation {
my ( $cat, $msg ) = @_;
push @generalrec, $msg;
push @sysrec, $msg if $cat =~ /sys/i;
push @secrec, $msg if $cat =~ /sec/i;
push @modeling, $msg if $cat =~ /mod/i;
}
sub infoprintml {
for my $ln (@_) { $ln =~ s/\n//g; infoprint "\t$ln"; }
}
sub infoprintcmd {
cmdprint "@_";
infoprintml grep { $_ ne '' and $_ !~ /^\s*$/ } `@_ 2>&1`;
}
sub subheaderprint {
my $tln = 100;
my $sln = 8;
my $ln = length("@_") + 2;
prettyprint " ";
prettyprint "-" x $sln . " @_ " . "-" x ( $tln - $ln - $sln );
}
sub infoprinthcmd {
subheaderprint "$_[0]";
infoprintcmd "$_[1]";
}
sub is_remote() {
my $host = $opt{'host'};
return 1 if ( $opt{'cloud'} && ( $opt{'ssh-host'} // '0' ) ne '0' );
return 0 if ( ( $host // '0' ) eq '0' );
return 0 if ( $host eq 'localhost' );
return 0 if ( $host eq '127.0.0.1' );
return 1;
}
sub is_docker() {
return 1 if -f '/.dockerenv';
if ( -f '/proc/self/cgroup' ) {
if ( open( my $fh, '<', '/proc/self/cgroup' ) ) {
while ( my $line = <$fh> ) {
if ( $line =~ /docker|kubepods|containerd|podman/ ) {
close $fh;
return 1;
}
}
close $fh;
}
}
return 1
if (
(
defined $ENV{'container'}
&& $ENV{'container'} =~ /^(docker|podman|lxc)$/
)
|| ( defined $opt{'container'} && ( $opt{'container'} // '0' ) ne '0' )
);
return 0;
}
sub is_int {
return 0 unless defined $_[0];
my $str = $_[0];
#trim whitespace both sides
$str =~ s/^\s+|\s+$//g;
#Alternatively, to match any float-like numeric, use:
# m/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
#flatten to string and match dash or plus and one or more digits
if ( $str =~ /^(\-|\+)?\d+?$/ ) {
return 1;
}
return 0;
}
# Calculates the number of physical cores
sub cpu_cores {
if ( $^O eq 'linux' ) {
if ( get_transport_prefix() eq '' ) {
my %cpus;
my %cores;
if ( open( my $proc_cpuinfo, '<', '/proc/cpuinfo' ) ) {
while (<$proc_cpuinfo>) {
if ( /^physical id\s*:\s*(.*)/ ) { $cpus{$1} = 1; }
if ( /^core id\s*:\s*(.*)/ ) { $cores{$1} = 1; }
}
close $proc_cpuinfo;
my $cntCPU = ( scalar keys %cpus ) * ( scalar keys %cores );
return $cntCPU if $cntCPU > 0;
}
}
my $cntCPU =
execute_system_command(
"awk -F: '/^core id/ && !P[\$2] { CORES++; P[\$2]=1 }; /^physical id/ && !N[\$2] { CPUs++; N[\$2]=1 }; END { print CPUs*CORES }' /proc/cpuinfo"
);
chomp $cntCPU;
return ( $cntCPU == 0 ? execute_system_command("nproc") : $cntCPU ) + 0;
}
if ( $^O eq 'freebsd' ) {
my $cntCPU = execute_system_command("sysctl -n kern.smp.cores");
chomp $cntCPU;
return $cntCPU + 0;
}
if ($is_win) {
my $cntCPU =
execute_system_command(
'wmic cpu get NumberOfCores| perl -ne "s/[^0-9]//g; print if /[0-9]+/;"'
);
chomp $cntCPU;
return $cntCPU + 0;
}
return 0;
}
# Calculates the number of logical cores (including HT)
sub logical_cpu_cores {
if ( $^O eq 'linux' ) {
if ( get_transport_prefix() eq '' ) {
my $cntCPU = 0;
if ( open( my $proc_cpuinfo, '<', '/proc/cpuinfo' ) ) {
while (<$proc_cpuinfo>) {
$cntCPU++ if /^processor\s*:\s*\d+/;
}
close $proc_cpuinfo;
return $cntCPU if $cntCPU > 0;
}
}
my $cntCPU = execute_system_command("grep -c ^processor /proc/cpuinfo");
chomp $cntCPU;
if ( $cntCPU == 0 ) {
$cntCPU = execute_system_command("nproc");
chomp $cntCPU;
}
return $cntCPU + 0;
}
if ( $^O eq 'freebsd' ) {
my $cntCPU = execute_system_command("sysctl -n kern.smp.cpus");
chomp $cntCPU;
return $cntCPU + 0;
}
if ($is_win) {
my $cntCPU =
execute_system_command(
'wmic cpu get NumberOfLogicalProcessors| perl -ne "s/[^0-9]//g; print if /[0-9]+/;"'
);
chomp $cntCPU;
return $cntCPU + 0;
}
return cpu_cores();
}
# Calculates the parameter passed in bytes, then rounds it to one decimal place