Skip to main content

Fix error establishing a database connection in WordPress

2022 Updated

Key Point

Have you encountered website screen saying error establishing a WordPress database connection? We will use the below command.


 
define('WP_ALLOW_REPAIR', true);

We are explaining here the best and most effective step to easily recover the website and fix your WordPress database connection.

What causes Error Establishing a Database Connection in WordPress?

The error that you see on the website that says ‘Error Establishing a database connection’ is usually caused by incorrect database information in your WordPress. Now, you may be thinking that you have not changed any settings. This can also happen when you update/install new plugins or have made some changes lately.

Simply, the database can be corrupt.

fix database connection error

We are only using 1 simple step that solves the problem for most users.

There are many solutions on the internet and there are actually many ways to fix this as well but we will be covering one single method which works for most of the cases to fix ‘ Error Establishing Database connection’.

Access Wp-Config.php file

For this solution you will need to access the wp-config file. You can do this by either Cpanel or by SSH.

Step 1: Open wp-config file in editor

On the top where the code starts, add the following line of code


define(‘WP_ALLOW_REPAIR’, true);


Step 2: Save wp-config

Save the wp-config file. This will do the trick but I recommend you restart your server if possible.

Step 3: Go to your website

Go to your website URL and add the below syntax in the end. Replace ‘yoursite.com’ with your website URL


http://www.yoursite.com/wp-admin/maint/repair.php


Step 4: Choose a repair option

Now you will see the options as shown in the image below. Just choose one of the options, and it will show you some passed database checks.

Repair wordpress database connection

After this is done. Just simply visit your website and this should have fixed your ‘Error establishing database connection’ issue in WordPress.

Step 5: Remove the code you added (IMPORTANT)

 Please go back to the wp-config file and remove the code we added just now. This ensures no one will be able to access your database and maintain security of your WordPress website.

Please let me know if this was helpful.  You can leave a comment below if this worked. 👍

 

 

Our Recent Writings

shivam sagar

Shiv has been working at Brandviser Melbourne as a lead web solution and marketing consultant.

Leave a Reply