Published on 14th Sep 2019 by Mr. Maregedze || Edited on 14th Sep 2019. 210
This tutorial will help you if your WordPress website suddenly began to show "Error Establishing a Database Connection" while it had been working for several months or even years. When this happens to your website do not quickly get worried, I have written this guide to help you know the causes and how to fix them. However, if you are not sure or too busy to do it on your own, simply get in touch with me here so that I do it for you.
1) Wrong Database Credentials
You may wonder how really some simple mistakes can result in a day's problem. You may have mistakenly changed your database password in error while following a tutorial on the internet for doing something cool. Once the password specified in wp-config.php is not correct your site will stop working immediately. If the database prefix has changed your site will stop working again.
It is common your busy website with many plugins to have its tables corrupted but not really damaged in any way. Database corruption may be a result of changes in the MySQL server itself. When this happens your site will fail and stop loading immediately.
Usually WordPress plugins may conflict with each other. This also happens with themes in your installation. When a certain plugin is failing to work with another, it will give your WordPress an impression that your tables are corrupt in someway. That is how your site goes offline immediately.
4) Missing Database Tables
It is possible your tables may go missing due to several reasons. Your site may be infested with a virus which then starts eating out important tables. Plugin conflicts may also cause such problems to occur. You may have deleted or modified a table while following a tutorial which you did not fully comprehend. Once a WordPress database table is modified or is deleted the whole website will go offline.
5) MySQL Server is Offline
Your database server may actually be offline when you try to reach it throug your website. If MySQL server fails to start or is blocked by a firewall, it means all applications on that server will have that connection terminated thus causing your WordPress website to go offline.
It is always a good idea to start with what you suspect when troubleshooting "Error Establishing a Database Connection" in WordPress website. If you are not certain what may be causing this error in your case just follow these steps to solve your problem.
1) Fixing Wrong Database Credentials
This may sound straightforward, but not to everyone. Open your your installation files and look for a wp-config.php file. In that file scroll down until you find lines with server credentials. Make sure those details are correct as expected. If you see anything strange, that could be the problem. Below is a sample wp-config.php file that has been extracted to show only MySQL lines:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
/** MySQL database username */
/** MySQL database password */
/** MySQL hostname */
/** Database Charset to use in creating database tables. */
/** The Database Collate type. Don't change this if in doubt. */
Those are the lines specifying your database login credentials. Make necessary adjustments if you remember making changes to the old credentials at all.
2) Fixing Corrupt Database Tables
In most case when your site goes offline with this error, it would be a corrupt database table. Solving is is very easy and straight forward. Open phpMyAdmin on the server where your database is stored. Select all tables in your database and run repair. Simple! You should always try running your site every time you finish each stage. If your problem was database corruption you are done with your fixing.
3) Fixing Conflicts in Themes or Plugins
You may have noticed this problem after an upgrade or a new installation of a plugin or theme. Perhaps you enabled auto upgrade so you won't know which plugin is destroying your site. The easiest way is to deactivate all plugins. If your site loads without all plugins it means the problem is in your plugins. To disable a plugin or theme simply rename it in /wp-content/plugins/ folder or /wp-content/themes/ folder respectively. Activate your plugins or themes one after another through your WordPress dashboard. Take time to test your site every time you enable your next plugin or theme. This will help you to do away with the problematic software in your site.
4) Fixing Missing Database Tables
This is a disaster! If you find your database missing some tables your site may never look the same anymore. WordPress is powered by database to display content. If the relevant tables containing that information are missing then you should not expect to see your site back as it was before. This problem can only be solved by running a backup of your entire installation. A WordPress website has at least 11 tables by default which are required for basic site loading even without content. Unfortunately, you can not always be successful if you replace missing tables by exporting from another site.
5) Fixing Offline or Unreachable Database
If your MySQL server is just offline or not reachable you may need to contact your hosting company for more help. Normally if you are on a shared server there's really nothing you can do to turn ON your MySQL server for your WordPress site. If you manage your MySQL server try restarting it, check if Apache or the web-server you are running is allowed to access the database server. Additionally, check if your database user has been granted enough privileges to modify or use data in your database.
If you still can not bring your site back online after following this guide I would recommend you to look for an expert or simply send me a message for a prompt assistance.
In all circumstances, prevention is always the best cure. There are just a few safe tricks you should always maintain for your site to remain working every time. Do not install nulled or cracked scripts as that will expose you to infested software. Do not directly modify files. Never edit core files in your WordPress installation. Always use security plugins. You must scan your website regularly. Use a secure password. Do not install outdated plugins. Always upgrade to the latest stable version. BUT After-All remember to keep a latest backup!
This article was written by Masimba Maregedze. If you would like more information or help with your website simply get in touch with me by sending a message using a form on contact page. You can as well comment on this post for general information on this topic.
Tutorial || Masimba Maregedze 210 2
If you have any suggestions or need help do not forget to comment here or sent a message to the author.
Masimba Maregedze on 14th Sep 2019