You might have encounter this error when entering the URL of your WordPress site and nothing appears other than this error message: “Error establishing a database connection.” It means the database connection error happens. In this tutorial, we will show you how to fix this error very easily way.
Why Does the Error Establishing a Database Connection Error Occur?
For some reason or another, the PHP code was unable to connect to your MySQL database to retrieve the information it needs to fully build that page. That’s why the error is shown on a blank page.
Now that we have understood the meaning of the error, we need to find out the reasons.
- Incorrect Login Credentials – It can be because of the wrong login username and password being used to access the admin panel. Changes to your database login details can “block” your website from accessing the database. So firstly you should be to confirm that the login credentials you’re entering are accurate.
- Unresponsive database server – the server that hosts your database may temporarily be down.
- Corrupted WordPress files
- A corrupted database – The WordPress database can be corrupted by a number of things like installing a faulty or incompatible plugin.
How to Fix Error Establishing a Database Connection Error
Before fixing the error, we need to determine exactly what is causing the error. If we can determine the root of our error, then getting a viable solution is a matter of when.
1. Is Your WordPress Database Corrupted?
First we need to check whether the error is as a result of a corrupted database. For this, we have to test if the error has crippled the backend.
Entering the backend URL: yoursite.com/wp-admin/ and you get an error saying “One or more database tables are unavailable. The database may need to be repaired.” These are clear signs that your database is corrupt.
Don’t worry , your database can easily be repaired using a built-in WordPress feature.You can be done in three simple steps.
Step 1: Login to your cPanel, and navigate to File Manager.
Navigate to your WordPress root folder. From this point, locating your wp-config.php file should be easy:
Step 2: Access to wp-config.php, open it in editing mode. Right click on the file to choose the code editor, or select code editor in the menu:
This will open your wp-config.php in a new tab. Add the following code at the bottom:
define( 'WP_ALLOW_REPAIR', true );
Save your changes.
This simple code of line allows you to repair and optimize your database. After adding this line, simply navigate to www.yoursite.com/wp-admin/maint/repair.php. This should open the following screen:
There are two options. The Repair and Optimize error takes more time than the simple Repair option so if you’re short on time then simply choose the first option.
Once your database is repaired, remember to delete the code you added to your wp-config.php file. Otherwise, you risk granting hackers access to your database, where they will wreck havoc.
2. Change Login Credentials
Still no luck on opening your website successfully even after repairing the database? Let’s check whether your database login details are the problem.
Open your wp-config.php once again and look for the following lines of code (it will probably be near the top of the file.)
// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', 'database_name'); /** MySQL database username */ define('DB_USER', 'database_user'); /** MySQL database password */ define('DB_PASSWORD', 'database_password'); /** MySQL hostname */ define('DB_HOST', 'localhost');
It shows the name of the database as “DB_NAME”, the login username as “DB_USER”, the login password as “DB_PASSWORD”, and the database host as “DB_HOST”. After going through all of this information, do make sure everything is correct. If any of these are incorrect, you will get the “error establishing database connection” error.
At this juncture, all you have to do is counter-check these credentials against those stored in your database.
You can check your database outside of WordPress by using PHPMyAdmin which is basically a tool for accessing and editing MySQL databases. But be careful when using this tool as it deals directly with your site’s database – there’s a great chance of messing things up.
If you are not a very experienced developer then I suggest you contact your web host to solve this error.
3. Fix Corrupted Files
If completely troubleshooting your database doesn’t even work, the last thing you can try is to fix any corrupted files in WordPress. Resolving this error simply by restoring the WordPress files. You need to be careful here because a tiny mistake can wipe away your entire site. First, make sure you back up your entire WordPress site before proceeding.
Download a fresh copy of the WordPress from WordPress.org.
Unzip it in your computer and delete wp-config.php as well as the wp-contents folder so there’s no chance of overwriting your current wp-config file. It will also ensure you don’t lose the themes, plugins and media stored in wp-content once you restore WordPress.
Once you have this covered, paste the rest of the files into your WordPress root folder using FTP or an FTP client. This should replace all problematic files.
This will replace the corrupted files or the files which are causing the error. By following these simple steps you’ll be able to easily fix the Error Establishing a Database Connection error.
Try reloading your site, and if you’re still seeing the error, first try cleaning your browser’s cache. If the error persists after cleaning your cache, please contact your web hosting service’s customer support because it’s entirely end.
Wrapping It Up
This Error Establishing a Database Connection error is one of the most frustrating errors you can encounter with your WordPress website. We covered some of the most probable reasons behind this error and walked you through three easy-to-follow methods to fix it.
If you get difficult to fix this error, feel free to contact us. We are also always here to help you.