A VPS for MySQL and MariaDB: the buffer pool, slow queries and backups
MySQL and MariaDB are the databases behind WordPress, Bitrix, OpenCart and most PHP projects. A small database lives comfortably on the same VPS as the website, but the default settings are designed for a minimal server: a 128 MB buffer pool, and slow queries aren't logged. Here is what to change and what to look for when choosing a plan.
innodb_buffer_pool_size: the key parameter
InnoDB keeps table and index pages in the buffer pool. As long as the hot data fits in it, queries rarely go to disk. MySQL itself offers a handy guideline: the innodb_dedicated_server mode gives the pool half the memory on servers with 1–4 GB and three quarters on servers with more than 4 GB. That rule is for a machine running only the database. If PHP-FPM, a web server and Redis run on the same VPS, subtract their usage and only then size the pool.
You can check whether the pool is big enough from the counters: compare Innodb_buffer_pool_reads (reads from disk) with Innodb_buffer_pool_read_requests (all requests). If the share of disk reads is noticeable and growing, there isn't enough memory. In MySQL 8 the pool size can be changed on the fly with SET GLOBAL, without a restart, so you can tune the value under real load.
The second parameter worth checking on a database with heavy writes is the size of the redo log. If it is small, InnoDB flushes modified pages to disk too often, and writes become jerky. Since version 8.0.30 the size is set by innodb_redo_log_capacity instead of the former innodb_log_file_size and innodb_log_files_in_group; MariaDB still uses innodb_log_file_size.
The rest of the memory goes on connections: sort and join buffers are allocated per thread. A high max_connections set "just in case" turns into an out-of-memory crash at peak traffic. A VPS with 4 GB is enough for a typical CMS site; a shop with a large catalogue needs more.
The slow query log
In most cases a database server is slow not because of a weak plan but because of a few queries without an index. Enable the slow query log:
slow_query_log = ONandlong_query_time = 1: the default threshold is 10 seconds, so the log is almost empty;log_queries_not_using_indexes: briefly, while investigating problems, since on a busy server the log grows fast;- build a summary with the
mysqldumpsloworpt-query-digestutilities, and analyse a suspicious query withEXPLAIN.
Typical findings are sorting on a column without an index, LIKE '%…%' on a large table, and queries against the options and plugin metadata tables of a CMS. An index added in five minutes often does more than moving to a plan twice as expensive. The database quirks of popular CMSs are covered on the VPS for WordPress and VPS for Bitrix pages.
Backups: dump, physical copy and binlog
- mysqldump with the
--single-transactionoption makes a consistent copy of InnoDB tables without blocking writes. It suits databases up to a few tens of gigabytes; restoring a large dump takes hours because all the indexes are rebuilt from scratch. - A physical copy with Percona XtraBackup for MySQL or
mariadb-backupfor MariaDB copies the data files on the fly and restores many times faster; for large databases it's the main method. - The binary log (binlog) on top of either copy gives point-in-time recovery: you restore the copy, then replay binlog events up to the position you need. Retention is set by
binlog_expire_logs_seconds; in MySQL 8 the default is 30 days, and on a busy database the logs take up more space than the data itself.
Send copies off the server, for example to a VPS for backups, and regularly test restoring them on a test machine. A backup that has never been restored is only the hope of a backup.
Replication and write durability
A replica on a second server lets you fail over quickly in an outage and takes heavy reports off the primary. Set it up with GTID: that makes it easier to switch roles and add new replicas. Remember that an asynchronous replica can lag, so a recently written row may not be visible on it yet. Check the lag in SHOW REPLICA STATUS (the Seconds_Behind_Source field), and keep the connection between servers on a private network or over WireGuard.
For durability, keep the defaults innodb_flush_log_at_trx_commit = 1 and sync_binlog = 1: every commit is flushed to disk. This is exactly where the difference between disks shows: on NVMe a synchronous write takes a fraction of a millisecond, while on an overloaded shared array waiting for it becomes the main bottleneck.