Connect with us

How to Fix “Error Establishing a Database Connection” in WordPress

solution

How to Fix “Error Establishing a Database Connection” in WordPress

Are you irritated with the “Error establishing a database connection” error in WordPress website? Are you tired of surfing better solutions to solve it? Don’t worry. This time you are at the right place. This article provides you the best solutions to get rid of WordPress database connection error.

Every website has a database which stores and organizes all the data related to it. If you parse a URL, then the requested data will be fetched from the database and presented on your screen. But sometimes in WordPress, if you enter your website URL, a white screen appears with an error message “Error establishing a database connection”.

This error appears when your website is unable to connect to your database. Solving this error may be difficult for beginners. But don’t feel tense as we are here to guide you.

How “Error Establishing a Database Connection” can be fixed?

Whenever you add a new post or a page to your WordPress site, those details are noted in your MYSQL database. Elements like widgets, font, title, date, etc. used in that post are also notified. There will be a specific PHP code for retrieving all the elements needed to build the page.

If PHP code failed to retrieve data from the database then, a page with database connection error appears. In addition to this, there are many reasons for the cause of establishing a database connection error. Let us Examine one by one.

Reason 1: Incorrect Login credentials

To enter into any website, Username and Password plays a crucial role. Changes made to these credential may affect the communication with your database. If the other credentials like database name and database host are specified incorrectly, then the website fails to connect to your database. This failure will raise an error while connecting database. It also leads to some wp-admin problems.

Reason 2: Database server unavailable

Sometimes server may go down because of the heavy traffic or when failed to support the new connection or may be due to some internet problems. In any of  these cases, the server fails to perform well . This  will raise a database connection error.

Reason 3: corrupted database

Sometimes your website front end works well, but you will get an error “One or more database tables are unavailable, the database may need to be repaired” on the backend. This error message is the best indication to confirm that your database had been corrupted. Because if the database gets corrupted, then the data in it are also infected. This database corruption leads to the loss of data tables.

Reason 4: Incompatible plugins

Some Bad plugins, cause damage to the database. These plugins result in the destruction of information in the database and encounter an error establishing a database connection. The damaged plugins can be the root cause for other WordPress errors also.

Reason 5: Corrupted files

WordPress root directory is a top most directory which holds all the files of WordPress. Some files in this directory may get corrupted and  cause an error. The file permissions also stand responsible for the cause of error establishing a database connection error which is one of the most occurring WordPress issues.

Reason 6: Forgot to update wp-cofig.php file

Frequently changing the password is a best practice to get rid of Security attacks. But whenever you change your database password, the wp-config.php file need to be updated. If you forget to update then, this will be one of the reasons to get the error.

Related:  Easy Ways to Fix Facebook Incorrect Thumbnail Issue

Solutions for the Establishing a Database Connection Error

BACKUP BEFORE SOLVING

Never forget to backup your WordPress website. Whenever you decide to do modifications especially in the database, you should maintain a good backup. we make changes to site data directly from PHPMyAdmin in the database. Any mistake made by you at this place can collapse your entire website. It is always a better practice to use the Best plugin for backup.

Solution 1: Check Login Credentials

Step 1:

The wp-config.php file stores all the credential data. So open your wp-config.php and check for the below code whether these values are set correct or not.

1define('DB_NAME', 'database-name');
2
3define('DB_USER', 'database-username');
4
5define('DB_PASSWORD', 'database-password');
6
7define('DB_HOST', 'localhost');

If any one of these credentials were set wrong then, your WP website cannot connect to its database. Make sure they are correctly defined. If all your credentials are not a reason, then try step 2.

Step 2:

Now it’s time to check your site URL. You can access your database from your Cpanel dashboard through a feature called PHPMyAdmin. Login here and you will find the database list on your left-hand side. Find your database name (which you specified in wp-config.php). If you could not find database then, the problem is with the host server.

If you find your database, give a click on it. You will see a list of tables on the screen. Click on the Browse button beside the wp-options table. Check URL and other settings. If they match with your wp-config entry then, your database is correct. If you find URL correct and other settings wrong then, name them as per the wp-config entry. You can update your URL using this line of code.

1UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

If all these are crystal clear, then go to step3.

Step 3:

Here you need to check your Username and password. For this, we have two methods.

