MySQL and MariaDB Tuning for Low-Memory VPS: Configuration That Fits 1-2 GB RAM

Running MySQL or MariaDB on a 1-2 GB VPS requires careful configuration. Out of the box, these databases allocate memory assuming a dedicated server with several gigabytes of RAM. On a budget VPS, that default configuration triggers swap usage, OOM kills, and cascading performance degradation. Here is how to tune MySQL and MariaDB for low-memory environments.

Start with a Memory Budget

Before touching MySQL configuration, calculate your available memory. On a 2 GB VPS running Nginx, PHP-FPM, and MySQL, roughly 500-800 MB should be allocated to the database. Subtract the OS overhead (200 MB), Nginx (50 MB), and PHP-FPM (200 MB), and you are left with about 800 MB for MySQL. On a 1 GB VPS, budget 300-400 MB at most.

InnoDB Buffer Pool: The Most Important Setting

The InnoDB buffer pool caches data and indexes in memory. Set it to 60-70% of your MySQL memory budget. For a 2 GB VPS with 800 MB allocated to MySQL: innodb_buffer_pool_size = 512M. For a 1 GB VPS with 400 MB allocated: innodb_buffer_pool_size = 256M. Add this to your /etc/mysql/mariadb.conf.d/50-server.cnf or /etc/mysql/my.cnf.

Reduce Per-Connection Memory Usage

MySQL allocates memory per connection. The defaults are generous and will exhaust your VPS under even moderate concurrency. Adjust these values:

sort_buffer_size = 256K
read_buffer_size = 128K
read_rnd_buffer_size = 256K
join_buffer_size = 256K
max_connections = 30

These settings reduce per-connection overhead from several megabytes to under 1 MB. With 30 connections, total connection memory stays under 30 MB. If your application needs more connections, use a connection pooler like ProxySQL.

Query Cache and Temporary Tables

For MariaDB, enable the query cache with a small allocation: query_cache_size = 32M and query_cache_type = 1. For MySQL 8.0+, the query cache is removed — use application-level caching instead. Keep temporary tables in memory: tmp_table_size = 32M and max_heap_table_size = 32M.

Swap Prevention and Monitoring

On a low-memory VPS, MySQL memory spikes can trigger swap. Monitor with free -h and sar -r 1. If MySQL hits swap, performance collapses. Add vm.swappiness=10 to /etc/sysctl.conf to discourage the kernel from swapping unless absolutely necessary.

Check your InnoDB buffer pool hit rate with SHOW ENGINE INNODB STATUS\G and look for the buffer pool hit rate — aim for 99% or higher. For benchmarks comparing VPS plans with different RAM configurations, visit our hosting comparison page.

Quick Configuration Template

[mysqld]
innodb_buffer_pool_size = 512M
innodb_log_file_size = 128M
innodb_flush_log_at_trx_commit = 2
sort_buffer_size = 256K
read_buffer_size = 128K
join_buffer_size = 256K
max_connections = 30
tmp_table_size = 32M
max_heap_table_size = 32M
table_open_cache = 256
query_cache_size = 32M
query_cache_type = 1

Apply these changes, restart MySQL, and benchmark with mysqlslap to verify the impact. A well-tuned low-memory instance can handle thousands of queries per second for typical web workloads.

Leave a Reply