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

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

Sometimes your website loses connection with your WordPress database. As a result, you see a blank white screen with the message “Error establishing a database connection.” 

There may be many reasons behind this connection loss.

It is one of the common errors in WordPress and is totally solvable. 

In this guide, you will learn what the “Error establishing a database connection is,” the possible causes, and various ways to fix it.

What is the “Error Establishing a Database Connection” in WordPress

Understanding this error is not a jigsaw puzzle. The error is self-explanatory. Here is what it means in detail: 

WordPress uses PHP and MySQL to function. 

  • PHP: It is a programming language used to write the core WordPress files. 
  • MySQL: It is a database that stores your site’s content, including pages, posts, layout, and all the settings. Basically, MySQL is the locker with all the elements of your website.

When a visitor lands on your website, WordPress executes PHP lines of commands to connect to MySQL. It then displays everything on the screen. 

When this connection breaks for some reason, WordPress cannot obtain information from the database to render on the screen. Thus, you are left with a blank white screen with the message “Error establishing a database connection” as below: 

Error Establishing a Database Connection Example

Common Causes of Error Establishing a Database Connection

The gist is that the PHP command is not able to connect with MySQL. There are a handful of reasons that result in this connection breakage. Here are some of the common causes of this error: 

1. Incorrect Credentials

WordPress needs specific credentials (username and password) to access the database. If the credentials you entered are wrong or mistyped, WordPress cannot fetch anything from your database. Thus, it shows the database connection error. 

2. Corrupted WordPress Files 

Sometimes, your core WordPress files get corrupted due to failed plugin updates, themes, or the main WordPress software. This, too, can cause the database connection error. 

3. Corrupted Database

Sometimes, a rogue plugin or a hacker’s attack can mess up your database, leading to a database connection error.

4. Server Issue

The database connection error also shows up when the server that stores your WordPress files gets corrupted or malfunctions.

5. Too Much Traffic 

Even though more traffic is what every site owner wants, too high a spike in traffic can cause your database to become unresponsive. It happens when your post or page gets viral, and everyone is trying to access your website. Your server cannot handle the request, and you get the database connection error.

How to Fix it?

Now that you know what this error is and what may cause it let’s look at how to fix it. You can follow these five ways to try and fix this error: 

Fix 1: Check Your Database Credentials 

Incorrect database credential is the most common cause for the database connection error in WordPress. If you have recently moved to another host, this is probably the reason in your case. 

You can check the credentials in your wp-config.php file. 

  • Log in to your site using an FTP client or a file manager. 

Here is an example of accessing the site using FileZilla.

access the wp-config file

The right quadrants show the list of folders that contain your website’s file. 

  • Navigate down to your website’s root folder. Mostly, the root folder is named after your domain name or public_html. Double click to open it. 
  • Right-click the wp-config.php file and click on the ’Edit’ option.
how to edit the wp-config

Now, look for the following four lines of information:

  • Database Name 
// ** MySQL settings ** //

/** The name of the database for WordPress */

define(’DB_NAME’, ’xxxxxx’);
  • Database username
/** MySQL database username */
define(’DB_USER’, ’xxxxxx’);
  • Database password
/** MySQL database password */

define(’DB_PASSWORD’, ’xxxxxxxxx’);
  • Database hostname (server)
/** MySQL hostname */

define(’DB_HOST’, ’localhost’);

You need to make sure this information is correct. You can confirm this from your hosting account dashboard. 

  • Log in to your hosting account. 
  • Under the ’Database’ section, click on the “MySQL databases.” 
accessing MySQL database

Note: The dashboard may differ depending on your hosting provider.

  • You will land on the database management page. From here, you can find the database name and the database username. 
confirming database name and database username

Below, in the “Current Users” section, you will also see the users and links to change the password.

confirming passwords of database users
  • After confirming the database name, database username, and password from the hosting account, you can change the information on your wp-config.php file accordingly.

Check if it solved the error. If not, let’s move on to the next fix.

