Cheapest .com domain Just KSh 999 Find Your Domains 10x Faster Now.
India English
Kenya English
United Kingdom English
South Africa English
Nigeria English
United States English
United States Español
Indonesia English
Bangladesh English
Egypt العربية
Tanzania English
Ethiopia English
Uganda English
Congo - Kinshasa English
Ghana English
Côte d’Ivoire English
Zambia English
Cameroon English
Rwanda English
Germany Deutsch
France Français
Spain Català
Spain Español
Italy Italiano
Russia Русский
Japan English
Brazil Português
Brazil Português
Mexico Español
Philippines English
Pakistan English
Turkey Türkçe
Vietnam English
Thailand English
South Korea English
Australia English
China 中文
Somalia English
Canada English
Canada Français
Netherlands Nederlands

How To Fix The “Error Establishing a Database Connection” in WordPress

Buy domains, business emails, hosting, VPS and more: Get Started

Cheapest Domains in Kenya

Get your .Co.ke domain now for just KSh 999 (Back to 1200 in 7 days)

.CO.KE for KSh 999 | .COM for KSh 999

When you try to log into WordPress admin and instead of your familiar dashboard, you’re greeted with the error, “Error Establishing a Database Connection”, you start to think all sorts of things: What does this mean? Is my site broken? Is this gonna be costly to fix? And can this even be fixed, or do I have to rebuild my site all over again? All those months of work down the drain!

But before you go into a tailspin and start calling every techie you know, let me assure you, this is a common problem. There are solutions for it. It costs nothing to fix. And you most definitely don’t have to rebuild your site. 

Your website is still intact; you just need to troubleshoot a little bit to reach it. Most of the fixes we’ll cover in this article are quick and simple to do.

 But, should you have any problem, the Truehost team is always here to help you

Let’s dive in. 

What Does It Mean When You See “Error Establishing A Database Connection”?

error establishing database connection in WordPress
error establishing database connection in WordPress

You will see this error when WordPress cannot communicate with the MYSQL database, meaning it cannot reach content, user details, and site settings. 

The error, which usually appears on a blank page, prevents the admin dashboard from loading. The public-facing site (what visitors see) also doesn’t load any information. Instead, site visitors see a blank page or an error message.

What Causes Error Establishing a Database Connection In WordPress?

Before we explore how to fix this error,  it’s important to understand what could be causing it. Here are the most common causes: 

Incorrect Database Credentials. 

Using the wrong database name, username, password, or host in your WordPress configuration file (wp-config.php) would deny WordPress access to data. 

Using incorrect credentials is the most common cause of this error, so it’s the first place you should look. It’s also the easiest problem to fix because all you have to do is use the correct credentials.

Corrupted Database

Plugin conflicts can corrupt database tables. When two or more plugins interact with the same parts of the WordPress database in incompatible ways, this can corrupt database tables. This can be caused by poor coding practices, outdated software, or overlapping plugin functionality.  

In some cases, a plugin may alter the database schema by adding, removing, or modifying table columns, making the database incompatible with other plugins or WordPress itself. 

This can lead to missing data, broken indexes, or invalid table structures, making it difficult or impossible for WordPress to retrieve information correctly. 

Maxing out your allowed server limit

Too many data connections could max out your server limit, especially on shared hosting. This is common when you’re using large hosting providers who have too many users on shared hosting.  

Server Issues

Server issues might not be in your control, but if you’ve got a bad hosting service to begin with, you might encounter problems with downtime, overload, and during such times, WordPress won’t connect to the database. 

Hopefully, if this is a very rare occurrence and your hosting provider is reliable 99.9% of the time, you can resolve this issue with a call to your hosting provider. But if this is a persistent issue, we recommend switching to a reliable host like Truehost

Database host issues

Database host issues can also cause the “Error Establishing a Database Connection” in WordPress when the MySQL or MariaDB server specified in the DB_HOST setting of your wp-config.php file is either unreachable, overloaded, or misconfigured. 

In shared hosting environments, many websites share the same database server, so a traffic surge or heavy query load from other users can temporarily max out the server’s connection limits, preventing your site from connecting. 

On some hosts, the database server is located on a separate machine from your website, meaning DB_HOST might not be localhost but a remote address. If you don’t enter the correct IP address the host provides for the database, the connection will fail.

