Updated on Nov 17, 2022
Numerous factors can cause errors when connecting to your database. It could be corrupt files on your website, for example, from themes, plugins, or the WordPress core. Knowing how to resolve these issues can help reduce the time your website is unavailable.
In this article, we'll show you what a database connection error in WordPress looks like. Then we'll go over four steps to resolve the issue.
A WordPress site is made up of two parts: files and a database.
When you download WordPress, you'll find static files and PHP code files, but no pages or posts, usernames, passwords, comments, or configuration data — all of that is stored in a MySQL database.
MySQL is a database server that manages databases, tables, and the data contained within them. WordPress communicates with MySQL over the network, authenticating with a username and password before sending a SQL query instructing the database.
Loading the WordPress home page, for example, executes a PHP file — typically the home.php or index.php file, depending on how the site is configured. The code connects to the database and retrieves the home page's content, inserting it into a template that creates an HTML document.
A connection error is WordPress's way of telling you that it cannot obtain the information required to construct the page you are attempting to load. WordPress requested data, and the database either ignored it or said no. To correct the error, we must first determine why MySQL is acting strangely.
There are several possible explanations:
In summary, WordPress database errors usually indicate that WordPress cannot connect to or authenticate with the database or that the database is unable to respond with the correct information.
So, what exactly causes this? So, here are a few common reasons. We'll go over each of these in greater detail so you can figure out how to fix them. This error is usually resolved in less than 15 minutes.
Because determining the cause of the error can be difficult, the best approach is to try the most likely fixes until your WordPress site resumes normal operation.
WordPress uses four pieces of information to talk to MySQL:
They are stored as variables in the wp-config.php file in the WordPress installation’s root directory, and they look like this:
/** MySQL database username */ define(‘DB_USER’, ‘xxxxxx’);
Comparing the values listed in the wp-config.php file to those listed in the database is the first step in verifying your database credentials. In cPanel, open phpMyAdmin and navigate to the WordPress database in the left menu. Check that the database name and the DB NAME variable in wp-config.php are the same. Check that the hostname matches the name in the database server section, which will almost certainly be "localhost."
Next, review the "Users" section of the database entry to ensure that the database's username and password match the values used by WordPress. You can change the values in the database to match those in wp-config.php, or you can change the values in wp-config.php directly using the HTML Editor in the cPanel File Manager.
If the database credentials check out but the error persists, it’s time to consider file or database corruption.
Corrupt files are more likely, but minor database corruption is easier and less dangerous to repair, so it's worth a shot first. There are two approaches to WordPress database repair. The first step is to navigate to your database, select all of the tables, and then click on the "repair table" item in the dropdown menu.
The second method employs WordPress's built-in database repair mode, which you can enable by adding the following line to the wp-config.php file:
define(‘WP_ALLOW_REPAIR’, true);
Next, navigate to this URL:
https://example.com/wp-admin/maint/repair.php
Select the repair option and wait for WordPress to do its thing. It may take some time, but it should resolve the most common database corruption issues. When you're finished, remove the line of code you added to wp-config.php. Anyone can access the database repair URL, and you don't want malicious visitors to use it.
WordPress files can become corrupted due to hardware issues, malware, or user error. The goal is to replace corrupt files with new ones freshly downloaded from WordPress.org while keeping your site's plugins and themes in place.
Download WordPress, unzip it, and go to the "wordpress" directory. Delete the wp-content folder, as well as the files wp-config.php and wp-config-sample.php.
Open the root folder of the affected WordPress site in cPanel File Manager and upload all of the remaining files and folders from your local copy of WordPress. If cPanel prompts you to overwrite existing files, answer yes.
WordPress's core files will be replaced with brand-new ones. If this does not resolve the database error, you may want to replace wp-config.php as well, in which case you will need to edit the new version to include the correct database credentials, as discussed above.
If none of these methods work, it's time to restore the WordPress site—both the files and the database—from a recent backup taken before the connection error occurred.
When you first see "Error Establishing a Database Connection" on your WordPress site, it can be frightening. Still, hopefully, you now understand what the error means and how to restore harmony between WordPress and its database.
We hope you find this article useful. Discover more about FastCloud - the top-rated Hosting Solutions for personal and small business websites in four consecutive years by the HostAdvice Community!