Imagine you’re having a nice cup of morning coffee, and you open up your laptop to browse your WordPress site. Suddenly, you see this disappointing message that says, “error establishing a database connection.”
You can see none of your website content and only this scary sentence staring at your face. What are you going to do?
If you have the same problem, we can safely say that you’re in the right place for a solution. In this article, we’re going to talk about five easy steps that will troubleshoot this issue.
Table of Contents
- Get to Know The Error Establishing a Database Connection Issue!
- Common Scenarios That Causes Error Establishing a Database Connection
- How to Resolve Error Establishing a Database Connection Issue?
- Wrapping Up
Get to Know The Error Establishing a Database Connection Issue!
Before troubleshooting this issue, you have to understand the basic principle of how databases work in WordPress.
WordPress saves all the website information on MySQL database technology. Examples of your site files include pages, posts, title, widgets layout, color settings, typography, and more.
To access all these website data, WordPress employs PHP programming language. PHP retrieves the site’s information from the database whenever you enter a URL on the browser.
WordPress requires specific information to connect with the database. Some of them are the database name, username, password, and server. It stores all of this information in the wp-config.php configuration file.
You get an error when WordPress cannot initiate a connection between the PHP command and MySQL database. Since there is no website file to display on your screen, it merely shows the message “Error establishing a database connection.”
Common Scenarios That Causes Error Establishing a Database Connection
You already know how WordPress relies on databases to load your site, right? It's time to grasp what causes the error that prevents PHP from connecting to the MySQL system.
Here are five common reasons why you get the error while trying to establish a connection with the database.
1. Login Credentials are Incorrect
In the previous section, we’ve mentioned that WordPress requires certain database information to log in to the MySQL system. It includes credentials like username and password.
Any change of these credentials will prevent WordPress from accessing the database and displaying the website content.
2. WordPress Files are Corrupted
WordPress cannot access database files because there can be a possible chance that the files are corrupted. Several reasons why files can get corrupted include failed update of themes, plugins, software, and more.
3. Database is Corrupted
Besides external factors, there could be an internal issue with the MySQL database as well. For instance, it can result in lousy performance due to an outdated plugin or a failing theme. The database could get corrupted due to hackers’ attacks as well.
4. Database Server is Down
Another reason for WordPress not building a connection between PHP and MySQL database is due to server issues. Such issues occur when the webserver and database server are different.
The system that hosts your website is called the webserver. If there’s an issue with the other server, WordPress cannot access files from that system. The server might be down due to several reasons, like a system crash.
5. Heavy Web Traffic
Suppose your content went viral recently, and now everyone wants to access your site. When a website gets a sudden traffic spike, the database system can not handle such high requests sometimes.
So, this is a possible cause of a connection gap between PHP and MySQL. The database can be unresponsive to high traffic, which can lead to the server crashing.
How to Resolve Error Establishing a Database Connection Issue?
We present to you the five most common approaches for solving the database connection-related issue. You may find most of these steps very simple. But few of them are demanding and require the knowledge of coding as well.
1. Check Your Database Login Credentials
The first step regarding the database issue is to check and verify the database credentials. If you’ve mistakenly changed this information, then MySQL will prevent WordPress from providing access.
Also, the data might change itself when you alter the hosting environment or make other changes to the site. In such cases, WordPress will save incorrect details to its configuration file (wp-config.php).
So, to correct this situation, you have to open up the wp-config.php file via ftp. There are four lines of programming codes that represent database login credentials. The codes are:
Now you have to check the above data with the actual ones stored in the database. For this process, go to the Database section inside your hosting control panel.
If you find that these data do not match each other, you have to edit the WordPress configuration file. Then, save the changes. You have to check the URL on yoursite.com/check.php the update status.
After you save the changes, you will see a message that says, “connected successfully.” You have resolved the login credential issue. If that’s the primary issue, you won’t get the ‘error establishing a database connection’ anymore.
2. Repair WordPress Corrupted Database
If the above method didn’t work for you, try logging in to your WordPress dashboard. WordPress will send you a message about the exact issue. If it shows the same “error establishing a database connection” message, then ignore this step.
However, if you see messages, like “One or more database tables are vulnerable,” you will have to repair your database.
You have to copy a certain programming code before proceeding. The code is:
WordPress automatically repairs the issue when you place it on the File Manager. For getting access to the File Manager, you have to log in to the Control Panel.
Then, open the File Manager and navigate for the WordPress configuration file (wp-config.php). Scroll down to the bottom, and you will find a sentence that reads:
/* That’s all, stop editing! Happy blogging. */
Paste the copied programming code in this file, right above the last sentence. Save your changes after that.
Now go to the URL yoursite.com/wp-admin/maint/repair.php. Two messages will appear on your browser, asking your permission to repair the database. The first button only improves the database, while the second one both repairs and optimizes it.
WordPress will repair the issue after you select any one of these buttons. Now go back to the website and check to see if it works.
However, don’t stop there. You still have to remove the previous code from the configuration file. So, open up wp-config.php and remove the code “define(‘WP_ALLOW_REPAIR’, true);” that you inserted earlier.
3. Check for a Corrupted Plugin or Theme Files
WordPress also might not build up a connection with the database by using corrupted plugins and themes.
There are a few reasons for it. Suppose you just updated a plugin or a theme, and it got collapsed due to some technical error. You might’ve also edited some of its code, and it got mismatched.
So, if you’re dealing with a corrupt plugin/theme, you have to connect to the host using FTP. You can perform this step using the FileZilla tool.
First, you have to log in to your FTP account. You can contact the web hosting provider if you don’t have the username and password. After you get access, connect to the server and find the WordPress root folder.
The root folder includes subfolders like “wp-content” and files like “wp-config.php”. Enter the wp-content folder, and you can find a “plugins” subfolder. Change its name to something else, like “plugins123,” and go back to see your site.
Is the error establishing a database connection still there?
If no, then there’s something wrong with one of the plugins. Go back to FTP and change the name back to “plugins.” Now enter that folder and change the name of each plugin one by one.
Also, check the status of your website every time you change the name. Once you find a particular plugin that is causing the trouble, search online to solve it. Else, you can delete it and re-install the updated version.
The same process applies to themes as well. If it is not the plugins causing the issue, follow the same process with all the subfolders inside the “themes” folder.
4. Restore the Default WordPress Files
In case none of the previous issues is the culprit, then there’s one more possibility. The core WordPress files could’ve gotten corrupted. There might be several hacking attempts that caused this issue. Or, it could be due to file modification by accident.
You can solve this issue by deleting the current files and restoring the default WordPress content. But, this process involves risk. You should back up your entire site before proceeding to this irreversible measure.
Now, download a newer version of WordPress and unzip its file. Locate and delete the wp-content folder and the wp-config.php file within the unzipped folder.
Go to cPanel and open File Manager. Simply upload the latest WordPress file to override all the previous content. Since you’ve already deleted wp-content and wp-config.php, you don’t have to worry about losing important site data.
Check the performance of the website. If it is still not working, then move on to the final step.
5. Contact the Web Hosting Provider
The final step is to contact the hosting provider regarding the issue. You can state your concern in different ways, such as live chat, phone call, or an email.
They can find out that your database might be getting a traffic spike. Else, the server might be down due to some internal failures.
We recommend you state the entire issue with their support team. With this method, the support team can troubleshoot the problem and solve it if the unit is capable of it.
Were our solutions helpful?
Usually, a minor error can waste a lot of time and effort. So, make sure you avoid this situation in the future.
To prevent your database from server crashes, opt for a premium quality hosting solution. You will not only get a fast and reliable service but also get help from their support team.
Additionally, make multiple backup files for your WordPress site. Some backup tools act automatically in case of an internal failure. Therefore, we recommend you to stay on a safe side when it comes to your website’s performance.
Also, check out our article on How to Fix WordPress Internal Server Error.