It’s also common for security firewalls or network restrictions to block requests from your web server to the database host. All these issues will need the hosting provider to resolve, as they control the server configuration and network routing. 

And if you keep experiencing this issue, it’s advisable to switch hosting or upgrade to a dedicated hosting plan. 

How to Fix “Error Establishing a Database Connection” in WordPress

1.  Confirm you’re using the correct database credentials

You’ll find database credentials in the wp-config.php file, in your WordPress root directory. To do this, go to cPanel or hPanel (if you’re using localhost). Once in cPanel: 

  1. Locate the wp-config.php. File>File manager>public_html>dev>sitename
  2. Scroll to the wp-config.php, right click on it, and click edit. 
  3. Scroll until you find these lines:
    define(‘DB_NAME’, ‘your_database_name’);
    define(‘DB_USER’, ‘your_database_username’);
    define(‘DB_PASSWORD’, ‘your_database_password’);
    define(‘DB_HOST’, ‘localhost’);
  4. Cross-check these credentials with the ones in your hosting account.
    MySQL Databases in cPanel
  5. In cPanel: Go to MySQL Databases.
  6. Cross-check the database name, username, password, and host with what’s in the wp-config file.
    Note: Some hosts use a hostname other than localhost. As mentioned earlier, this can happen if your database is set on a different server from the website. If that’s the case, get the correct DB Host credentials from your host. A port number also appears next to the host name, so make sure the number in your wp-config file is the right one.
  7. Update and save any incorrect details in wp-config.php.
  8. Go back to your site and refresh. If credentials were the problem, your WP dashboard should load.

You should also repair your permalinks to reset the page links. You don’t need to change anything here; just click save to refresh the changes. To do this:

  1. Go to settings on your dashboard
  2. Under General, click Permalinks 
  3. Scroll to the bottom of the page and click save
    This should reset any links that had a problem. 

2) Repair the WordPress Database

If credentials are correct but the error persists, check if your database is corrupted and repair it. To repair, you should activate WordPress’s main database repair tool. 

Follow these steps to enable the repair tool

  1. Open wp-config.php.
  2. Add: define(‘WP_ALLOW_REPAIR’, true);

You can add this repair line anywhere in the file, but it’s best to add it just before the comment line /* That’s all, stop editing! Happy publishing. */

  1. Click the save changes button at the top of the screen. 

Next, run the repair:

  1. Go to: “https://yourwebsite.com/wp-admin/maint/repair.php” and choose Repair Database.
Load the repair Database tool in WordPress
  1. This will load the file below, which gives the status of different items in your database. Repair any items that show up as not fine. 

Note the words at the bottom asking you to remove the line you added in the wp-config file 

WordPress Database repair status

So, next, disable the repair tool

  1. Head back to the wp-config and delete the line ‘WP_ALLOW_REPAIR’, true; to disable the repair tool.
  2. Save changes. 

3) Update the WordPress Site URL in the Database

An outdated site URL could also cause this error. If you recently migrated domains, installed SSL, or changed hosting, the site URL in the database may be outdated.

  1. Log in to your hosting control panel and open phpMyAdmin.
  2. Select the WordPress database that’s connected to your site (the database name should match DB_NAME in wp-config.php).
  3. Open the wp_options table and click edit on the first option on the panel that’s on the right. 
edit url in wp-options
  1. Edit the siteurl and home rows so they match your correct domain (including http:// or https://) and click go at the bottom of the screen. 
edit site url and save
  1. Reload your WordPress site.

4) Server Status and Resource Check

Sometimes the issue is on your host’s end.

  1. Check your hosting provider’s status page for outages.
  2. If there is no outage, check server resource limits and usage.

Your host can see how much traffic you have and if it’s good or bad traffic. They can then help you block any bad traffic or increase server resource count if your hosting cannot handle the traffic to your website. 

5) Restore a Backup

If the error started after a recent change, restore your site to a working version.

Ordinarily, to restore backups, you would use a plugin like UpdraftPlus, but since you cannot access your admin, contact your hosting provider to update to your latest backup. You should also ask your provider to check server health. 

Test the restored site before making new changes.

6) Restart your SQL services

Restarting your SQL services will reset your database and hopefully resolve the problem. You might need your hosting provider’s help to restart your services. 

