MySQL 8.4 Tuning on a 2GB VPS: my.cnf Settings That Actually Help

This guide covers MySQL 8.4 tuning 2GB VPS owners can apply in one sitting, without copying a config written for a bigger server. A 2 GB VPS runs MySQL next to the OS, Nginx, and PHP-FPM, so every megabyte you give the database is one something else cannot use. The goal is not peak speed but a database that stays up and out of swap during a traffic spike, so the my.cnf settings below trade a little throughput for a server that stays up.

MySQL 8.4 Tuning 2GB VPS: Check Memory Before You Change Anything

Do not edit my.cnf blind. Run free -h and look at available memory. If it sits under 300 MB at idle, fix the memory budget before you touch any other setting.

Next, find out how much memory MySQL itself uses. Run htop, sort by memory, and note the mysqld row and the PHP-FPM workers beside it. For a repeatable way to watch RAM, CPU, and disk together, this guide to monitoring disk, RAM, and CPU usage with htop and Glances walks through both tools. PHP-FPM deserves attention too, because its workers eat RAM as well; the companion guide to PHP-FPM tuning and pm.max_children shows how to size that side of the stack so the two services stop competing.

Note three numbers before you change anything: available memory at idle, the mysqld resident size, and the total of your PHP-FPM workers. Compare them with the same numbers after the restart.

A my.cnf That Fits in 2 GB of RAM

Here is a complete [mysqld] section for a 2 GB VPS running MySQL 8.4. Put it in /etc/mysql/my.cnf or a file under /etc/mysql/mysql.conf.d/, after backing up the current file:

[mysqld]
innodb_buffer_pool_size = 512M
innodb_buffer_pool_instances = 1
innodb_log_file_size = 128M
max_connections = 50
thread_cache_size = 8
sort_buffer_size = 256K
join_buffer_size = 256K
read_buffer_size = 128K
read_rnd_buffer_size = 256K
tmp_table_size = 32M
max_heap_table_size = 32M
table_open_cache = 400
performance_schema = OFF

The query cache is missing on purpose: MySQL removed it in version 8.0, so there is nothing to tune in 8.4. An old my.cnf from a MySQL 5.7 tutorial may contain query_cache_size or query_cache_type, which MySQL 8.4 rejects as unknown variables, so delete those lines.

Why Each Setting Gets That Value

The buffer pool decides whether tuning helps or hurts. InnoDB keeps table data and indexes in the pool, so a bigger pool means fewer disk reads. On a 2 GB box, innodb_buffer_pool_size at 512 MB leaves roughly 1.5 GB for the operating system, Nginx, PHP-FPM, and the per-connection buffers below. A 1 GB pool leaves so little room that the kernel starts swapping, and a swapped database is slower than a smaller pool in RAM. The log file follows the pool: 128 MB is a quarter of it, enough to batch writes without checkpointing constantly.

Connections are the quiet memory eater. MySQL allocates sort, join, and read buffers per connection, so worst case memory grows with every connection you allow. Fifty is plenty for a small site behind PHP-FPM. If your application opens a new connection per request and never closes it, fix that first: raising the limit only moves the crash from “too many connections” to “out of memory”.

The per-connection buffers stay small for the same reason. A 256 KB sort buffer and a 256 KB join buffer look tiny next to the multi-megabyte values in old tuning guides, but those guides assumed a server with room to spare. Temporary tables get 32 MB each, set in both tmp_table_size and max_heap_table_size, since MySQL uses the lower of the two. Queries that need more spill to disk, which is slower, but that beats letting temp tables eat the RAM your buffer pool needs.

Turning off the performance schema is the easiest saving here. It instruments queries, waits, and stages, which costs memory and CPU on every statement. On a big server the insight is worth it; on a 2 GB VPS running a blog or a small shop, it is not. Turn it back on only for a short session when you need to chase a specific slow query.

How to Check MySQL Memory Use Right Now

In the MySQL shell, run SHOW GLOBAL STATUS LIKE 'Threads_connected'; to see open connections. If you sit at 45 out of 50 on a normal afternoon, your limit is too low or your application leaks connections; find out which before you raise the limit.

To check mysql memory usage from the shell, run ps -o rss,comm -p $(pgrep -x mysqld) for the resident size in kilobytes. SHOW ENGINE INNODB STATUSG prints pool size, free pages, and pages read from disk; check it twice, a day apart.

One more check matters: SHOW GLOBAL VARIABLES LIKE 'innodb_buffer_pool_size'; reports the value MySQL actually loaded, not the value you think you wrote. Run it after every config change; if the number does not match your file, you edited the wrong file or MySQL read a different config path.

Apply the Changes and Verify They Took Effect

Editing my.cnf changes nothing until MySQL restarts, and most settings above are not dynamic. Save the file, then restart with sudo systemctl restart mysql; on some distributions the service is named mysqld instead. Expect a few seconds of downtime, so do this outside peak hours.

After the restart, verify in order. First, confirm the service is up and read the last lines of the error log, usually at /var/log/mysql/error.log, for warnings about unknown variables. Second, confirm the buffer pool reads 536870912 bytes, which is 512 MB. Third, load the site, click through a few pages, and watch free -h for ten minutes to confirm available memory stays above your baseline.

If MySQL fails to start, restore the backup and read the error log. Usually it is a typo or a variable that does not exist in 8.4. MySQL 8.4 resizes log files on restart, but if a crashed shutdown locked them, remove them while MySQL is stopped and the server recreates them at the new size.

Common Mistakes on a 2GB VPS

The most common mistake is an oversized buffer pool. A pool of 1 GB or 1.5 GB looks like a win in every tuning calculator, because calculators assume MySQL owns the machine. It does not. Set the pool to 512 MB.

The second mistake is allowing too many connections. A max_connections value of 200 or 500, copied from a tutorial for a bigger server, lets a traffic spike pile up connections until the box runs out of memory. Fifty keeps the worst case inside the budget. If the site truly needs more, the fix is a bigger VPS or a connection pooler, not a bigger number in my.cnf.

The third mistake is chasing the wrong bottleneck: slow pages are often caused by uncached PHP work, oversized images, or a backup job running at noon. Enable the slow query log for a day with slow_query_log = ON and long_query_time = 1; if it stays empty, the problem lives elsewhere. The fourth mistake is editing the wrong config file. MySQL reads several files in order and a later file overrides an earlier one, so a value in /etc/mysql/mysql.conf.d/mysqld.cnf beats the same value in /etc/mysql/my.cnf. Confirm with SHOW GLOBAL VARIABLES that the running server uses your value.

When Tuning Is Not Enough

Good tuning buys headroom, but it cannot fix a workload that outgrew the box. If the pool stays warm, connections stay under control, and pages are still slow, look at the data: add a missing index before you add RAM, since a query that scans a whole table stays slow on any pool size.

Swap is the safety net when tuning runs out. A small swap file keeps the out-of-memory killer from taking MySQL during a short spike, but a database that lives in swap crawls. If you do not have swap yet, this guide to adding swap on a VPS shows how to create a file and set swappiness so the kernel treats it as a last resort.

There is also a point where self-hosting stops being the cheapest option. If the site needs more RAM every few months, or database admin keeps eating time you would rather spend on the site, compare the numbers. This managed MySQL versus self-hosted MySQL cost comparison lays out what a managed service charges against a VPS plus your own time, so you can decide with real numbers. For most small sites, these settings keep MySQL 8.4 steady on a 2 GB VPS for a long time.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.