Saturday, December 5, 2015

Configuring MySQL to use minimal memory

http://www.tocker.ca/2014/03/10/configuring-mysql-to-use-minimal-memory.html


I've been experimenting with MySQL Fabric - the official tool in development to provide HA and sharding. Part of this experimentation has meant that I have been running 7 copies of the MySQL Server on my laptop at once, which can consume a lot of memory :)
So today I wanted to write about how to configure MySQL to use less memory, which may also be beneficial in situations where peak performance is not important. For example: a developer VM environment.

Previous configuration guides

Before I get started, let me point to some of my previous configuration guides:

Configuring MySQL to use minimal memory

The MySQL defaults have to balance performance with what is considered reasonable for what may be a development system with other applications needing to run alongside MySQL. In many cases, this will mean 4-8GB, but on virtual machines (or in my case with 7 copies of mysqld running), there is a lot less available.
Obligatory warning: If you are running these settings on a machine with 1GB+ RAM, you should expect worse performance when compared to the defaults.
SettingDefaultMinimum
innodb_buffer_pool_size128M5M
innodb_log_buffer_size1M256K
query_cache_size1M0
max_connections1511 (although 10 might be more reasonable)
key_buffer_size83886088
thread_cache_size(autosized)0
host_cache_size(autosized)0
innodb_ft_cache_size80000001600000
innodb_ft_total_cache_size64000000032000000
thread_stack262144131072
sort_buffer_size26214432K
read_buffer_size1310728200
read_rnd_buffer_size2621448200
max_heap_table_size1677721616K
tmp_table_size167772161K
bulk_insert_buffer_size83886080
join_buffer_size262144128
net_buffer_length163841K
innodb_sort_buffer_size1M64K
binlog_cache_size32K4K
binlog_stmt_cache_size32K4K
(Note: you may be able to save a little more memory by disabling InnoDB or performance schema, but I don't recommend these two steps.)
To summarize these changes:
# /etc/my.cnf:
innodb_buffer_pool_size=5M
innodb_log_buffer_size=256K
query_cache_size=0
max_connections=10
key_buffer_size=8
thread_cache_size=0
host_cache_size=0
innodb_ft_cache_size=1600000
innodb_ft_total_cache_size=32000000

# per thread or per operation settings
thread_stack=131072
sort_buffer_size=32K
read_buffer_size=8200
read_rnd_buffer_size=8200
max_heap_table_size=16K
tmp_table_size=1K
bulk_insert_buffer_size=0
join_buffer_size=128
net_buffer_length=1K
innodb_sort_buffer_size=64K

#settings that relate to the binary log (if enabled)
binlog_cache_size=4K
binlog_stmt_cache_size=4K