How To Fix Error Establishing A Database Connection

Best way to Repair Database & Optimize Database

Error Establishing A Database Connection is an error that occurs when WordPress is unable to make a connection to the Database. Several things can affect your WordPress database connection, making it a bit difficult for beginners to troubleshoot. But this article will help you resolve it and get your site back online.

All of the data on your WordPress website, such as posts, pages, plugin settings, meta information, etc…, are stored in your MySQL database. The only data that are stored in disk are media content such as images and your theme/plugin and core files of your website, such as index.php, wp-admin.php, etc. When someone visits your website, PHP will execute the code on the page and queries the Database for relevant  information. And then display it to the visitor in their browser.

For fixing these types of issues, you need to access your wp-config.php file, which contains your WordPress configuration & installation settings.  You can find the wp-config.php file in your WordPress root directory. For accessing that, you need to login into your cPanel & then move to the File Manager, as follows.


Once you reach there, then navigate to the WordPress root folder as shown in the following screenshot.

file manager

Now, move to the public-html folder, then you will be able to find the wp-config.php file there. Right-click on that file and click edit, then it will open the wp-config.php file in the code editor. 

After that, add the following code to the wp-config.php file.

define ( 'WP_ALLOW_REPAIR' , true); 

Once you enter this line, you will be able to optimize and repair your database. To do that, move to “” link. Replace “yourwebsite” with your actual URL.

Once you type this URL in your address bar, you will see a whole new window, as shown in the screenshot below.

Error Establishing A Database Connection fix

Here, you will see two options – Repair Database & Repair and Optimize Database. You can choose an option. However, remember that optimization will take a little bit more time.

One thing you should keep in your mind is that database repair is not secure. Without even log in to your website, anyone will be able to access the repair database URL.

Therefore, once you’re done with database repair, be sure to remove the line of code that you’ve added to the wp-config.php file. Once you remove that code, it will prevent the users from accessing the repair page. Hope this will help solve your Error Establishing A Database Connection. If it doesn’t try creating a topic on our WordPress support forum and someone will help you.

Darshana Tharanga

I’m Darshana. A Computer Network undergraduate based in Colombo, Lk, who enjoys building things that live on the internet. I develop exceptional websites, web apps, and web content.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

We use cookies to give you the best online experience. By agreeing you accept the use of cookies in accordance with our cookie policy.