Magento 2 MySQL High Memory Usage: Fix OOM Crashes

Magento 2 MySQL High Memory OOM Killer Fix

One sudden MySQL memory spike can bring an otherwise healthy Magento 2 store to a complete stop. When Magento 2 starts using unusually high amounts of MySQL or MariaDB memory, the impact can quickly spread across your entire store. Pages may become slow, checkout requests can fail, and background jobs may stop unexpectedly. In serious cases, Linux can trigger the OOM killer and terminate mysqld or mariadbd, causing database errors and temporary downtime.

The problem becomes harder to diagnose when memory usage keeps rising without an obvious reason. Large InnoDB buffers, too many database connections, expensive queries, cron jobs, indexers, imports, or poorly optimized extensions can all contribute to Magento 2 MySQL high memory usage.

This guide walks you through how to identify the source of MySQL memory spikes, check Linux OOM logs, review Magento database activity, tune memory settings, and decide when additional server RAM is actually needed.

What Causes Magento 2 MySQL High Memory Usage?

Magento 2 MySQL high memory usage usually occurs when the database has to handle more work than the available server resources can comfortably support. Large product catalogs, heavy customer activity, frequent orders, database connections, cron jobs, and Magento indexers can increase MySQL or MariaDB memory consumption. Settings such as innodb_buffer_pool_size, max_connections, temporary table limits, and per-connection buffers also affect how much RAM the database uses. When these settings are too aggressive for the server, memory usage can rise quickly during busy periods. For stores operating on limited infrastructure, moving to a cheapest dedicated server with sufficient RAM can also provide more predictable resources for database-heavy Magento workloads.

Common causes include:

Oversized innodb_buffer_pool_size
Too many simultaneous MySQL connections
Large per-connection buffers
Expensive queries from Magento or third-party extensions
Large temporary tables
Heavy indexing operations
Product or customer imports

How Do You Know MySQL Is Causing the Crash?

To confirm whether MySQL is responsible for a Magento 2 crash, start by checking the Linux system logs for Out-Of-Memory (OOM) events. Messages containing mysqld, mariadbd, out of memory, or killed processes can indicate that Linux terminated the database process after available RAM was exhausted. You can also check current memory usage with free -h and monitor processes using htop. If MySQL is consistently consuming most of the available RAM before the crash, while PHP-FPM, Redis, and other services use the remaining memory, the database is a strong area to investigate for Magento 2 database OOM issues.

Check MySQL or MariaDB Memory Settings

Connect to the database:

mysql -u root -p

Then inspect important settings:

SHOW VARIABLES LIKE 'innodb_buffer_pool_size';
SHOW VARIABLES LIKE 'max_connections';
SHOW VARIABLES LIKE 'tmp_table_size';
SHOW VARIABLES LIKE 'max_heap_table_size';
SHOW VARIABLES LIKE 'sort_buffer_size';
SHOW VARIABLES LIKE 'join_buffer_size';
SHOW VARIABLES LIKE 'read_buffer_size';
SHOW VARIABLES LIKE 'read_rnd_buffer_size';

These settings matter because MySQL memory usage has both shared and connection-specific components.

For example, the InnoDB buffer pool is a major shared memory area used for cached data and indexes. MySQL documentation notes that the buffer pool can typically be configured around 50–75% of system memory, depending on the workload and what else is running on the server. That range is not a universal Magento setting; the rest of the server’s memory requirements must be considered.

The mistake is to treat that percentage as a target without considering PHP, Redis, web services, background jobs, and operating-system overhead.

Example

Imagine a server with 16 GB RAM.

If you allocate almost all of it to MySQL, Magento still needs memory for:

  1. PHP-FPM workers
  2. Nginx or Apache
  3. Redis
  4. Cron
  5. Elasticsearch/OpenSearch where applicable
  6. Magento CLI commands
  7. Linux itself

During an import or reindex, those services may need additional memory.

That is when an apparently healthy configuration can suddenly become unstable.

Don’t Ignore Per-Connection Memory

One of the easiest mistakes is increasing MySQL buffers without considering connection count.

Some MySQL memory allocations are associated with individual connections or queries. Sort buffers, read buffers, join buffers, thread-related memory, and temporary work can increase memory consumption as workload grows.

For example, setting a very large join_buffer_size may look like a performance improvement.

It isn’t necessarily one.

MySQL documentation explains that a join buffer can be allocated for full joins where indexes aren’t being used. Increasing the setting doesn’t replace the need for proper indexes and efficient queries.

On Magento, that distinction matters.

A badly optimized query can create more memory pressure than a sensible server configuration.

How to Find the Query Creating the Memory Spike

If memory increases during a specific period, find out what Magento is doing at that time.

Check running queries:

SHOW FULL PROCESSLIST;

For a deeper investigation, use Performance Schema where available.

You can also check slow queries through the MySQL slow query log.

Look for queries that:

Run for a long time
Scan large tables
Use temporary tables
Perform expensive sorting
Execute repeatedly
Appear during checkout
Start during cron jobs
Appear during indexing
Come from a recently installed extension

A useful pattern is to compare the database activity with the time of the memory spike.

For example:

  • 2:00 AM: memory rises
  • 2:03 AM: cron starts
  • 2:05 AM: indexer activity increases
  • 2:10 AM: MySQL reaches 95% RAM
  • 2:11 AM: mysqld is killed

That timeline tells you much more than simply saying “MySQL has a memory leak.”

Magento Cron and Indexers Can Trigger Spikes

Magento cron handles many background operations.

Indexing can also generate significant database activity, particularly on stores with large catalogs or frequent product updates.

