MySQL Database Performance Optimization Using MySQLTuner on Ubuntu 18.04 LTS

Databases are generally considered as the main source of performance bottlenecks in most applications. We usually use tricks like caching to get around them.

Here, we will be looking at a tool to help fine-tune a MySQL database to thereby improving the speed and general performance. We will be using a tool called MySQLTuner

MySQLTuner is an open source tool written in Perl which helps you increase the performance of your MySQL database server by analyzing all the configurations and providing recommendations.

It is really easy to use and it is recommended to analyze your database regularly in order to get the best performances. In this tutorial, we are going to show you how to install and use MySQLTuner on Ubuntu 14.04.

The first thing we will do is to log in to the Ubuntu server and perform a system upgrade. It may take a few minutes but it is crucial for your server to be up-to-date with security releases. Use the following command to upgrade all your system software to the latest version available:

sudo apt-get update && sudo apt-get -y upgrade

Once the upgrade is completed, you can then install the MySQLTuner on your Ubuntu box. We will be going over two different approaches.

The first way is to use the apt package manager. Apt is the official package manager for Ubuntu. Since MySQLTuner is already added to the Ubuntu repositories, you can install it using the following command:

sudo apt-get install mysqltuner

To run MySQLTuner, you can execute the command:

sudo mysqltuner

It could take a few moments before the MySQLTuner provides you with the final report. We will review a sample report below.

The other way to install MySQLTuner is to download the source code from Git. This is generally the preferred approach since it allows you to use the latest version of the software.

Navigate to the /opt directory using the change directory command like listed below, on your server and download the latest version of MySQLTuner:

cd /opt/
wget http://mysqltuner.pl/ -O mysqltuner.pl

Make the script executable by using the command:

chmod +x mysqltuner.pl

And finally, run the script using the command:

./mysqltuner.pl

Or, if you do not like to make the script executable by adding the execute bit, you can directly call Perl to start the program by using the following command:

perl mysqltuner.pl

If your database requires a username and password to log in, you need to use the command below:

mysqltuner --user [replace with user name] --pass '[replace with password]'

Once the script is done running, you should get something like this:

 >>  MySQLTuner 1.7.2 - Major Hayden 
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering

[--] Skipped version check for MySQLTuner script
[OK] Logged in using credentials passed on the command line
[OK] Currently running supported MySQL version 10.1.34-MariaDB-0ubuntu0.18.04.1
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[--] Log file: /var/log/mysql/error.log(0B)
[OK] Log file /var/log/mysql/error.log exists
[OK] Log file /var/log/mysql/error.log is readable.
[!!] Log file /var/log/mysql/error.log is empty
[OK] Log file /var/log/mysql/error.log is smaller than 32 Mb
[OK] /var/log/mysql/error.log doesn't contain any warning.
[OK] /var/log/mysql/error.log doesn't contain any error.
[--] 0 start(s) detected in /var/log/mysql/error.log
[--] 0 shutdown(s) detected in /var/log/mysql/error.log

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +Aria +CSV +InnoDB +MEMORY +MRG_MyISAM +MyISAM +PERFORMANCE_SCHEMA +SEQUENCE
[--] Data in InnoDB tables: 7G (Tables: 314)
[--] Data in MyISAM tables: 71M (Tables: 40)
[OK] Total fragmented tables: 0

-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[!!] User '[email protected]%' hasn't specific host restriction.
[--] There are 612 basic passwords in the list.

