Skip to content

MySQL my.cnf tuning — the variables that actually move the needle

A pragmatic walk through innodb_buffer_pool_size, log files, connection limits, and timeouts — what to set, why, and the verification queries to confirm.

MySQL ships with conservative defaults so the daemon starts on a 1 GB VM and on a 256 GB host without complaint. On any real workload those defaults are wrong. This reference covers the dozen variables that move the needle on throughput and latency, what to set them to on a dedicated database host, and the runtime queries that confirm your changes took effect.

How to verify

To see the current values of every variable that matters:

SHOW VARIABLES WHERE Variable_name IN (
  'innodb_buffer_pool_size','innodb_buffer_pool_instances',
  'innodb_log_file_size','innodb_log_files_in_group','innodb_log_buffer_size',
  'innodb_flush_method','innodb_flush_log_at_trx_commit','innodb_io_capacity',
  'innodb_io_capacity_max','innodb_read_io_threads','innodb_write_io_threads',
  'max_connections','thread_cache_size','table_open_cache',
  'tmp_table_size','max_heap_table_size','sort_buffer_size','join_buffer_size',
  'wait_timeout','interactive_timeout','max_allowed_packet',
  'binlog_format','sync_binlog'
);

And the runtime state that tells you how MySQL is using what you gave it:

SHOW ENGINE INNODB STATUS\G
SHOW GLOBAL STATUS LIKE 'Threads_%';
SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool_pages_%';

What’s happening

MySQL has dozens of tunable variables, but five categories drive ninety percent of the impact: InnoDB buffer pool (memory), InnoDB log (durability and write throughput), connection limits and timeouts, per-session buffers for sorts and joins, and binlog/durability for replicated topologies.

The biggest single lever is innodb_buffer_pool_size — the in-memory cache of InnoDB pages. On a dedicated host, give it 60-70% of system RAM. Anything less and you read from disk on every cache miss; anything more risks the OOM killer or swap. Second is innodb_log_file_size, which buffers writes — too small means frequent checkpoints (slow); too large means longer crash recovery.

After those two, the rest is fine-tuning. Connection limits prevent the daemon from running out of file descriptors; per-session buffers control how much memory each query consumes; binlog flags trade durability against throughput.

The procedure

A working [mysqld] section for a dedicated 16 GB host serving an OLTP workload:

[mysqld]
# --- InnoDB memory ---
innodb_buffer_pool_size        = 11G        # ~70% of RAM
innodb_buffer_pool_instances   = 8          # split into 8 shards for concurrency
innodb_buffer_pool_chunk_size  = 128M       # buffer pool is allocated in chunks of this size

# --- InnoDB log (redo) ---
innodb_log_file_size           = 1G
innodb_log_files_in_group      = 2          # total redo = 2G
innodb_log_buffer_size         = 64M

# --- InnoDB IO ---
innodb_flush_method            = O_DIRECT   # bypass OS page cache (we have our own)
innodb_flush_log_at_trx_commit = 1          # 1 = ACID; 2 = looser; 0 = fast and lossy
innodb_io_capacity             = 2000       # baseline IOPS budget for background ops
innodb_io_capacity_max         = 4000       # burst IOPS budget
innodb_read_io_threads         = 8
innodb_write_io_threads        = 8

# --- Connections ---
max_connections                = 500
thread_cache_size              = 100        # cache threads for connection reuse
table_open_cache               = 4000       # file-descriptor cache for tables
table_definition_cache         = 2000

# --- Per-session buffers (be conservative — these multiply by max_connections) ---
sort_buffer_size               = 4M
join_buffer_size               = 4M
read_buffer_size               = 2M
read_rnd_buffer_size           = 2M
tmp_table_size                 = 64M
max_heap_table_size            = 64M

# --- Timeouts ---
wait_timeout                   = 600
interactive_timeout            = 600
max_allowed_packet             = 64M

# --- Binlog and replication ---
binlog_format                  = ROW
sync_binlog                    = 1
binlog_expire_logs_seconds     = 604800

# --- Slow query log ---
slow_query_log                 = 1
slow_query_log_file            = /var/log/mysql/mysql-slow.log
long_query_time                = 0.5
log_queries_not_using_indexes  = 1

Step-by-step tuning loop:

  1. Set buffer pool first. This requires a restart on MySQL versions before 5.7.5; from 5.7.5+ you can grow it online: SET GLOBAL innodb_buffer_pool_size = 11811160064;.

  2. Confirm the new size took effect.

    SHOW VARIABLES LIKE 'innodb_buffer_pool_size';
  3. Check the hit ratio after 24 hours of warm-up.

    SELECT
      (1 - (SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME = 'Innodb_buffer_pool_reads')
         / (SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME = 'Innodb_buffer_pool_read_requests')
      ) * 100 AS hit_ratio_pct;

    Aim for above 99%. If lower, you need more buffer pool or you have a working set bigger than the host RAM (in which case start considering sharding or read replicas).

  4. Resize log files. As of MySQL 8.0.30, innodb_redo_log_capacity (new variable) lets you resize redo online. On older versions, the procedure is: stop the daemon cleanly, edit innodb_log_file_size, remove the old ib_logfile* files, start the daemon (it recreates them at the new size). A dirty shutdown before the move loses transactions in the redo log — clean shutdown only.

  5. Tune connections to match the application pool. max_connections should be 20% higher than the sum of all application connection pools. If your app servers have pools of 50 each and you have 8 app servers, that’s 400 — set max_connections = 500.

  6. Set timeouts to match the longest legitimate query. Default wait_timeout = 28800 (8 hours) leaves idle connections hanging too long. 600 (10 minutes) is the sweet spot for most application workloads.

  7. innodb_flush_log_at_trx_commit trade-offs. Value 1 is ACID — fsync on every commit, durable to disk. Value 2 fsyncs once per second — survives MySQL crash but not host crash. Value 0 fsyncs once per second and skips even the commit-time write to OS — fastest, most data loss. For financial workloads, never use anything but 1. For analytics ingest where data is replayable, 2 is often defensible.

  8. Restart and observe. Run SHOW ENGINE INNODB STATUS\G after the workload has run for an hour. Look at “BUFFER POOL AND MEMORY” section for hit ratio, “ROW OPERATIONS” for insert/update/delete throughput, and “LATEST DETECTED DEADLOCK” for contention signals.

Common pitfalls

  • innodb_buffer_pool_size set to 90% of RAM crowds out the OS page cache, journal buffers, and the daemon’s own working memory — leading to OOM kills during checkpoints. 70% is the practical ceiling.
  • Setting sort_buffer_size = 256M triples per-connection memory consumption — with 500 connections, that’s potentially 384 GB of sort buffers. Keep per-session buffers small (2-4 MB) and only tune up for specific queries with SET SESSION.
  • max_connections too low produces “Too many connections” errors during traffic spikes. Too high, paired with large per-session buffers, can OOM the host.
  • innodb_log_file_size too small forces frequent checkpoints — the daemon stalls while writing dirty pages back to disk. The symptom is sawtooth latency: smooth, then a 500 ms spike, then smooth.
  • query_cache_size was removed in MySQL 8 — if you migrate from 5.7, drop the variable from my.cnf or the daemon will refuse to start with an “unknown variable” error.

In the engagements we run, MySQL configuration ships as a versioned mysql.conf.d/zz-stackharbor.cnf overlay applied by Ansible, with values templated from the host’s actual RAM so the buffer pool scales when we resize. We watch the runtime metrics through mysqld_exporter and treat any sustained hit-ratio dip as a capacity signal. Operated under Managed Operations.