Optimizing a MySQL server database running Sysbench
In this example study, we are going to optimize a MySQL instance by setting the performance goal of maximizing the throughput of operations towards the database.
As regards the workload generation, in this example we are going to use Sysbench, a popular open-source benchmarking suite.
To import the results of the benchmark into Akamas, we are going to use a custom script to convert its output to a CSV file that can be parsed by the CSV provider.
Environment Setup
In order to run the Sysbench suite against a MySQL installation, you need to first install and configure the two software. In the following , we will assume that both MySQL and Sysbench will run on the same machine, to obtain more significant results in terms of performance you might want to run them on separate hosts.
A set of scripts is provided to support all the setup steps.
MySQL Installation
To install MySQL please follow the official documentation. In the following, we will make a few assumptions on the location of the configuration files, the user running the server, and the location of the datafiles. These assumptions are based on a default installation of MySQL on an Ubuntu instance performed via apt.
Configuration file: /etc/mysql/conf.d/mysql.cnf
MySQL user: mysql
MySQL root user password: root
This is a template for the configuration file mysql.cnf.template
If your installation of MySQL has different default values for these parameters please update the provided scripts accordingly.
Sysbench Installation
To install Sysbench on an ubuntu machine run the following command
sudo apt install sysbench
To verify your installation of Sysbench and initialize the database you can use the scripts provided here below and place them in the /home/ubuntu/scripts folder. Move in the folder, make sure MySQL is already running, and run the init-db.sh script.
This is the init-db.sh script:
#!/bin/bashset-ecd"$(dirname "$0")"mysql-uroot-proot-e"CREATE DATABASE IF NOT EXISTS sbtest"HOST="--mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --mysql-password=root"sysbench oltp_read_only --tables=10 --table_size=1000000 --threads=100 $HOST --time=300 --max-requests=0 --report-interval=1 --rand-type=uniform --db-driver=mysql --mysql-db=sbtest --mysql-ssl=off prepare| tee -a res.warmup.ro.txt
#sleep 5#sudo systemctl stop mysql###Create the backup#echo "Backing up the database"#sudo rm -rf /tmp/backup#sudo mkdir /tmp/backup#sudo rsync -r --progress /var/lib/mysql /tmp/backup/#sleep 2##sudo systemctl start mysql#sudo systemctl status mysql
This script will:
connect to your MySQL installation
create a sbtest database for the test
run the Sysbench data generation phase to populate the database
The init-db.sh script contains some information on the amount of data to generate. The default setting is quite small and should be used for testing purposes. You can then modify the test to suit your benchmarking needs. If you update the script please also update the run_benchmark.sh script accordingly.
Optimization Setup
Here follow a step by step explanation of all the required configuration for this example. You can find attached a zip file that contains all of the YAML files for your convenience.
System
In this example, we are interested in optimizing MySQL settings and measuring the peak throughput measured using Sysbench. Hence, we are going to create two components:
A mysql component which represents the MySQL instance, including all the configuration parameters
A Sysbenchcomponent which represents Sysbench and contains the custom metrics reported by the benchmark
The Sysbench component
MySQL is a widespread technology and Akamas provides a specific Optimization Pack to support its optimization. Sysbench, on the other hand, is a benchmark application and is not yet supported by a specific optimization pack. To use it in our study, we will need to define its metrics first. This operation can be done once and the created component type can be used across many systems.
First, build a metrics.yamlfile with the following content:
metrics: - name:throughputdescription:The throughput of the databaseunit:tps - name:response_time_avgdescription:The average response time of the databaseunit:milliseconds - name:response_time_95thdescription:The response time 95th percentile of the databaseunit:milliseconds - name:durationdescription:The duration of the task (load or benchmark execution)unit:seconds
You can now create the metrics by issuing the following command:
akamascreatemetricsmetrics.yaml
Finally, create a file named sysbench.yaml with the following definition of the component:
name:Sysbenchdescription:> Sysbench benchmark. It is a purely synthetic benchmark that can create isolated contention on system resources. Each of the benchmark’s transaction imposes some load on three specific resources: CPU, disk I/O, and locks. It is also used to simulate a database workload.
parameters: []metrics: - name:throughput - name:response_time_avg - name:response_time_95th - name:duration
You can now create the component by issuing the following command:
akamascreatecomponent-typesysbench.yaml
Model the system
Here’s the definition of our system (system.yaml):
name:MySQL-Sysbenchdescription:A system for optimizing MySQL with Sysbench
Here’s the definition of our mysql component (mysql.yaml):
A workflow for optimizing MySQL can be structured in 6 tasks:
Reset Sysbench data
Configure MySQL
Restart MySQL
Launch the benchmark
Parse the benchmark results
Here below you can find the scripts that codify these tasks.
This is the restart-mysql.sh script:
#!/usr/bin/env bashset-ecd"$(dirname "$0")"#Stop the DBecho"Stopping MySQL"sudosystemctlstopmysql&> /dev/null#sudo systemctl status mysql#Apply Configurationecho"Copying the configuration"sudocpmy.cnf/etc/mysql/conf.d/mysql.cnfsync; sudosh-c"echo 3 > /proc/sys/vm/drop_caches"; sync#Restart DBecho"Restarting the database"sudosystemctlstartmysql&> /dev/null#sudo systemctl status mysqlsleep2
We are going to use Akamas telemetry capability to import the metrics related to Sysbench benchmark results, in particular, the transaction throughput and latency. To achieve this we can leverage the Akamas CSV provider, which extracts metrics from CSV files. The CSV file is the one produced in the last task of the workflow of the study.
In this example, we are going to leverage Akamas AI-driven optimization capabilities to maximize MySQL database transaction throughput, as measured by the Sysbench benchmark.
Here is the Akamas study definition (study.yaml):
name:MySQL Sysbench Tuningdescription:Tuning of mysql-8 with Sysbench benchmarksystem:MySQL-Sysbenchworkflow:MySQL-Sysbenchgoal:objective:maximizefunction:formula:Sysbench.throughputconstraints: []# Akamas score automatically trim 1m of warm-up and 1m of tear-downwindowing:task:testtype:trimtrim: [1m,1m]# We optimize some common MySQL parametersparametersSelection: - name:mysql.mysql_innodb_buffer_pool_sizedomain: [5242880,10485760] - name:mysql.mysql_innodb_thread_sleep_delaydomain: [1,3000] - name:mysql.mysql_innodb_flush_method - name:mysql.mysql_innodb_log_file_size - name:mysql.mysql_innodb_thread_concurrencydomain: [0,4] - name:mysql.mysql_innodb_max_dirty_pages_pct - name:mysql.mysql_innodb_read_ahead_threshold# The metrics we are interested inmetricsSelection: - Sysbench.throughput - Sysbench.response_time_95th# Each experiment can run multiple trials to evaluate stabilitynumberOfTrials:1steps:# We first run a baseline experiment with default values - name:baselinetype:baselinerenderParameters: ["mysql.*"]# We then optimize for 200 experiments - name:optimizetype:optimizeoptimizer:AKAMASnumberOfExperiments:200maxFailedExperiments:200renderParameters: ["mysql.*"]
You may need to update some parameter domains based on your environment (e.g. InnoDB buffer pool size maximum value depends on your server available memory)
You can create the study by running:
akamascreatestudystudy.yaml
You can then start it by running:
akamasstartstudy"MySQL Sysbench Tuning"
You can now follow the study progress using the UI and explore the results using the Analysis and Metrics tabs.