One of the most shocking situation to a WordPress website owner is when your website is down. Apart from losing your traffic, the reputation and ranking of your site is also negatively impacted.
Database connection error is among the errors that make your website temporarily inaccessible. Indeed, you may have encountered this error when trying to access your site and this message was displayed “Error establishing a database connection.”
However, this article will show you how to fix this error with ease. Before that, let us learn about this error.
What does Error Establishing Database Connection Mean?
The error establishing database connection in WordPress means that the PHP code cannot connect to your MySQL database to get all information it requires to build that page. The reason why a blank page shows this error. There is no information of your site because your database is not connected.
What causes this error?
Well, you encounter this error when WordPress fails to start a database connection. Nevertheless, WordPress is unable to start a database connection for various reasons. These includes:
- Corrupted WordPress files: Your WordPress core files might corrupt because of a failed update. For example, failed updates of themes, plugins, and the primary WordPress software updates.
- Heavy traffic: Although this problem is a good one to you a site owner, it remains an issue. Indeed, your database may be unresponsive because of a high increase in traffic.For instance, if any of your posts went viral, then everyone tries to access your website. As a result, your server fails to handle some of those visits.
- When database server is down: In some instances, the web server that keeps your site files may run of a different machine instead of the database. For some reasons, this happens because your database server is down.
- Incorrect login credentials: WordPress requires a defined login and password for you to access the database. Thus, if any of those have changed, then WordPress cannot fetch anything from your database.
- Corrupted database: Your database might corrupt because of a theme failing, and a hacking attack. Alternatively, due to a rogue plugin that messes up the database internally.
How to Fix the Error Establishing a Database Connection in WordPress
NB: Before you troubleshoot this error, ensure you have created a backup for your site. Irrespective of being a tech-savvy, you should always backup your WordPress website before fixing anything.
Here are best ways to fix this error:
- Fix Corrupted Files
If troubleshooting your database did not work, then some ofyour WordPress files corrupted. Thus, you need to fix any corrupted files to this error. In fact, you can fix this, whether it started from a hacker accessing your site, an issue with transfer of files through FTP, or an issue with your host.
The corrupted files are in the primary system. Therefore, you need to replace the main version of WordPress on your website. However, you should do so with a lot of care, as a tiny mistake can delete your whole website. Before making any step, backup your data either with a help of a plugin or manually.
Now, substitute the old corrupted files with new WordPress files, by downloading a new copy of WordPress CMS from WordPress.org.
Next, unzip this file on your computer. Delete the entire wp-config file and wp-contents folder to avoid overwriting of your present wp-config file.
After that, paste the remaining files in your WordPress root folder with the help of an FTP client or File Manager. This substitutes all corrupted files with clean and fresh files. Clear your browser cache and refresh your site to check if the error is gone.
- Repair Corrupt Database
Are you seeing your website normally but you get an error whenever you try accessing the wp-admin page? Alternatively, are you getting a different error thatsays, “One or more database tables unavailable…database may need to get repaired?” If so, these are clear indicators that your database is corrupt.
Unlike the error establishing a database connection that appear on the frontend, this error displays on the backend.
However, no need to panic as you use a built-in WordPress feature to repair your database with ease. Naturally, this feature is disabled. Thus, you must enable it to repair your database. All you need is to log in your wp-admin.php file, and add this code at the bottom of your wp-config.php file.
define (‘WP_ALLOW_REPAIR’, true);
This code line enables you to repair and enhance your database. Once you add this line, go to www.yoursite.com/wp-admin/maint/repair.php. You will see a page with two options. The option to repair the database and that of repair and optimize the database. However, the repair and optimize option takes a lot of time than the repair option. Thus, if your time is limited, select the first option. In addition, you are troubleshooting an outage on your website, making it is an ideal choice as it is faster.
Furthermore, after running the repair of your database, delete the line of code that you added to your wp-config.php file. This line is not safe, and hackers can use to mess with your database.
- Change your Login Credentials
Have you repaired your database successfully but still unable to open your site? If that is the case, you should examine your database login settings in the wp-config file.
The reason why your credentials stopped working is that maybe you some vital information regarding your database was not manually update in the wp-config file. Alternatively, you changed your hosting company.
To do so, open the wp-config file and locate the database login information. Situated near the top of the file.
It displays the database name as “DB-NAME”, login username as “DB_USER”, the database host like “DB_HOST”, and login password as “DB_PASSWORD”. After checking all this information, ensure everything is correct because WordPress cannot connect to the database if one of these values is incorrect.
Nevertheless, you can use PHPMyAdmin to examine your database outside WordPress. Although the use of this tool is to access and edit MySQL databases, you should be cautious with it as works direct with the database of your website. Chances of messing things up are high.
Here is how to do it:
- Once you login from your PHPMyAdmin account, go to the list of databases on your server and click on your database. Ensure you have clicked on the database that match the name in the wp-config file.
- As a result, you will see many names of the tables in your database. To ensure you selected the right database name, search a table named wp-options and click the Browse button which next to it.
- In turn, you will head to a page where you can view the URL, name, and other settings of your WP site. Ensure the name is similar to that in the wp-config file. If not, rectify it in the wp-config file before any taking another action. That is all you need to do for the database name.
Likewise, there exist a few ways to
test if both the username and password are correct. One of them is examining if
the already in use username and password are correct. To do so, you create a
simple .php file that will test if you can connect to the database with the credentials
or not. To test, create a .php file, give it a name of your choice, and then
add this code:
This code enables you to check the connection by visiting the URL of the file in your browser. For instance, www.yoursite.com/sample-file.php. From this, your connection will either be successful or not. Unsuccessful connection means your previous login credentials are not functioning.
How to create a new database user and password
To do so, you must use MySQL Databases. Go to your cPanel>>MySQL Users>> Add New User. This takes you to a new page that requests for your new login credentials.
Choose your new password and username. After that, note them in the wp-config file, and click Create User to proceed. In the next screen, click Add User to Database, and then select your WordPress database and new username. Finally, click Add.
Next, update your wp-config file using your new password and username. This ensures that your database name, username, and password are all 100% correct.
- Contact Your Web Host Provider
If all the above methods failed to resolve your issue, get in touch with your host provider. The problem might be in your database server. For example, the error can occur if there many concurrent connections happening to your database. Reason being, servers of most hosts has limits on the number of connections authorized at once.
To reduce database interactions on your website, use a caching plugin. Use any of these top WordPress cache plugins.
Nonetheless, this issue mostly occurs on shared hosts as they use all similar resources on servers. One of the reason you should consider using managed WordPress hosts. Check WordPress Hosting Services because they can handle high traffics.
- Restore your Recent Backup
The last method to apply if needed is restoring your latest backup. Indeed, this might be a quicker way of resolving the problem in some cases. However, only if you are ready to lose some data between your last backup and now.
Even so, most hosts have their own process of restoring backup. Also, keep in mind you might require to restore both your files and database.
How to Prevent this Error in Future?
Although there is no a guaranteed way of protecting your website from error establishing a database connection, you can be 90% safe by applying these two:
- Create backups: You should always have backups of your site. To do so, install a simple plugin such as UpdraftPlus that creates automatic backups. Thus, you can restore your backup if the other methods fails.
- Quality and reliable hosting: Select a hosting provide that have optimized their WordPress working platforms. In turn, you can count on quick help and informed support. In addition, you will avoid most common WordPress issues.
Among the most frustrating errors, you can encounter in your WordPress site is Error Establishing a Database connection. However, you can fix it by utilizing the above easy-to-follow ways to fix this error.
If you have any question, comment, or suggestion, use our comments section.