Fixing WordPress “Error Establishing a Database Connection”

One of the most common and disruptive problems WordPress site owners encounter is the message:

Error establishing a database connection

This means WordPress cannot communicate with MySQL. While incorrect credentials or corrupted tables can cause this, a frequent and often overlooked cause on small VPS instances is MySQL crashing due to insufficient memory. On 1 GB servers, this issue is extremely common with MySQL 8.0.

This article explains how to diagnose the issue using system and SQL logs and provides the exact steps required to fix it permanently.


Why WordPress Shows the Database Connection Error

WordPress communicates with MySQL to load every page, query posts, authenticate users, and handle settings. If MySQL is not running or cannot start, WordPress has no database to connect to and displays the generic database connection error.

Checking MySQL on the server:

systemctl status mysql

usually shows something similar to:

Main process exited, code=killed, status=9/KILL
Failed to start MySQL Community Server.

The important detail is status=9/KILL, which indicates the Linux kernel forcibly terminated MySQL. This almost always happens due to memory exhaustion, especially on 1 GB droplets without swap.

To confirm this, you must inspect the MySQL logs.


Checking the MySQL Error Logs

MySQL writes detailed failure information to its error logs. These logs provide the clearest insight into why the database is not running.

Log locations vary slightly by system, but the most common paths are:

/var/log/mysql/error.log
/var/log/mysql/mysql.log
/var/log/mysql/error.log.1

View the log with:

sudo cat /var/log/mysql/error.log

Or view the systemd log stream:

sudo journalctl -u mysql --no-pager

To search for memory-related failures:

grep -i memory /var/log/mysql/error.log

Typical errors confirming memory exhaustion include:

Cannot allocate memory for the buffer pool
Plugin initialization aborted
Data Dictionary initialization failed.
Aborting
Unable to allocate memory
Can't create thread to handle new connection (errno= 11)

These messages mean MySQL attempted to allocate memory for InnoDB and failed. When this happens, MySQL aborts during startup. On a 1 GB system without swap, this is expected behavior because MySQL 8.0 has much higher memory requirements.

Once MySQL is down, WordPress cannot access its database and shows the connection error.


Why This Happens on WordPress Servers

Several factors increase memory pressure on small WordPress droplets:

  • MySQL 8.0 has a larger InnoDB buffer pool by default
  • WordPress collections and plugin queries generate background load
  • wp-cron and scheduled tasks create recurring spikes
  • Bots and crawler traffic increase concurrent connections
  • No swap space means the kernel cannot handle memory spikes safely

When memory runs out, the Linux kernel uses the Out Of Memory (OOM) Killer to terminate MySQL in order to keep the system running. This happens silently, and WordPress simply displays the database error.

The permanent fix requires enabling swap and reducing MySQL’s memory usage.


Step 1: Add a 2 GB Swap File

Swap provides the OS with emergency memory when physical RAM is exhausted. Without swap, MySQL will be killed repeatedly.

Create a 2 GB swap file:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Make swap persistent:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Verify:

free -h

This should display a swap section with approximately 2 GB available.


Step 2: Reduce MySQL’s Memory Usage

Next, tune MySQL to fit within the limits of a 1 GB machine.

Open MySQL’s configuration file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Add these settings at the bottom:

innodb_buffer_pool_size=128M
innodb_log_file_size=64M
max_connections=20
thread_cache_size=2
table_open_cache=200
performance_schema=OFF

Restart MySQL:

sudo systemctl restart mysql

These values significantly reduce MySQL’s RAM requirements and prevent future crashes.


Step 3: Verify MySQL and WordPress Are Working Again

Check whether MySQL is now active:

systemctl status mysql

If it shows active (running), refresh your WordPress site. The database connection error should now be resolved.

If the error persists, re-check:

  • SQL logs for new errors
  • Remaining available memory
  • Whether the swap file is active

Summary

The WordPress message “Error establishing a database connection” is often caused by MySQL not running. On 1 GB servers, the most common cause is memory exhaustion, which triggers the kernel to terminate MySQL.

To fix this issue permanently:

  1. Check the SQL logs to confirm memory allocation errors
  2. Enable swap space to protect MySQL from OOM kills
  3. Tune MySQL to use less memory
  4. Restart MySQL and verify WordPress connectivity

Once these steps are complete, MySQL becomes stable on a 1 GB server and WordPress will load normally.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *