How to Fix the Error Establishing a Database Connection in WordPress

If you are a long-time WordPress user, then at some point in time you have faced the “Error Establishing a Database Connection” issue on your website. The WordPress error establishing a database connection is a common issue WordPress website owners face.

This issue means, Your WordPress can't connect to the database. WordPress is written in PHP and uses MySQL as its database system. So, when a user visits your site, PHP commands will fetch information from the database and show the contents to the user. But, if there is no connection to the database, then the WordPress “Error Establishing a Database Connection” message will pop up.

This issue can cause damage to your website and reputation. That is why in this write-up we will find the reasons for this issue, what damages it can cause to a website and how you can solve it in several ways.

Let's take a pen and notepad, and start taking notes.

Reasons for “ WordPress Error Establishing a Database Connection” Issue

There are quite a few reasons why the error establishing a database connection WordPress can occur. These reasons include-

  • Incorrect database login credentials: The WordPress database uses different login credentials from your cPanel. If there is any changes to your database username and password. then the old login information stored in your WordPress files will be invalid.
  • Corrupted database: Common issues like plugin failure or plugin/theme conflict can cause database corruption or missing information. When database information is missing or corrupted, then it can cause an error.
  • Database server error: The error may be caused by your web hosting. When there is a sudden surge of traffic or there are any permission issues, then you may face this issue.
  • Core files corruption: The WordPress core files define how your site works. If there is a problem with a plugin or theme, then the files can get corrupted. So, when modified incorrectly, it can cause an error establishing a database connection error.

If you see the following window on the back-end of your WordPress site, the problem likely comes from invalid login credentials.

This is a screenshot of error establishing database connection

You need to fix the login credentials to solve this issue.

Effect of WordPress Error Establishing a Database Connection Issues for Any Website

Suppose, you are running a physical store. That means you have opening and closing time. Customers will only come to your store during operating hours. However, when you are running an online store, there is no opening and closing time.

From getting subscribed to buying products, everything is in auto gear. Customers can visit your online store, place an order, and complete the transaction even in the middle of the night.

But what if they can't? Because when they visit your site, they can see only a white screen. This causes you not only to lose a potential customer/visitor but also to lose your brand reputation.

Here's how the “WordPress Error Establishing a Database Connection” can affect your website-

  • Website Downtime
  • Loss of Traffic and Revenue
  • Negative Impact on SEO
  • User Frustration
  • Damage to Brand Image
  • Data Loss or Corruption
  • Security Risks
  • Reduced User Engagement
  • Reputation Damage
  • Difficulty in Recovery

When you face this issues, you must act quickly to solve this issue. In the next part, we will address the ways you can solve this issue.

How to Solve the WordPress Error Establishing a Database Connection: 10+ Methods

WordPress is a big community and everyone is there to help each other. when they face any issue and solve it, they share the solution with others in the community. So that they can also solve the issue.

So, we went around and found some ways the users have solved this issue. Also, we have listed some of our own solutions as well. Here are the ways you can solve this issue-

  1. Make Sure Your Domain Is Pointing to Your Host Provider
  2. Check Your Database Server
  3. Check the Database Login Credentials
  4. Fix the Corrupted Files
  5. Repair the Database in WordPress
  6. Create a New Database
  7. Check Database Host Information
  8. Check Database Prefix
  9. Check Server Resource Limits
  10. Check for Plugin or Theme Issues
  11. Check Database Server Status
  12. Contact Hosting Support
  13. Check Database User Permissions
  14. Examine Error Logs

Now, let's see the solutions in detail-

1. Make Sure Your Domain Is Pointing to Your Host Provider

When you switch to a new hosting provider, your files will automatically move your files to the new host. But, if your domain is not pointing towards the new host, then your website will face failure.

To do so, start by checking the nameservers through your hosting control panel and WHOIS lookup tool.

If both nameservers match each other, then your domain is pointing correctly. Othrwise, you need to change the nameservers at your domain register. Simply access your hosting dashboard to get a list of the nameservers and enter them into your domain registrar’s control panel.

2. Check Your Database Server if its Down!