Adobe’s current Commerce hardware guidance notes that database memory needs are closely related to the size of the stored data and indexes. It also provides separate memory expectations for different Commerce operations, showing why application, cron, and database workloads need to be considered together.

Check indexer status:

php bin/magento indexer: status

Then check cron:

php bin/magento cron:run

Don’t repeatedly run heavy Magento commands manually on a production server while troubleshooting. A command that consumes significant memory can make the original problem worse.

Also check whether multiple cron processes are overlapping.

A poorly scheduled import, reindex, backup, and cache operation can create a short but dangerous memory spike.

How to Fix a MariaDB Memory Leak on Magento

If you suspect a fix mariadb memory leak magento situation, first confirm whether memory keeps increasing after workload decreases.

A genuine leak behaves differently from normal cache growth.

For example:

Normal behavior:

Memory rises during a heavy operation workload finishes memory stabilizes.

Potential leak:

Memory rises workload finishes memory continues increasing repeated workloads push usage higher service eventually crashes.

Track memory over several hours or days.

You can use:

free -h

and:

ps aux --sort=-%mem | head

For MySQL:

mysqladmin processlist

Record the values instead of checking only when the server is already failing.

Also review your exact Magento, MySQL, and MariaDB versions. Adobe’s database guidance lists supported database combinations and notes that MariaDB behavior can differ by version, including differences in reindexing performance.

If a memory problem starts immediately after an upgrade, compare the old and new versions and review their documented compatibility requirements before changing random database variables.

Reduce Dangerous MySQL Memory Settings

Don’t copy a large MySQL configuration from another server.

A configuration designed for a 64 GB database server can be disastrous on a 16 GB Magento server.

Review:

innodb_buffer_pool_size
max_connections
tmp_table_size
max_heap_table_size
sort_buffer_size
join_buffer_size
read_buffer_size
read_rnd_buffer_size

Make one change at a time.

Then monitor the server.

This makes it easier to identify which change actually helped.

Temporary tables deserve special attention. MySQL can use memory for internal temporary tables and switch to disk when size limits are reached.

That means simply increasing temporary-table limits isn’t automatically a fix. It can trade disk activity for higher RAM consumption.

Check Magento Extensions Before Blaming MySQL

Third-party extensions can generate inefficient queries or perform heavy background processing.

If memory problems started after installing or updating an extension, investigate that change.

Review:

Recently installed modules
Custom observers
Product import extensions
Search integrations
Reporting modules
ERP integrations
Payment extensions
Inventory synchronization
Custom cron jobs

Use Magento’s module list:

php bin/magento module: status

Don’t disable production modules blindly. Test changes in a staging environment first.

If one extension creates thousands of database queries or repeatedly loads large datasets into memory, increasing server RAM may only hide the underlying problem.

How Much RAM Does a Magento 2 Dedicated Server Need?

The RAM required for a Magento 2 dedicated server depends on your store size, product catalog, traffic, database workload, PHP-FPM processes, Redis, search services, and background tasks. A small Magento store may run with a modest memory allocation, while larger stores with heavy traffic, frequent imports, and complex extensions need significantly more RAM. For stable performance, don’t size RAM for MySQL alone. Leave enough memory for Magento, PHP, caching, search services, and the operating system. Monitoring peak usage and testing your real workload is the best way to determine the right Magento dedicated server RAM sizing for your store.

Need more resources for your Magento 2 store? Choose a dedicated server with enough RAM and processing capacity to handle database workloads, traffic spikes, and background tasks without unnecessary memory pressure. Explore reliable dedicated server hosting and find a configuration that fits your Magento workload.

Frequently Asked Questions

1. What causes high MySQL memory usage in Magento 2?

High MySQL memory usage can result from large InnoDB buffer pools, too many database connections, inefficient queries, temporary tables, cron jobs, indexing, imports, and resource-heavy Magento extensions.

2. Why does Magento 2 MySQL get killed by the OOM killer?

The Linux OOM killer may terminate MySQL when the server runs out of available RAM. This can happen when MySQL, PHP-FPM, Redis, search services, and other processes collectively consume more memory than the server has.

3. How can I check if MySQL is causing Magento 2 crashes?

Check Linux kernel logs for mysqld, mariadbd, out of memory, or killed process messages. You can also use free -h and htop to monitor RAM usage before and during a crash.

4. How do I reduce Magento 2 MySQL high memory usage?

Review MySQL memory settings, reduce excessive connection and buffer limits, optimize slow queries, check Magento cron and indexers, and investigate extensions that generate heavy database activity.

5. Can Magento 2 extensions cause MySQL memory spikes?

Yes. Poorly optimized extensions can generate expensive queries, load large datasets, or run resource-intensive background tasks, increasing MySQL and overall server memory usage.

Wrapping Up

Magento 2 MySQL high memory usage doesn’t automatically mean that MySQL or MariaDB has a memory leak. The spike may come from the InnoDB buffer pool, connection-level buffers, temporary tables, expensive queries, cron jobs, indexers, imports, or simply a server that doesn’t have enough RAM for the complete Magento stack.

Start with evidence. Check the Linux OOM logs, identify the process that was killed, review MySQL memory settings, inspect queries, and correlate the spike with Magento activity.

Once you know what is consuming memory, the solution becomes much clearer. You may need database tuning, extension optimization, better job scheduling, additional swap, more physical RAM, or a larger dedicated server. For stores with consistently heavy database workloads, a properly configured MySQL dedicated server can provide the resources needed to handle database operations more reliably.

The key is to fix the actual source of memory pressure instead of repeatedly restarting MySQL and hoping the problem disappears.