Are you experiencing an “Error Establishing a database connection” while opening your WordPress? Don’t know what to do with the error? Also, you are unable to get access to WordPress due to this. Don’t worry, if you are here looking out for solutions, then you are in the right place. 

Here we have discussed the query “Error establishing a database connection- how to fix?”. But before we move on to the solution to solve this error, let’s check a brief about the error.

Some Common Scenarios that Cause this Error:

WordPress basically uses two main constituents of technology, so that it can provide the best of its features as well as abilities to you. These two constituents are PHP and MySQL. Combining both the PHP and the MySQL, WordPress works perfectly thereby provides information it needs and displays everything. And if WordPress is unable to get any information from any of the constituents, then this is where the error occurs.

Let’s see what usually causes this error, thus resulting in failure to access the WordPress any further. Below we have mentioned some of the probable causes because of which it displays “Error establishing a database connection

  • Providing incorrect credentials: While logging in, you might have provided the wrong credentials to access the database or the credentials that you have provided might have changed. Thus, WordPress is showing this error.
  • Presence of corrupted files: This error can occur when there is a presence of corrupted files in the MySQL. Even sometimes some of the core files get corrupted due to errors in MySQL. Thus, this results in failure in loading the plugins, themes and in worst cases even the WordPress.
  • The database might be corrupted: Corrupted Database can occur when there is a presence of rogue plugin in WordPress and thus this is messing with the database. Therefore, you might be facing this error.
  • The database server is down: Many users have reported that when the webserver is running on another machine, apart from the one you are using, then this error can occur.
  • A lot of traffic: This is one of the most common problems, that causes this error.  When there is a lot of traffic on the database. Therefore, resulting in unresponsive pages and thereby causing the error.

Reliable Solutions to Resolve this Database Connection Error in WordPress:

Now as you are aware of the causes behind the WordPress showing “Error establishing a database connection”, you would be obviously looking for solutions to fix the error. Go through the given below instructions till the end to fix the error.

Fix 1: Check the Database Connection Credentials

You might know, just like other network systems, WordPress uses a specific username and password to let you access the database. However, these credentials do not change by themselves. So, if you face an error when you are unable to login to the database with the provided credentials, then you can check the credentials of the login. 

However, to fix such issues, you can go through the given below instructions.

  • Usually, the fix is done in a specific file type “wp_config.php”. Then follow the given below lines;

                         define(‘DB_NAME’, ‘some_db’);

                         define(‘DB_USER’, ‘some_username’);

                         define(‘DB_PASSWORD’, ‘some_pass’);

                         define(‘DB_HOST’, ‘localhost’);

  • These lines store all the data login information to log in to the database of WordPress.
  • To get these files, you have to log in to your hosting account. Then click on the MySQL databases which are present under the database section.
  • Now, this will take you to the Database Management page. This is where you will find all the necessary details of the database such as username, password.
  • Below that, you will find the link to change the data. 
  • Thereafter, you have to change the information of the username, password, etc.
  • Then try to visit again and provide the new credentials of the database.
  • Lastly, check if the error is fixed or not.

However, if you still see the error, then there can be any other error that is causing the problem so you can go through the below-given instructions to solve the error.

Fix 2: Connect with the Web Hosting Provider

The first thing that you need to check is whether your web hoster is working or not. If you are a quality Host provider, then you can get a quick response. Go to the host provider’s website, then click on the Contact button. Further, click on the bar, copy and paste the “error establishing a database connection”. After that, you will be left with two options:

  • Whether the database server is up and running with no problems.
  • Or if there is no traffic activity at that moment.

However, as soon as you tell them about the error, they will tell you the reason behind the issue. or they can even fix the issue for you. Apart from that, they will let you know if there is an error with the database or if there is any suspicious activity on the server of the database, which is very important to know. 

Thereafter, if you find that the fault is not with their web hosting, then you can move to the next solution.

Fix 3: Check the Plugins, Themes or Other files

There can be a lot of misfortune events that can lead to this error. Many users have reported this error is also shown when there is an error with the plugins or themes and thus failing the page to load, thereby causing this error. Although, if you don’t have any plugins or themes installed in the database, then you can skip this step.

  • First, you have to connect to your host provider via the FTP.
  • Provide your credentials and thereby log in to the FTP account.

(you can check your mail if you don’t remember login credentials)

  • Once you have logged in, now navigate to the WordPress root folder. It is basically named “public_html“ or ”www “.
  • Now, locate the folder with the names “wp_content” and “wp_admin”. Next, open the “wp_content” folder.
  • Once the folder is open, then right-click on “plugins” and click on Rename. Then rename the folder to any name but plugins.
  • After that, go back to the site and check if the error exists or not.
  • If the error is still not fixed, then again, go back to the FTP and thereby rename the folder to plugin again. 
  • Open the plugin folder and change all the names of folders each and every one of them.

( You can simply use an underscore at the end) 

  • Thereafter, go back and check if the site is opening or if it is still showing the error. 
  • Now, if you see the site is working fine, then you have to remove the plugin from the database of WordPress.

Fix 4: Repair the Database

You might be aware of the fact that the database of WordPress is quite a complex thing. And there are a lot of complex things that are needed to work perfectly to ensure that the database is working fine. Therefore, if anything goes out of place, it will cause this database connection error.

  • The first thing you need to do is to access your WordPress dashboard by providing the credentials. Hit to the given below link

                        YOURSITE.com/wp-admin

  • Then when the window opens and you see something different. After that, check what are the solutions that are being provided by the WordPress to resolve the issue. As the new WordPress is advanced and will provide problem-solving solutions when you are facing any error.

And if you see it is showing as “One or more database tables are unavailable”, then you have to repair the database.

  • To repair the database, the steps are very easy. Simply log into the FTP of the database.
  • Hit the WordPress root folder and then just download the “wp_config.php” file on the desktop.
  • Once the downloaded, open the file in notepad and add the given below line in the end:

                             define(‘WP_ALLOW_REPAIR’, true)

  • Thereafter, upload the file on the site again by overwriting the previous one.
  • Now go back to the website and check if the error still exists or not. If you still see the error, then you have to put the previous “wp_config.php” file without the line. Thereby, try out the next step.

Additional Solutions that you can Try:

If the above-mentioned troubleshooting solutions did not work, then you can go for the given below solutions as according to many users these solutions have solved the error.

  • Update the Site URL

You can opt to update the WordPress URL by using the phpMyAdmin. Open the Hosting account dashboard, then go to the phpMyAdmin and select the WordPress database. Thereafter, click on the SQL menu. After that, you have to enter the given following in the MySQL query.

“UPDATE wp_options SET option_value=’YOUR_SITE_URL’ WHERE option_name=’siteurl’”

In the “YOUR_SITE_URL” provide your URL, then change the wp_options as per your own tables name.

  • Reboot the Web Server

According to many users when they reboot the Web Server, the error will get fixed. So if you are on a dedicated server, local servers, or virtual private server, then you can choose to reboot it. However, you can try to reboot the services as well. This process will restart the web as well as the database server. Now, this will fix all the errors or any temporary glitches.