When everything seems to be working fine but still you are getting the “WordPress Error Establishing a Database Connection” issue, then your database server aka MySQL server may be down.

This happens when you there is suddenly a spike in traffic and your server can't handle the load. You will see your site will slow down and some users will get error while visiting your website.

You need to quickly contact your hosting provider and ask them if your MySQL server is responsive. Also, if you have more than one site running on the same server, then you should also check those sites to see how they are doing.

If you do not have any other site on the same hosting account, then simply go to your hosting dashboard and try to access phpMyAdmin and connect the database.

If you can connect, then we need to verify if your database user has sufficient permission. Create a new file called testconnection.php and paste the following code in it:

<?php
$link= mysqli_connect('localhost', 'username', 'password');
if(!$link) 
{die('Could not connect: '. mysqli_error());}
echo'Connected successfully';
mysqli_close($link);?>

3. Check the Database Login Credentials

The most common problem for error establishing database connection error is the wrong database login credentials. If you recently moved to a WordPress site to a new host, then this is the most likely reason.

Your WordPress database credentials are stored in the wp-config.php file. It is the WordPress configuration file that contains important WordPress settings including database information.

You’ll be looking for the following lines in the wp-config.php file:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

You need to make sure that the information for the database name, username, password, and database host is correct.

You can confirm this information from your WordPress hosting account dashboard. Simply log in to your hosting account and click on MySQL databases under the database section.

This is a screenshot of choosing MySQL Database option from cPAnel

This will take you to the database management page in your hosting dashboard. From here, you can find out your database name and the username.

This is a screenshot of Database user name

Below that you will find the database users and links to change the user password.

This is a screenshot of changing password and username

After verifying your database name, username, and password, make any necessary adjustments in your wp-config.php file with the updated information.

Save the changes and attempt to access your website again to check if the “Error Establishing a Database Connection” issue has been resolved.

If the error persists, it indicates that there might be another underlying issue. Proceed with additional troubleshooting steps to identify and address the problem.

4. Fix the Corrupted Files

Besides corrupted databases, corrupted plugins and theme files can also cause the issue of error establishing database connection.

If you are using a nulled plugin/theme or a faulty plugin/theme, then it can cause the error. To identify the issue, you need to check every plugin and theme file separately. If your theme is the problem, then you can activate another theme to see if the issue is solved.

To activate another theme, just go to Appearance–> Themes and activate another theme.

If changing the theme doesn't work then you can check the plugin files as well. You can try deactivating a plugin one by one. But if you don't want to do that, then you can try to rename the plugin folder from your cPanel.

Navigate to the File Manager menu on your cPanel dashboard. Click on public_html → wp_content and locate the plugins folder. Right-click on the folder, then choose Rename.

This is a screenshot of renaming the plugin folder

It will deactivate all the plugins at once. You can do that for the themes as well. If the error disappears after renaming the plugin folder, then you will know that the problem is with one of the plugins. Don't forget to rename the plugin folder to its original name after finding our the issue.

5. Repair the Database in WordPress

Corrupted database can also cause this issue. You need to repair the database to solve this issue.

You might have a corrupted database if your WordPress dashboard shows a message along the lines of “One or more database tables are unavailable. The database may need to be repaired.”

Fortunately, you can easily fix this problem by inserting the following function into the bottom of your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

6. Create a New Database

If none of the previously mentioned methods yield successful results, your database may have become irreparably corrupted. In such a scenario, the recommended course of action is to generate a new MySQL database and user through your hosting control panel.

After establishing a new MySQL database, it is imperative to safeguard your website's essential data by restoring it from a previously created backup. This process guarantees that you do not incur any loss of crucial information.

Navigate to the “Databases” section in cPanel, and then select “MySQL Databases.” At the top of the page, locate the “Create New Database” section. Begin by entering the desired name for your new database and proceed to click on the “Create Database” button to initiate the database creation process.

This is a screenshot of creating a new database

The system will then add your new database to the list of Current Databases.

In the same window, you have the option to create a new MySQL user and associate it with your recently created database. This user can be assigned specific privileges, granting them the ability to perform various tasks such as table creation, row updates, and more.

