Quick answer: “Error establishing a database connection” means WordPress could not talk to its MySQL or MariaDB database, so it cannot load any content. The most common causes are wrong database details in wp-config.php, a database server that is down or overloaded, a corrupted database table, or a hosting account that has hit its limits. Start by asking your host whether the database server is healthy, then check the credentials in wp-config.php, then try WordPress’s built-in repair tool. Take a backup before changing anything.
What the error actually means
Every WordPress page is built on the fly. When someone visits, PHP runs WordPress, WordPress connects to the database, reads posts, settings and menus, and assembles the page. If that connection fails at the very first step, WordPress has nothing to show and prints this short message instead.
Because the database holds everything apart from your theme, plugins and uploaded files, the whole site goes down, front end and admin. The good news is that in most cases nothing has been lost. The content is still there. WordPress just cannot reach it.
The usual causes
- Incorrect database credentials. The database name, username, password or host in
wp-config.phpdo not match what the server expects. This often happens after a migration, a host change or a password reset. - Database server down or overloaded. On shared hosting, a busy neighbour or a spike in your own traffic can exhaust connections or crash the MySQL service.
- Corrupted tables. A failed update, a server crash or a disk problem can damage tables, especially
wp_options. - Hosting limits. Some plans cap database size or connections. Hitting the cap can block new connections.
- A hacked site. Malicious code can change
wp-config.phpor damage database tables. It is less common, but worth considering if nothing else explains it.
Before you start: back up
If you can access your hosting control panel, download a copy of your files and, if possible, export the database through phpMyAdmin or your host’s backup tool. Many hosts also keep daily backups you can restore. Our guide to website disaster recovery explains how to plan for days like this.
Step 1: Check whether it is the whole site
Visit both the front end and /wp-admin/. If the admin shows a different message, such as “One or more database tables are unavailable. The database may need to be repaired”, the connection works but tables are damaged. Skip to Step 4.
If both show the same connection error, continue.
Step 2: Ask your host about the database server
This is the fastest check and often the answer. Contact your host’s support or look at their status page. Ask:
- Is the MySQL server on my account running?
- Have I reached a connection or size limit?
- Was there maintenance or an outage?
If the server is down, the host has to fix it. If your traffic regularly overloads the database, it may be time to look at better WordPress hosting or caching.
You can also test the server yourself. If other sites on the same hosting account load fine, the server is probably working and the problem is specific to your site.
Step 3: Check the credentials in wp-config.php
Connect with your host’s file manager or SFTP and open wp-config.php in the site’s root folder. Look for these lines:
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );
Then compare them with your hosting control panel:
- DB_NAME: the database should exist in the database list. On cPanel, names usually carry your account prefix, such as
account_wp123. - DB_USER: the user should exist and be assigned to that database with all privileges.
- DB_PASSWORD: if you are not sure, reset the user’s password in the control panel and paste the new one into
wp-config.php. - DB_HOST: usually
localhost, but some hosts use a separate hostname or IP address, sometimes with a port. Check your host’s documentation.
Watch for stray spaces and quotes when copying. A single extra character breaks the connection.
If you recently moved the site, this is the most likely culprit. Our guide on moving WordPress to a new host covers the steps where credentials usually go wrong.
Test the connection directly
If you want to be certain the credentials work, a developer can create a small temporary PHP file that tries to connect with the same details and reports success or the exact MySQL error. Delete that file as soon as you are done, because it contains your password.
Step 4: Repair the database
WordPress includes a repair tool. Add this line to wp-config.php, just above the line that says “That’s all, stop editing”:
define( 'WP_ALLOW_REPAIR', true );
Then visit https://yoursite.com/wp-admin/maint/repair.php. Choose “Repair Database” or “Repair and Optimize Database”. The page does not need a login, which is why you must remove the line from wp-config.php as soon as the repair finishes.
You can also repair tables in phpMyAdmin: select the database, tick all tables and choose “Repair table” from the dropdown.
Once the site is back, regular housekeeping helps prevent repeat problems. See our guide to WordPress database optimization.
Step 5: Check the site URL settings
If the site loads partly but redirects oddly after a move, check siteurl and home in the wp_options table, or set them temporarily in wp-config.php with WP_HOME and WP_SITEURL. This is not the connection error itself, but it often appears alongside it after migrations.
Step 6: Look for signs of a hack
If the credentials keep changing, you see unfamiliar files in the root folder, or new admin users appear, treat it as a security incident. Change all passwords, including database and hosting, scan the files, and restore a clean backup if needed. Our hacked WordPress site guide lists the steps in order.
Step 7: Restore from backup
If none of the above works, restoring the most recent good backup of files and database is often the quickest route back online. Restore to a staging copy first if your host offers one, so you can confirm it works before replacing the live site.
When the error comes and goes
An error that appears for a few minutes and then clears usually points to resource limits rather than wrong settings. Signs include errors during traffic peaks, during scheduled backups or when bots crawl heavily. Fixes include:
- Page caching, so most visits never touch the database
- An object cache such as Redis on hosts that support it
- Blocking abusive bots and brute-force login attempts
- Removing plugins that run heavy queries on every page
- Moving to hosting with dedicated database resources
Uptime monitoring tells you how often it happens and when, which helps your host find the cause. See our guide to website uptime monitoring.
FAQ
Have I lost my content?
Almost always no. The error means WordPress cannot reach the database. The data is usually intact.
Why does only wp-admin show a different error?
WordPress checks tables more strictly in the admin. A message about repairing tables means the connection works but the database needs repair.
Can a plugin cause this error?
Indirectly. A plugin that floods the database with queries can overload it, but a plugin cannot usually break the connection settings themselves.
Should I leave WP_ALLOW_REPAIR switched on?
No. Anyone can open the repair page while it is enabled. Remove the line straight after use.
Keep the site steady
Most database connection errors are fixed in minutes once you know the order of checks. Good hosting, caching, backups and monitoring stop them becoming regular events. If you want a WordPress site that stays up and loads quickly, see how we help businesses turn visitors into customers.