Are you a WordPress website owner but seeing a dreaded message in a white box that displays “Error establishing a database connection”? Do you fear that there is permanent damage to your website? Don’t panic; we’ll show you how to fix the error message with the help of easy and quick troubleshooting steps.

Over 455 million websites use WordPress. Why? Because you can get your website up and running without having to learn complex coding languages. With the help of WordPress plugins, built-in blogs, and themes, you can customize the design of your website anytime you want.

However, quite a few WordPress users probably feel a bit scared when they see a crucial site error. Today, we’ll discuss what WordPress database error is and what causes it to pop up on your screen.

Why does my WordPress website need a database?

Why does my WordPress website need a database?

A WordPress website consists of the files and the database. When you download WordPress through your hosting provider, it installs code files in PHP format. However, you won’t see posts, pages, user credentials, comments, or configuration data because all of this information is stored in a MySQL database.

So what is MySQL? It is a database server that manages tables, databases, and the information stored in them. WordPress authenticates with a username and password by communicating with MySQL before sending an SQL query instructing the database.

So when you load a WordPress home page, it executes a PHP file – usually index.php or home.php file. The code in the PHP file connects to the MySQL database to retrieve the information it needs to build the page, resulting in an HTML document.

What is WordPress database error?

What is WordPress database error?

Quite a few users report that when they try to load their WordPress website on a web browser, they see an “Error establishing a database connection” message on their browser screen. The error also pops up when users want to access their WordPress dashboard.

Database connection error means WordPress can’t recover the information it needs to create the page you’re trying to visit. To put it in simple words, WordPress inquired for data, and the database neglected it or said no.

What causes Error establishing a database connection in WordPress?

What causes Error establishing a database connection in WordPress?

To fix the database connection error on WordPress, you have to figure out what causes MySQL to misbehave. These causes can be the following.

  • Your login credentials for your database are wrong (These login details are different from the ones you use to access your WordPress website)
  • Your WordPress database is corrupted.
  • The server that hosts your database is down due to a big traffic spike.
  • Your WordPress plugin and theme files are corrupted.

How do I fix Error establishing a database connection in cPanel?

How do I fix Error establishing a database connection in cPanel?

WordPress needs four core bits of information to access your database and build a page for viewing.
  • Name of the database
  • Login username
  • Login password
  • Database hostname

If any one of these values is incorrect, WordPress will not connect to the database. To check the values on your database, you can use the phpMyAdmin tool in your hosting cPanel dashboard.

Once you’ve logged into phpMyAdmin, verify that the database name matches the DB variable in your wp-config.php file. Also, confirm that the hostname (Usually, localhost) matches the name in the database server file.

Afterward, ensure that the username and password in the “Users” section of the database match the values in the wp-config.php file. If the values don’t match, you can edit them in the database. You can also edit the values in the wp-config.php file by selecting the HTML editor under Control Panel File Manager program.

How do I fix Error establishing a database connection in WordPress?

How do I fix Error establishing a database connection in WordPress?

If the DB variables and user value match, but the error establishing a database connection persist, try these troubleshooting steps.

Rule out Plugins

Your WordPress files can corrupt due to a plugin or a theme, and the whole website can collapse. Do the following steps to rule out your website plugins as the cause of the database connection error.

  • Use a free tool to connect to your host via FTP. You will need login credentials to your FTP account, which you can get from your hosting provider.
  • Next, connect to the server and go to your WordPress website root folder, i.e., public_html.
  • Select the wp-content folder and look for the Plugins folder.
  • Now rename the plugin folder and go to your website to see if the error persists.
  • If the database error is gone, rename the main folder to plugins again.
  • Go to the plugins folders and change the individual plugin sub-folder names.
  • Browse your website each time you rename an individual plugin folder to locate the troublemaker.
  • Finally, remove the specific plugin or seek online help to repair it.

Use WordPress built-in database repair feature

WordPress has a built-in way to attempt to repair your database, but you’ll have to enable this feature first. To do this, you’ll need to access your wp-config.php file, which contains your WordPress installation settings and
configurations.
You can find wp-config in your root WordPress file directory, which you can access through the FTP client or your cPanel file manager. Once you have this file open, add the line “define(‘WP_ALLOW_REPAIR’, true);” to the bottom.
The above line of code will enable you to optimize and repair your database by visiting the “yourwordpresswebsite/wp-admin/maint/repair.php” URL.  When you are there, choose a repair option (You can select any) and wait for the process to complete.
The line of code you added on the WP-config file makes the database repair URL insecure, and anyone can access it. Therefore, be sure to remove it after you are done with repairing the database.

Restore WordPress files

As mentioned above, WordPress files can get corrupt for various reasons. No matter the cause, you can restore the default WordPress files to fix the database connection error. To do this, refer to the following steps.
  • Download the latest WordPress copy and unpack the zip file on your desktop.
  • Delete the wp-content folder in the unpacked archive.
  • Go to your website root folder via FTP.
  • Next, upload the new WordPress files.
  • Say “Yes” to the overwrite prompt for all files.
  • When the WordPress files are uploaded successfully, go to your website and see if it loads this time.

You can also upload the new WordPress files through your hosting control panel. To do this:

  • Open the unpacked WordPress folder and delete the wp-content folder, wp-config.php, and wp-config-sample.php files.
  • Go to the site’s root folder in cPanel File Manager
  • Select the upload option to replace the old WordPress files with the newly downloaded files.
  • Say “Yes” to the overwrite prompt for all files.

Now the new files will replace all of WordPress’s core files. If this doesn’t fix the error, try replacing wp-config.php too. In this case, edit the new version to add the correct database credentials, as discussed above.

Conclusion

If you followed all the troubleshooting steps that we took today and your website is still down, it could be a problem on your host’s end. Contact your web hosting support and provide them with all the details of the error and all the troubleshooting actions you’ve taken. Good Luck!!