Get A Professional To Perform Deeper Fixes

If the error persists even after you’ve done all the above, your site may require deeper debugging, such as inspecting server logs, checking database tables manually, or resolving configuration issues.

Unless you are really good at handling technical WordPress stuff, leave the deeper fixes to a WordPress developer or hosting support team.

7) Delete and Reinstall Corrupted WordPress Core Files

If up to this point you still haven’t fixed the Error Establishing a Database Connection, it’s likely being caused by broken or missing core files. 

So you need to delete and replace WordPress core files in cPanel, using these steps: 

  1. Open cPanel and backup your site.
back up your site in cPanel

Download a full account backup. 

select download a full account backup
  1. Next, go to the public_htm (it contains the files that the public sees). Double-click on it to open. You should see these folders: wp-admin, wp-content, and wp-includes. There are also files under this folder, including wp-config.php.
  2. Delete wp-admin and wp-includes, and any other files you might not need, but do not delete wp-content because this contains all your files. Also, do not delete wp-config.php because that’s what connects your site to the database.
delete wp-admin and wp-includes
  1. Next, download the latest WordPress package from wordpress.org and then upload it to public_html.
upload the WordPress zipped file to public_html
  1. Right-click on the zipped WordPress folder to extract files. Make sure all files all located in /public_html. If you don’t see them, move them there. 
  2. Refresh your WP admin page to log in. 

8) Repair all tables

You may have already run the repair tool as part of the quick fixes, but to be on the safer side, it’s good practice to go into your database to prompt manual repairs yourself just in case the repair tool missed something. 

So, go back to your cPanel and under databases, select phpMyAdmin. This will load all your databases on the left panel. Find the correct database and click on it. This will load all the tables on the right panel. 

  1. Scroll to the bottom of the right panel and click check all to select all the tables.
Database tables in phpMyAdmin
  1. Next, backup tables. Before you repair, head back to the top of the right panel and click export to back up the current tables. This way, you can always restore to the current tables should anything go wrong. 
  2. Repair tables. 
Repair all tables in PhpMyAdmin

Once the repair’s done, you should see this notification just at the top of the right panel “your SQL query has been executed successfully”. 

Your SQL query has been executed successfully

9) Review .htaccess

Plugins, especially security plugins, usually add entries in the .htaccess file, and this could create a conflict. By resetting the .htaccess file (or generating a new one), you remove any conflict that exists. 

To reset your .htaccess file in WordPress settings, first find the public_html directory. 

  1. In cPanel, go to Files > File Manager. 
  2. Scroll to public_html
  3. Scroll through your files to find the .htaccess file. If you don’t see the .htaccess file, click on settings on the top right of the screen, and enable hidden files. 
reset .htaccess
  1. Rename the .htaccess file. This will generate a new file. 
  2. Reload your site to check if this fixes the error. 

10) Increase PHP Memory

Your hosting allocates a site a certain memory limit, but your WordPress might only use a certain portion of it. 

It’s possible you’re using too little of that memory, and that increasing it (say to 512) may give your WordPress data a performance boost. 

But before you try this method, check how much memory your account can support. 

It’s also not a good idea to adjust the memory too high because this could cause your site to be inefficient. 

To adjust memory, 

  1. In your wp.config file, scroll to the bottom and insert the line define(‘WP_MEMORY_LIMIT’, ’256M’) just before the line ” /* That’s all, stop editing! Happy publishing. */ ”.
add define('WP_MEMORY_LIMIT', '512M');
  1. Change ‘256M’ to the desired memory. Try 512 for starters. 
  2. Click save changes and reload your site. Hopefully, having a higher memory limit will solve this problem. 

How to Prevent Error Establishing a Database Connection from Happening Again 

That’s it! 

You now know how to fix “error establishing a database connection”. 

After you fix it, you should do the following to prevent it from happening again: 

  1. Regularly back up your content and plugins using a tool like Updraft
  2. Use reliable hosting. Upgrade to Truehost now
  3. Limiting the number of plugins you install. 
  4. Keeping WordPress core, themes, and plugins updated.

Cheapest Domains in Kenya

Get your .Co.ke domain now for just KSh 999 (Back to 1200 in 7 days)

.CO.KE for KSh 999 | .COM for KSh 999