To set up a new MySQL user, input the desired username and password in the “MySQL Users” section. After providing the necessary credentials, click on the “Create User” button to generate the new user and assign it to the corresponding database.

Once a new user is created, scroll down to add the user to your new database.

A screenshot of showing Error establishing a database connection and its possible causes

Under the Modify Databases section, you will see two options to check or repair your database. To fix a corrupted database, choose the Repair Database option.

Modify databases window showing where to choose Repair Database option

Allow the system some time to complete the database creation process. Once finished, access your WordPress site to verify if the “Error Establishing a Database Connection” message persists.

Alternatively, you can attempt to repair database tables using phpMyAdmin. Navigate to “Databases” and select “phpMyAdmin.” Locate your database name, click on it, and then select all tables. Afterward, click on the option to check and repair the selected tables.

This is a screenshot of phpmyadmin database

Then, click on the dropdown menu next to the Check all button and select the Repair table option.

This is a screenshot of reparing table

It will trigger the REPAIR_TABLE statement to start and fix any corrupted table.

7. Check Database Host Information

Ensure that the database host information in your WordPress configuration file (wp-config.php) is correct. The database host is usually “localhost,” but it can vary depending on your hosting provider.

define('DB_HOST', 'localhost');

8. Check Database Prefix

Verify that the database table prefix in your wp-config.php file is correct. By default, it is set to ‘wp_', but if you have changed it during the installation, make sure it matches the actual database.

$table_prefix  = 'wp_';

9. Check Server Resource Limits

Insufficient server resources such as low PHP memory limit or low MySQL connection limits can lead to database connection errors. You can try increasing the memory limit in your wp-config.php file:

define('WP_MEMORY_LIMIT', '64M');

We have written a guide on how to check and update PHP version in WordPress. Have a read.

10. Check for Plugin or Theme Issues

Sometimes, faulty plugins or themes can cause database connection errors. Disable all plugins and switch to a default WordPress theme (like Twenty Twenty-One) to see if the issue persists. If the problem goes away, activate plugins and themes one by one to identify the culprit.

11. Check the Database Server Status

Ensure that your MySQL or MariaDB server is running and hasn't crashed. You can check the status of your database server through your hosting control panel or command line.

12. Contact Hosting Support

If you've tried all the above steps and still can't resolve the issue, it's a good idea to contact your hosting provider's support. They may be able to identify and resolve server-related problems that could be causing the database connection error.

13. Check Database User Permissions

Confirm that the database user associated with your WordPress installation has the necessary privileges to access and modify the database. Sometimes, permissions may get revoked or changed.

14. Examine Error Logs

Check the error logs on your server for more detailed information about the issue. Both PHP error logs and MySQL error logs can provide valuable insights into what might be causing the problem.

Remember to always back up your website before making any significant changes, especially when dealing with databases.

Get Rid of the Error Establishing Database Connection Issue!

So, we have shown you 14 ways to solve the WordPress error establishing database connection. You need to check one by one to find out what exactly is causing the issue.

Then apply the solution we have provided to solve the issue.

If the above solutions doesn't work, then you can try the below solutions as well-

  • Update WordPress Site URL: Try updating the WordPress site URL using phpMyAdmin. Simply access phpMyAdmin from your hosting account dashboard, and select your WordPress database.
  • Reboot Web Server: Users on dedicated servers, local server, and virtual private servers (VPS) can try rebooting their servers. This will restart your web and database server which may fix some temporary glitches causing the error.
  • Ask for help: If everything else fails, then you may need to contact your web hosting company. All good WordPress hosting companies will help you troubleshoot the problem, point you in the right direction, or even fix it for you.

If you face any issues while solving this problem, don't forget to give us a knock.

Subscribe to weDevs blog

We send weekly newsletter, no spam for sure

Rabbir Shad
Written by

Rabbir Shad

Shad is a Content Writer with expertise in eCommerce, SEO, WordPress, and Technology. He has a passion for Football. Besides, he likes to spend time reading a quality book or watching any classic film.

Have something to say? Cancel Reply

Your email address will not be published.

Table of Contents