Optimize MySQL/MariaDB performance with MySQLTunner tool

MySQLTunner is a Perl tool that you’ll assist you and tuning your MariaDB/MySQL database server. It checks many parameters of the server, verifies user passwords, checks for anonymous users, insecure passwords(where for example the password is the username), checks for known CVE vulnerabilities etc.

The first step in running the tool is to download the script file from the official website https://github.com/major/MySQLTuner-perl

wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl
wget https://github.com/major/MySQLTuner-perl/raw/master/vulnerabilities.csv

The basic command for running the script is:

perl mysqltuner.pl

To also check for vulnerabilities, you must download the vulnerabilities.csv file and execute the command:

perl mysqltuner.pl --cvefile=vulnerabilities.csv

An example of the output from a test server:

[root@web /]# perl mysqltuner.pl --cvefile=vulnerabilities.csv

 >>  MySQLTuner 1.8.7 - Major Hayden <major@mhtx.net>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.pl/
 >>  Run with '--help' for additional options and output filtering

[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 10.6.5-MariaDB
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[OK] Log file /var/log/mysqld.log exists
[--] Log file: /var/log/mysqld.log(69K)
[OK] Log file /var/log/mysqld.log is not empty
[OK] Log file /var/log/mysqld.log is smaller than 32 Mb
[OK] Log file /var/log/mysqld.log is readable.
[!!] /var/log/mysqld.log contains 394 warning(s).
[!!] /var/log/mysqld.log contains 49 error(s).
[--] 22 start(s) detected in /var/log/mysqld.log
[--] 1) 2022-01-08 16:04:41 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 2) 2022-01-04 12:20:12 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 3) 2021-12-27 17:21:20 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 4) 2021-12-11 15:39:01 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 5) 2021-12-11 14:47:28 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 6) 2021-12-11 14:47:22 0 [Note] /usr/sbin/mysqld: ready for connections.
-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Data in Aria tables: 32.0K (Tables: 1)
[--] Data in MEMORY tables: 0B (Tables: 40)
[--] Data in MyISAM tables: 1.3G (Tables: 34)
[--] Data in InnoDB tables: 217.2M (Tables: 31)
[OK] Total fragmented tables: 0

-------- Analysis Performance Metrics --------------------------------------------------------------
[--] innodb_stats_on_metadata: OFF
[OK] No stat updates during querying INFORMATION_SCHEMA.

-------- Views Metrics -----------------------------------------------------------------------------

-------- Triggers Metrics --------------------------------------------------------------------------

-------- Routines Metrics --------------------------------------------------------------------------

-------- Security Recommendations ------------------------------------------------------------------
[OK] All database users have passwords assigned
-------- CVE Security Recommendations --------------------------------------------------------------

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 4d 19h 31m 2s (18M q [45.289 qps], 550K conn, TX: 53G, RX: 3G)
[--] Reads / Writes: 90% / 10%
[--] Binary logging is disabled
[--] Physical Memory     : 4.0G
[--] Max MySQL memory    : 38.3G
[--] Other process memory: 0B
[--] Total buffers: 305.0M global + 259.7M per thread (150 max threads)
[--] P_S Max memory usage: 0B
[--] Galera GCache Max memory usage: 0B
[OK] Slow queries: 0% (33/18M)
[OK] Highest usage of available connections: 76% (114/150)
[OK] Aborted connections: 0.02%  (86/550353)
[!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performance
[OK] Query cache is disabled by default due to mutex contention on multiprocessor machines.
[OK] Sorts requiring temporary tables: 0% (227 temp sorts / 3M sorts)
[!!] Joins performed without indexes: 197183
[!!] Temporary tables created on disk: 56% (990K on disk / 1M total)
[OK] Thread cache hit rate: 99% (645 created / 550K connections)
[OK] Table cache hit rate: 95% (22M hits / 24M requests)
[OK] Open file limit used: 8% (1K/16K)
[OK] Table locks acquired immediately: 99% (17M immediate / 17M locks)

-------- Performance schema ------------------------------------------------------------------------
[--] Performance schema is disabled.
[--] Memory used by P_S: 0B
[--] Sys schema is installed.

-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is enabled.
[--] Thread Pool Size: 2 thread(s).
[--] Using default value is good enough for your version (10.6.5-MariaDB)

-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 61.8% (9.9M used / 16.0M cache)
[OK] Key buffer size / total MyISAM indexes: 16.0M/152.4M
[OK] Read Key buffer hit rate: 100.0% (768M cached / 379K reads)
[!!] Write Key buffer hit rate: 54.4% (3M cached / 1M writes)

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[OK] InnoDB File per table is activated
[!!] InnoDB buffer pool / data size: 128.0M/217.2M
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (75 %): 96.0M * 1/128.0M should be equal to 25%
[--] Number of InnoDB Buffer Pool Chunk : 1 for 1 Buffer Pool Instance(s)
[OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances
[OK] InnoDB Read buffer efficiency: 99.98% (499634018 hits/ 499732161 total)
[!!] InnoDB Write Log efficiency: 270.94% (232636 hits/ 85863 total)
[OK] InnoDB log waits: 0.00% (0 waits / 318499 writes)

-------- Aria Metrics ------------------------------------------------------------------------------
[--] Aria Storage Engine is enabled.
[OK] Aria pagecache size / total Aria indexes: 128.0M/712.0K
[OK] Aria pagecache hit rate: 95.4% (18M cached / 860K reads)

-------- TokuDB Metrics ----------------------------------------------------------------------------
[--] TokuDB is disabled.

-------- XtraDB Metrics ----------------------------------------------------------------------------
[--] XtraDB is disabled.

-------- Galera Metrics ----------------------------------------------------------------------------
[--] Galera is disabled.

-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] Binlog format: MIXED
[--] XA support enabled: ON
[--] Semi synchronous replication Master: OFF
[--] Semi synchronous replication Slave: OFF
[--] This is a standalone server

-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    Check warning line(s) in /var/log/mysqld.log file
    Check error line(s) in /var/log/mysqld.log file
    Reduce your overall MySQL memory footprint for system stability
    Dedicate this server to your database for highest performance.
    Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1
    We will suggest raising the 'join_buffer_size' until JOINs not using indexes are found.
             See https://dev.mysql.com/doc/internals/en/join-buffer-size.html
             (specially the conclusions at the bottom of the page).
    When making adjustments, make tmp_table_size/max_heap_table_size equal
    Reduce your SELECT DISTINCT queries which have no LIMIT clause
    Performance schema should be activated for better diagnostics
    Before changing innodb_log_file_size and/or innodb_log_files_in_group read this: https://bit.ly/2TcGgtU
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    join_buffer_size (> 1.0M, or always use indexes with JOINs)
    tmp_table_size (> 16M)
    max_heap_table_size (> 16M)
    performance_schema = ON enable PFS
    key_buffer_size (~ 10M)

Check carefully the Recommendations section and take the necessary actions. The MySQL/MariaDB options are stored in /etc/my.cnf file. You can edit this file with an editor like vi or nano:

nano /etc/my.cnf

In the end, save the file and restart the MySQL server:

service mysqld restart

Leave a Reply