Skip to content
Hosting

How to Fix Error Establishing a Database Connection

The 'error establishing a database connection' message means WordPress cannot reach its database. This guide walks through the checks that fix it, from wp-config.php to database repair.

By TremAfrica Research Updated November 5, 2026 5 min read

“Error establishing a database connection” means WordPress cannot reach the MySQL or MariaDB database that holds your posts, pages and settings. In most cases the cause is one of four things: wrong login details in wp-config.php, a database server that is down or overloaded, a corrupted database, or damaged WordPress files. Work through the checks below in order and you will usually find the problem within a few minutes.

What the error means

Every WordPress page is built from data stored in a database. When a visitor loads a page, WordPress opens a connection to the database using the details in a file called wp-config.php. If that connection fails, WordPress cannot build anything, so it shows a single line of text instead of your site.

The error looks the same whatever the cause. That is why it helps to check the likely causes one at a time, starting with the simplest.

Before you start: take a backup

Some of the fixes below involve editing files or repairing database tables. Take a copy of your files and database first if you can. On cPanel hosting you can do this from the Backup tool. Our guide on how to take a backup walks through it.

Step 1: Check the database details in wp-config.php

This is the most common cause, especially after a migration, a password change or a move to a new host. Open wp-config.php in the root folder of your WordPress install using the cPanel File Manager or an FTP client. Find these four lines:

  • DB_NAME – the name of the database
  • DB_USER – the database username
  • DB_PASSWORD – that user’s password
  • DB_HOST – the database server, usually “localhost” on cPanel hosting

Now compare them with what exists on your hosting account. In cPanel, open MySQL Databases. Check that:

  • The database name matches exactly. cPanel adds your account username as a prefix, so a database called “wpdb” will appear as something like “cpuser_wpdb”.
  • The database user exists and has the same prefix.
  • The user is added to the database with All Privileges. A correct username and password will still fail if the user is not assigned to the database.

If you are not sure of the password, set a new one for the database user in cPanel, then paste the same password into DB_PASSWORD. Save the file and reload your site.

Step 2: Test the login in phpMyAdmin

If the details look right but the error remains, test them directly. Open phpMyAdmin from cPanel and check that the database is listed and that it contains tables (usually starting with “wp_” or a custom prefix). If the database is empty, WordPress has nothing to connect to. You may need to import a backup or restore from your host’s backups.

Also check the table prefix line in wp-config.php. If it says “wp_” but your tables start with something else, WordPress will not find them.

Step 3: Check whether the database server is running

If several sites on the same account show the error at the same time, the database server itself may be down or overloaded. Signs include:

  • The error comes and goes, often during busy periods.
  • phpMyAdmin is slow or will not open.
  • You see “Too many connections” in error logs.

On shared hosting you cannot restart the database service yourself, so contact your host’s support. On a VPS or dedicated server with root access, check the service with systemctl status mysql (or systemctl status mariadb) and restart it if needed. If it keeps stopping, check free memory and disk space. A full disk is a common reason a database server refuses to start.

Step 4: Repair a corrupted database

Sometimes the front of the site shows the connection error while /wp-admin shows a different message saying one or more tables are unavailable and the database may need to be repaired. That points to corrupted tables.

WordPress has a built-in repair tool:

  1. Add this line to wp-config.php, just above the line that says “That’s all, stop editing”: define(‘WP_ALLOW_REPAIR’, true);
  2. Visit yoursite.com/wp-admin/maint/repair.php in your browser.
  3. Click Repair Database (or Repair and Optimize).
  4. Remove the line from wp-config.php when you are done. The repair page does not require a login, so leaving it enabled is a security risk.

You can also select the tables in phpMyAdmin and choose Repair table from the drop-down menu.

Step 5: Replace damaged WordPress core files

A failed update or an infection can damage core files. Download a fresh copy of WordPress from wordpress.org. Upload the wp-admin and wp-includes folders over your existing ones. Do not overwrite wp-content (your themes, plugins and uploads) or wp-config.php.

Step 6: Rule out a hacked site

If the database password changed and nobody on your team changed it, or you find unfamiliar files or admin users, treat the site as compromised. Change your cPanel, database and WordPress passwords, and scan the account for malware. If you need help, Tremhost’s Armor SOS offers free same-day triage for hacked sites.

How to stop it happening again

  • Keep regular backups of both files and database, and test that you can restore them. See how to restore from a backup.
  • Update WordPress, themes and plugins regularly, and test big updates on a staging copy first.
  • Remove plugins you no longer use.
  • Use strong, unique passwords for cPanel, database users and WordPress admins.
  • Choose hosting with enough resources for your traffic, so the database server is not overloaded.

Frequently asked questions

Will I lose my content if I see this error?

Not usually. The error means WordPress cannot reach the database, not that the data is gone. In most cases fixing the login details or restarting the database server brings everything back. Only a badly corrupted or deleted database causes real data loss, which is why backups matter.

Why did the error appear after I moved my site to a new host?

The new host will have a different database name, username and password, and sometimes a different database host. Update all four values in wp-config.php to match the database you created on the new account, and make sure the user has privileges on that database.

What should DB_HOST be?

On most cPanel hosting it is “localhost”. Some hosts run databases on a separate server and give you a hostname or IP address instead. If “localhost” does not work, ask your host which value to use.

Is it safe to leave WP_ALLOW_REPAIR switched on?

No. Anyone can open the repair page while it is enabled. Remove the line as soon as the repair has finished.

Next step

If you would rather not troubleshoot database errors yourself, Tremhost Managed WordPress includes updates tested on staging, daily backups and malware scanning and removal. Support is available Monday to Friday, 08:00 to 17:00 CAT.

Ask us on WhatsApp