How to Fix the Error Establishing a Database Connection in WordPress?

Alternatives to WordPress

If are you getting the error ‘Error established a database connection’ in your WordPress site when you are trying to open your website.

Fix the Error Establishing a Database Connection in WordPress

Then you are in the right place because, in this article, I will help you to understand what is this error and how to fix it?

Here the main thing you need to is don’t be frustrated and don’t take any action without having proper knowledge of it.

First, let’s understand what is this error and why it occurred on your website?

Why Error established a database connection error occurs?

In WordPress, all your website data like posts, comments, pages are stored into the MySQL database with the help of PHP.

Now when someone visits your website and your site makes a connection with your database and shows all posts and pages.

But sometimes because forsome reason, your website can not make a connection to your MySQL database and that time you see this error.

Many reasons cause this error, here I discussed one by one with solutions.

Note: Before starting anything make sure you have a backup of your website and database.

Check Your Database server

The first thing you need to do is to check your database server.

Sometimes you get many visitors on your website at one time and your host cannot handle that much traffic.

And your site might be down and because of it, you are getting this error.

Now if you are on shared hosting and don’t have access to admin console then contact your hosting provider, they will tell you about the server is down or okay.

And if you are on VPS and have access to admin console then follow below steps:

To check your database server status, open your admin console and login with your username and password.

Now type below command in your console and hit enter:

sudo service mysql status 

If you see something like the below image then your database is active and jump to the next section

And if you see something like below then your server is not active and we must have to activate it.

Now to active the MySQL server enter below command and hit enter.

sudo service mysql restart

After restarting your MySQL server if you can access your website and also WordPress dashboard then great you don’t need anything.

Also if you are getting this error again and again and you have to restart your MySQL server again and again then you need to add swap space in your Ubuntu to avoid this error.

And if your MySQL is active and still you getting this error then there might be another problem.

Check your wp-config File

Now your MySQL server is active but still, you are getting the same error then it might possible that your wp-config credential wrong.

To check the credentials, follow the below steps:

The first thing you need to do is login to your cPanel and open your file manager.

Now go to your website root directory and look for the wp-config.php file.

Just click on that file and click on the view button or you can also download this file in your pc.

Once you download it next open it in any text editor.

Now in this file, you will find the MySQL setting section which looks like:

Here you need to check your credentials.

  • Database Name (DB_NAME)
  • Root Username (DB_USER)
  • Root User Password (DB_PASSWORD)
  • Database Host (DB_HOST)

Make sure you recheck all the things one by one. If you found any mistake then make that changes and save the file.

Now after saving all changes upload that new wp-config into your website root directory. (Make sure you delete the old wp-config file before uploading a new one)

And check if your site is okay and working fine then great but if you’re still getting the same error then looks at the next method.

Here if you don’t know you wp-config credentials or not sure those are right or wrong then you can follow the below steps to verify it.

What if you don’t know your credentials of wp-config?

Now if you don’t know that your credentials are correct or wrong then follow below steps to verify it:

Here to verify credentials you need to create one .php file in your website root folder. (where your wp-config is located)

So, open your text editor in your computer and just copy-paste below code in you’re text editor:

$link = mysqli_connect('localhost', 'username', 'password'); 
if (!$link) { 
die('Could not connect: ' . mysqli_error()); 
echo 'Connected successfully'; 

Now save this file with testfile.php and make sure you used .php extension in the file name. (Make sure that you replaced the ‘localhost’, ‘username’ and ‘password’ as same as in your wp-config file.)

Now upload this file into the root folder of your website.

On next step visit to below URL in your browser:

Here if you get a message like ‘Connected successfully’ then your credentials are correct.

If you get a message ‘Could not connect: Access denied for user ‘username’@‘localhost’’ (using password: ‘password’) or some other error then your credentials are wrong.

Now if your credentials are wrong then you must create a new WordPress user and add it to your database.

Make Sure Your Database Hasn’t Been Corrupted

The best way to check that your database has been corrupted or not is to open your website WordPress admin panel.

If you are getting the same error then your database is okay but you are getting an error like:

“One or more database tables are unavailable. The database may need to be repaired”

This error means your database has been corrupted and you need to repair it.

Now the good news is that WordPress has a built-in way to repair your database.

You just need to activate this function. Follow the below steps to activate it.

Open your file manager into cPanel or FileZilla (If you are using it).

Go to the root folder of your website and click on the wp-config file and download it on your pc.

Now open that file in a text editor and add below line into that file.

define( ‘WP_ALLOW_REPAIR’, true );

Save the file and upload it on your website root folder. (Make sure you delete an old wp-config file or change the name of old like wp-config1.php)

Now visit below URL (Make sure your website by your domain name):

Here you should see two options: ‘Repair Database’ and ‘Repair and Optimize Database’.

You can go with any of the second ones will some more time but We recommended to go with it.

Once you repair the database makes sure you edit that file and delete that line from wp-config because anyone can access that page without any login username or password.


Here you can see that there are few ways to fixed ‘Error establishing in a database connection’ in WordPress website. I am sure that the majority of you have a problem with wp-config credentials or your MySQL server is down.

To avoid this error in the future, make sure you are using good hosting and also upgrade your hosting plan if you are getting more traffic.

Last if you like this article then make sure you share it on your WordPress group and also on your social media.

If you have any questions related to this error let us know in the comment box.

One thought on “How to Fix the Error Establishing a Database Connection in WordPress?”

  1. Well, this is nice information! Thanks for sharing this as it would help many users to know about important information about WordPress. Knowing about this, we are looking forward to implementing things accordingly.

Leave a Reply

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