Fix 2: Confirm Your Database Host Information

Sometimes, you may see the database connection error even if you enter the correct database name, database username, and password. In such a case, make sure you are using the right information of your database host. 

Most of the hosting companies use the local host as the database host. However, the managed WordPress hosting providers designate separate servers to host your database. In such a case, the local host is not your database host information. 

We recommend contacting your hosting company to confirm the database hosting information.

Fix 3: Repairing Your WordPress Database 

If you see a different error that reads, “One or more database tables are unavailable. The database may need to be repaired,” it means your database is corrupted. In such a case, you need to repair the database. 

To repair the database, add the following line before ’That’s all, stop editing! Happy blogging’ on your wp-config.php file.

define(‘WP_ALLOW_REPAIR’, true);

After adding the line, visit the page “http://www.yoursite.com/wp-admin/maint/repair.php.” This page is accessible even without logging in. You will get the option to repair the database and also optimize it.

repairing database in WordPress

We recommend selecting the “Repair Database” option since it is quicker. 

Note: After repairing your database, make sure to remove the line you added earlier on your wp-config.php file.

Fix 4: Fixing the Corrupt WordPress Database Files 

Another reason for the database connection error can be corrupted WordPress files. Whether the corrupt files occurred due to the hacker’s activity or during the file transfer via FTP, you can quickly fix it with the following procedure: 

Note: Before proceeding with this fix, make sure to backup your WordPress file if anything goes wrong later.

You are going to replace the core WordPress version on your website. So you’re not touching any themes, plugins, or media, only the WordPress installation.

  • Download WordPress from WordPress.org
Downloading WordPress
  • Unzip the files on your computer. 
  • Now, delete the wp-content folder and wp-config-sample.php file.
deleting the wp-content and wp-config-sample files in WordPress

Now, upload all the remaining files using SFTP to your website. This will overwrite and replace all the corrupted files with fresh ones. We recommend clearing your browser cache after uploading the files. Check if the database connection error persists.

Fix 5: Check if the Database Server is Down

If you still see the database connection error even though everything seems fine, it may be that your database server is down. It happens due to traffic overload on the server, and it cannot handle it. Such an issue occurs mostly on shared hosting plans.

If so, contact your hosting provider via phone or live chat. You can ask them if your MySQL server is down or not. If you have your other websites on the same server, you can also check if they are running or not to confirm the database server error.

Other Possible Fix for the “Error Establishing a Database Connection” 

If you are facing the error even after applying all the above tips, then here are some additional shots you can take: 

1. Updating the WordPress Site URL

You can try to solve the error by updating your site’s URL using phpMyAdmin. 

  • Access the phpMyAdmin on your hosting account’s dashboard.
accessing phpMyAdmin
  • Click on the SQL menu tab at the top.
accessing the SQL tab
  • Enter the following query: 

UPDATE wp_options SET option_value=’SITE_URL’ WHERE option_name=’siteurl’

Note: Replace the “Site_URL” part in the query with your actual WordPress site’s URL name.

2. Reboot the Web Server 

If you have your site on a local server, dedicated server, or VPS (Virtual Private Server), you can try to reboot your server. Doing so will restart your database server, which may fix the temporary glitches.

3. Reach Out for Technical Support 

If you still see the error, it is time to reach out to the support team of your hosting company. Good hosting companies offer 24/7 support with various options of reaching out, such as phone, email, and even live chat. They will help you out with solving the error.

Conclusion 

The database connection error can cause a severe dip in your SEO, conversion, as well as visitor’s trust if you soon don’t fix it. Here we have listed eight ways to fix the “error establishing database connection” in WordPress. 

Remember, the most common cause for this error is invalid credentials in your wp-config PHP file. We recommend you to start there. You can try other fixes sequentially if the first one does not work. 

You may also want to check out the article on what is 503 error and how to fix it. 

So, how did you fix the “error establishing a database connection” in WordPress? Do comment below: 

Leave a Reply

Your email address will not be published.