{{announcement.body}}
{{announcement.title}}

MySQL 101: Linux Tuning for MySQL

DZone 's Guide to

MySQL 101: Linux Tuning for MySQL

When trying to do some Linux tuning for MySQL, there are a few options that will greatly influence the speed of MySQL.

· Database Zone ·
Free Resource

When trying to do some Linux tuning for MySQL, there are a few options that will greatly influence the speed of MySQL. Below are some of the most important of these settings to help you get started.

Swappiness

The first thing to look at is what swappiness is set to. This will determine the tendency of the kernel to swap out memory pages. In may cases, you will want to set this to "1" to keep the swapping to a minimum. A value of "0" will disable it entirely.

You can determine the current value with the following command: 

Java
 




x


 
1
cat /proc/sys/vm/swappiness



If this is not set to "1", you should consider making the change by using one of the following options:

Shell
 




x


 
1
# Make sure you are root and set swappiness to 1 
2
echo 1 > /proc/sys/vm/swappiness
3
 
4
# Or, you can use sysctl to do the same sysctl 
5
vm.swappiness vm.swappiness = 1



If the change helps, you will want to make it permanent by making the change in /etc/sysctl.conf: 

Java
 




xxxxxxxxxx
1


 
1
vm.swappiness = 1



I/O Scheduler

The default for many systems is either "noop" or "deadline". In almost all cases, "noop" is more efficient than "deadline", "cfq", or "anticipatory". To check the current value of the I/O Scheduler, issue the following command: 

Java
 




xxxxxxxxxx
1


1
cat /sys/block/sdb/queue/scheduler



If you want to test performance with "noop", try the following command:

Java
 




xxxxxxxxxx
1


 
1
 sudo echo noop > /sys/block/sdb/queue/scheduler



If you want to make the change permanent, you will need to do the following in the GRUB configuration file: 

Java
 




xxxxxxxxxx
1


 
1
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash elevator=noop"



CPU Governor

Most modern processors are capable of operating with a number of different clock frequency and voltage configurations. These are referred to as Operating Performance Points or P-states. Generally, the higher the clock frequency and the higher the voltage, the more instructions can be executed by the CPU over time. Likewise, the higher the clock frequency and the voltage, the more energy is consumed. As such, there is a trade-off between CPU capacity and power draw by the processor.

You can check what driver and governor are in use with the following command:

Shell
 




xxxxxxxxxx
1


 
1
cpupower frequency-info
2
...
3
driver: acpi-cpufreq
4
...
5
available cpufreq governors: conservative, ondemand, userspace, powersave, performance
6
...
7
The governor "ondemand" may decide which speed to use within this range.
8
...



In the above, you can see that the driver is "acpi-cpufreq" and the governor is set to "ondemand".

You can also check the governor setting by issuing the following: 

Shell
 




xxxxxxxxxx
1


1
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor



If "performance" is an option, you can make the change by issuing the command:

Shell
 




xxxxxxxxxx
1


 
1
echo "performance" | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor



Of course, you will want to decide if this has helped performance, so testing is essential.

NUMA

NUMA stands for "Non-uniform memory access." With NUMA, an SMP's system processor can access its own local memory faster than non-local memory. This may result in swapping and thus negatively impact database performance. When the memory allocated to the InnoDB Buffer Pool is larger than the amount of the RAM available and the default memory allocation policy is selected, swapping will likely occur. A server with NUMA enabled will report different node distances between CPU nodes.

You can determine your setting with the following: 

Java
 




xxxxxxxxxx
1


 
1
numactl --hardware



Whenever numactl shows different distances across nodes, the MySQL variable innodb_numa_interleave should be enabled to ensure memory interleaving. With Percona Server, you can improve NUMA support by using the flush_caches variable. This will help with allocation fairness across nodes. To determine whether the allocation is equal across nodes, you can check numa_maps with the script: https://github.com/sonots/bin/blob/master/numa-maps-summary.pl

Conclusion

As is normally the case, you should always test any changes you make in a lower environment before making them in production. Workloads are so different, and you must see how any changes impact your system.

Topics:
database, linux tuning, mysql, mysql 101, percona, tutorial

Published at DZone with permission of Michael Patrick . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}