-------- CVE Security Recommendations --------------------------------------------------------------
[OK] NO SECURITY CVE FOUND FOR YOUR VERSION

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 21d 23h 9m 35s (184M q [97.192 qps], 3M conn, TX: 1864G, RX: 21G)
[--] Reads / Writes: 97% / 3%
[--] Binary logging is disabled
[--] Physical Memory     : 31.3G
[--] Max MySQL memory    : 103.3G
[--] Other process memory: 796.6M
[--] Total buffers: 102.8G global + 3.4M per thread (151 max threads)
[--] P_S Max memory usage: 0B
[--] Galera GCache Max memory usage: 0B
[!!] Maximum reached memory usage: 103.3G (330.03% of installed RAM)
[!!] Maximum possible memory usage: 103.3G (330.04% of installed RAM)
[!!] Overall possible memory usage with other process exceeded memory
[OK] Slow queries: 0% (0/184M)
[!!] Highest connection usage: 99%  (150/151)
[OK] Aborted connections: 0.61%  (22585/3681785)
[!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performance
[!!] Query cache may be disabled by default due to mutex contention.
[OK] Query cache efficiency: 33.4% (81M cached / 243M selects)
[!!] Query cache prunes per day: 440717
[OK] Sorts requiring temporary tables: 0% (591 temp sorts / 5M sorts)
[OK] No joins without indexes
[!!] Temporary tables created on disk: 58% (1M on disk / 2M total)
[OK] Thread cache hit rate: 99% (150 created / 3M connections)
[OK] Table cache hit rate: 91% (535 open / 582 opened)
[OK] Open file limit used: 0% (73/16K)
[OK] Table locks acquired immediately: 99% (86M immediate / 86M locks)

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

-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is enabled.
[--] Thread Pool Size: 8 thread(s).
[--] Using default value is good enough for your version (10.1.34-MariaDB-0ubuntu0.18.04.1)

-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 18.6% (156M used / 838M cache)
[OK] Key buffer size / total MyISAM indexes: 800.0M/11.9M
[OK] Read Key buffer hit rate: 100.0% (164K cached / 30 reads)
[OK] Write Key buffer hit rate: 96.3% (810 cached / 780 writes)

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 100.0G/7.0G
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (1.7578125 %): 900.0M * 2/100.0G should be equal 25%
[OK] InnoDB buffer pool instances: 64
[--] InnoDB Buffer Pool Chunk Size not used or defined in your version
[OK] InnoDB Read buffer efficiency: 100.00% (16384398198 hits/ 16384540927 total)
[OK] InnoDB Write log efficiency: 94.33% (10010898 hits/ 10612739 total)
[OK] InnoDB log waits: 0.00% (0 waits / 601841 writes)

-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is enabled.
[OK] Aria pagecache size / total Aria indexes: 128.0M/1B
[OK] Aria pagecache hit rate: 99.3% (206M cached / 1M reads)

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

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

-------- RocksDB Metrics ---------------------------------------------------------------------------
[--] RocksDB is disabled.

-------- Spider Metrics ----------------------------------------------------------------------------
[--] Spider is disabled.

-------- Connect Metrics ---------------------------------------------------------------------------
[--] Connect is disabled.

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

-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] This is a standalone server.

-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    Restrict Host for [email protected]% to [email protected]
    Reduce your overall MySQL memory footprint for system stability
    Dedicate this server to your database for highest performance.
    Reduce or eliminate persistent connections to reduce connection usage
    Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1
    Increasing the query_cache size over 128M may reduce performance
    When making adjustments, make tmp_table_size/max_heap_table_size equal
    Reduce your SELECT DISTINCT queries which have no LIMIT clause
    Performance should be activated for better diagnostics
    Consider installing Sys schema from https://github.com/mysql/mysql-sys
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    max_connections (> 151)
    wait_timeout (< 28800)
    interactive_timeout (< 28800)
    query_cache_size (=0)
    query_cache_type (=0)
    query_cache_size (> 1000M) [see warning above]
    tmp_table_size (> 16M)
    max_heap_table_size (> 16M)
    performance_schema = ON enable PFS
    innodb_log_file_size * innodb_log_files_in_group should be equal to 1/4 of buffer pool size (=50G) if possible.

As you can see from the report above, the MySQLTuner provides comprehensive information regarding your server. Once you get the final report and the MySQLTuner recommendations, you can make the changes to your MySQL database server settings. Of course, be careful while making the changes since you may lose some important data from your databases if something is set in a wrong way.

If you are not sure about an issue, it is best to contact a MySQL expert in order to get the right answer. The best practice is to create a backup of your MySQL databases before making any changes so you can restore the databases later if something goes wrong.

Dotlayer is a knowledge-sharing platform built around a community of knowledge-seekers. We aim to make Dotlayer our readers' go-to source for all things tech-related. If you are interested in advertising or sponsorship opportunities, we are happy to discuss. Please submit all inquiries [email protected]. And if you enjoyed this post, we recommend reading our latest featured stories.

MySQL Database Performance Optimization Using MySQLTuner on Ubuntu 18.04 LTS
Rate this post

Join over 20,000 users and get our best content

Each week we send 1 email to 20,000 smart entrepreneurs just like you. Enter your email below to join the crew.

Thank you for subscribing.

Something went wrong.

Leave a Comment