The database connection error blocks your access to the WordPress dashboard. You try to load a page, but all you see is a white box with a mysterious error message. This error brings down your entire website and has to be rectified immediately. The more it’s delayed, the more it will negatively impact your website traffic, sales, and user experience.
How can you fix a database error when you can’t even open the admin dashboard to see what’s wrong?
Also Read: move an email account from one cPanel server to another
What is the Error Establishing a Database Connection?
This is a common error; it doesn’t mean your content is lost. It is fixable. The error establishing a database connection means that for some reason or another, the PHP code could not connect to your database to retrieve the information it needs to build that page fully.
When a page request is made, PHP pulls the page content from the database and displays it in your browser. That will include all content, plugin information, images, videos, audio, and almost everything you see on the page. Like how the database gets corrupted, your core WordPress files can also get corrupted.
That’s why the error is always shown on a blank page because there is no information about your site as it is not connected to your database.
Also Read: Is Your Cpanel Not Receiving Emails-What to Do?
Why did this Error Occured?
We need to find out what is causing the error when we try to open the WordPress site. It could be because of many reasons, as errors in database connectivity may have several different underlying causes.
For instance, it can be because of the wrong login username and password being used to access the admin panel. If your web server is not up to the sudden spike in traffic, your database could become unresponsive. Another possibility is that the database might be corrupted; the WordPress database can be corrupted by several things, like installing a faulty or incompatible plugin.
Also Read: How to upload Laravel Project on Shared Hosting With cPanel?
How to Fix Error Establishing a Database Connection
Before we start fixing the error, we recommend you back up your website and store it on your PC. There will be a lot of manipulation of your database information, and if anything goes wrong, we’ll have a way to recover the data.
An easy way to back up your website is through WordPress plugins such as VaultPress or WP Time Capsule.
As the website has now been backed up and we have no fear of losing data, let’s fix the error.
1. Check Your Database Login Credentials
The very first thing you should do is check whether the login credentials are correct. Incorrect login details are the most common reason for this error to occur.
The wp-config.php file stores the connection details of your WordPress site. You can find this file in the root directory of your WordPress website. It contains the Database Name, MySQL database Username, MySQL database password, and MySQL database hostname.
You can either use an SFTP client to open the wp-config.php file by connecting to your website via SFTP and browsing to the root directory. Or you can open it using cPanel.
Go to “File Manager” in cPanel, open it, browse to the root of your website, right-click on it, and select Edit the File.
The wp-config.php file will open in a window containing all the credentials. Now, you have to cross-check the credentials.
To Check the Credentials
STEP I: Open MySQL Databases in cPanel.
STEP II: This is the database management page. The Database name and username are listed here.
And below this, you can find the database users and links to change the user password.
STEP III: Cross-check the Database Name, username, and password information with the wp-config.php file. If the information is wrong, you can change it.
Now, try opening your website again; if the problem persists, something is wrong. It can also be because of a corrupt database.
Also Read: How to Take Backup of Emails from cPanel
2. Repairing Corrupt Database
A database can get corrupted because of the constant change in the database by new plugins and themes. If you see ‘One or more database tables are unavailable. The database may need to be repaired on the WordPress site’s dashboard, which means the database has been corrupted. This error might only be seen at the backend and see the ‘error establishing a database connection’ on the front.
You can easily fix this error by initiating the database repair mode and adding the following to the bottom of our wp-config.php file.
Now browse to ‘https://yourdomain.com/wp-admin/maint/repair.php’ on your website. You can now see the option to repair the database or repair and optimize the database. You can choose any of the options to repair the database; we recommend choosing the repair database option since it is quicker.
An alternate way is to simply click on the ‘Repair Database’ button in the MySQL databases screen in cPanel.
After the repair is complete, remove the code line from the PHP file’s bottom.
Also Read: A Simple Guide To Connect Your cPanel Email to Gmail
3. Fixing Corrupt Files
This issue can be due to a hacker gaining access to your site or a problem with your hosting provider, or simply due to an issue while transferring files via FTP. You can easily fix this issue.
Simply replace the core version of your WordPress. You don’t have to modify or do anything to your plugins, themes, or anything on your website; you just have to reinstall WordPress. Just download a fresh copy of WordPress from WordPress.org.
Unzip the file on your computer and delete the wp-content folder and wp-config-sample.php file. Now upload the remaining files via SFTP to your website, overwriting the existing files.
All the problematic files have been replaced with fresh files. Be sure to clear your browse cache after doing this.
Also Read: How to Create a PHPinfo File and View PHP Information?
4. Database Server Issues
If the error persists, there may be a problem with the database server. In this case, you can contact your hosting provider to check if the issue is from their end.
The error may be due to too many concurrent connections to your database; the host limits the servers on how many connections are allowed at once. The database interactions can be minimized by utilizing a caching plugin.
Also Read: What is FTP & How to Create an FTP Account In cPanel?
Conclusion
This Error Establishing a Database Connection error is one of the most frustrating errors you can encounter with your WordPress website. Several possible root causes for this error make it difficult to identify and resolve it. Your site’s data is still there; you just need to find a way to fix the connection so that WordPress can access the database again.
We covered some of the most probable reasons behind this error and walked you through easy methods to fix it. We hope this article helped you understand the error and how it happens, and, most importantly, know that it’s not as severe as it seems.