METHOD 1: create a simple .php file as follows

1<?php $link = mysql_connect('localhost', 'root', 'password');
2
3if (!$link)
4{
5
6die('Could not connect: ' . mysql_error());
7
8}
9
10echo 'Connected successfully'; mysql_close($link);
11
12?>

Save this file to your WordPress directory as “connectiontest.php”. Enter your URL in the browser. If a message connected successfully appears then, your username and password are correct. If the message appears like Could not connect thenchange username and password as per wp-config.php entry.

METHOD 2: You can change your username and password from Cpanel->MYSQL databases->MYSQL user->Add new user. Set your credentials according to wp-config.php and select “create user.” Now click on “Add user to database” option. With this, you can overcome the error establishing a database connection.

Solution 2: Repair your Database

When your database damages, frontend works good but the backend may not. Sometimes it may be Vise-Versa. If this happens, You will get an error message telling you to repair your database. For avoiding this, you need not spend much time on tutorials. WordPress provides you an easy way to recover your database.

Log in to your account through Cpanel. Click on FileManager->WP root directory->wp-config file. Edit this file and add the below line.

Re-Upload the file and give URL as http://yoursite.com/wp-admin/maint/repair.php. Now you will see a page as shown below

Choose “Repair Database.” Optimizing your database at this point is not probably a problem. If you do this, this may lead to other problems. So better avoid choosing Repair and Optimise Database option. After the repair is done well, run your WordPress site URL. If you can see your website working well, congrats you are done.

If you still facing the same problem, you should step into next process.

Solution 3: Replace Corrupted core files

Your database is OK and all the login credentials sync with your wp-config.php entries. So the problem is possibly due to a bad file. Sometimes Re-uploading your WordPress core files can quickly fix your problem. Also Resetting File Permissions or Replacing the core files can solve the issue Error establishing a database connection.

To reset the files in a single step, Download the latest version of WordPress. Unzip the files on your system and delete wp-cofig.php and wp-content.php. Now through Cpanel login, Upload the remaining files to the WordPress RootDirectory.

This will resolve your Problem if the error is due to the Bad file. If this is not the reason, scroll down to find the other solution.

Solution 4: Remove the bad plugin

Sometimes it might be a bad plugin which causes this error. So try to disable all the plugins and re-activate one by one until the error page disappears. To disable you plugins, you have to log in to Cpanel and select PHPMyAdmin. Open wp-options. Find active plugins.

click on EDIT to see the list of plugins like below

For a reference, copy this output to a text editor and delete all the plugins. Now enter the URL. If you can see your Dashboard, your War is over. Through dashboard Activate the plugins you want.

If you are still struggling with Database Connection Error page, Don’t get frustration. Still, you have hope.

Solution 5: Troubleshoot your server

If you are done with all the above mentioned solutions and still you are facing the Error establishing a database connection problem then, Server is the culprit. Usually, server problem arises when it is struck by heavy traffic. If this is the case, wait for a while and reload the page.

If it is due to the malfunction of your server then, check out the host number and give a call. Making any changes by yourself can affect the entire website. So it is always a better idea to have your hosting support team to solve the server side errors in WordPress

Conclusion:

We hope this article helped you a lot in solving database connection establishing error. Enjoy surfing your WordPress Website. For more information about the WordPress errors and to get more suggestions for your WordPress site, keep following our website.

Continue Reading
You may also like...

We are a WordPress plugin developer company that focuses on useful WordPress plugin creation and empowering people to earn passive incomes from their blogs. We build unique and groundbreaking plugins that will revolutionize your blog!

Click to comment

Leave a Reply

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

More in solution

    Sign up for our newsletter and get a free affiliate e-book!


    About Me:

    Szabi Kisded

    Hi, my name is Szabi and I'm documenting my journey selling plugins on CodeCanyon. I will show you every step of it: learning to code, plugin ideas, WordPress stuff and more. Read more…

    Mega Plugin Bundle:

    CodeCanyon Portfolio:

    Learn to Code Plugins:

    Latest Promotions:

    Recommended Theme:

    Start Your Own Blog:

    Recommended VPN:

    Translate Your Blog:

    AdSense Alternative:

    Best Article Spinner:

    Popular Posts:

    Latest Posts:

    To Top