How To Fix The Error Establishing A Database Connection [4 Easy Ways]

A common error on websites is when the database refuses to communicate with WordPress. The site loses its functionality as anyone visiting it gets an Error Establishing A Database Connection message.

This can happen when the database username or password has changed or if it becomes corrupted. This is a simple issue that can be resolved within minutes. But if the site continues in this state, you will lose valuable visitors and traffic. Thus, it leads to fatal damage to your business, sales, and revenue.

Nobody wants that, right? Read ahead to get your WordPress blog or website to connect with the database again.

Error Establishing a Database Connection

What is Error Connecting to Database in WordPress?

Error Establishing a Database Connection in WordPress occurs when the website fails to connect to the database and retrieve the content to show you.

Error Establishing a Database Connection in WordPress

Let me simplify this for you.

When we enter any website address on our browser, we see a beautifully displayed interface. Nothing complex or cryptic stuff, just a simple webpage.

But underneath the hood, it’s actually some logical representation of data perfectly stored in a database. Every time you hit the site address, WordPress connects to the database and fetches the data to display the website you see on the browser.

However, to connect to a database, WordPress needs some information such as:

  • Your database name
  • User name
  • Password, and
  • The database server address.

WordPress stores all of these data in a WordPress configuration file named wp-config.php. This file is located in the root directory of the WordPress installation. Most of the time, incorrect information in this file leads to error establishing a database connection. Such as the wrong username or password.

Corruption of the main database is less common but can happen nonetheless.

However, your site’s visitors may not notice this error right away. This is due to the CDN server caching the site’s old contents. When the cache refreshes, they will see this and may start complaining.

So, before that happens, it’s better to resolve this issue.

4 Easy Ways To Fix Error Establishing A Database Connection In WordPress

Sometimes, the WordPress dashboard becomes inaccessible when the database connection breaks. So, you can’t fix this issue from the wp admin panel. Instead, use your hosting provider’s dashboard to check the database issues.

Before starting the troubleshooting steps, make sure you are logged in to the hosting account. Then proceed to the steps below:

1. Check if your Database Server is Online

While dealing with a database error, check whether the hosting server is running smoothly or has gone offline!

During sustained load, it’s normal for servers to crash, resulting in sites not working. And for users in a shared hosting plan, this is a very common thing to happen. Maybe one of the sites on the same server had a spike in traffic, resulting in downtime for the entire bunch.

So, the first action would be to contact the hosting provider and see if they can answer what’s gone wrong. If the culprit was a crashed server, you just need to wait it out for some time before your site comes online.

Next, run a PHP script to test if WordPress can communicate with the database. Also, if the credentials are not okay, we will know that, too.

To do that, paste the following code into a text file and save it as a dbconnection.php file on your computer. Replace the username and password with your database credentials. Then, upload it inside the public_html folder present in your site’s directory.

<?php
$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysqli_error());
}
echo ‘Connected successfully’;
mysqli_close($link);
?>

Once the script is uploaded, run it by going to www.yourdomain.com/dbconnection.php from your browser. If you see the Connected successfully message, that means WordPress and the database can communicate successfully.

But, if you get an error message instead, there is some problem with the database username and password on the wp-config file. We will deal with that in the next step.

2. Check Your WordPress Database Credentials

The most common reason behind “error establishing a database connection” is an incorrect username and password.

Now, let’s check if the credentials are correct.

WordPress stores the database username and password in the file named wp-config.php. This is part of the core WordPress files that are present on the hosting servers. Meanwhile, all the databases and their configurations are presented in a separate section on the Hosting dashboard.

Check the username and password from both locations and see if they are the same.

To do that, go to the wp-config.php file that is present under the public_html folder. You can also open this using the Hosting dashboard’s default File Manager.

Note: Back up the wp-config file before making any changes to it. This will come in handy if something goes wrong later on.

Once you find it, double-click to open the file. The saved database username and passwords are present in the following lines:

wp-config.php file

Take note of the values next to DB_NAME, DB_USER, and DB_PASSWORD. For your convenience, you can copy these to a notepad file.

Now, go back to your Hosting dashboard. Click the ‘Website’ tab then navigate to the Advance tab. Scroll down to find Databases and click on the Manage button next to it.

Navigating to the MySQL Database Page from hosting

This will open the cPanel MySQL database page. You’ll find the database credentials listed here.

There can be more than one database. Don’t get overwhelmed and find the one that WordPress uses to make the connection.

In the Current Database section, check the database name and the username.

current database

Match it from the database name and username we got earlier. See if its username is correct. If not, copy the correct username and paste it next to the proper arguments inside the wp-admin file.

For example, place the username inside quotation marks next to ‘DB_USER’.

Now, for the password, scroll down to the Current Users. Find the username that you’re working on and click Change Password next to it to change the password.

Change the database password here that match the wp-admin.php file we opened earlier. Save the wp-admin.php file and close it. Then visit your website to ensure it’s running properly.

This should fix the connection problem and the website will load fine without showing you any errors.

However, if the error establishing a database connection is still showing, then let’s follow another way to solve it.

3. Repair WordPress Database

Sometimes, the database connection is okay, but WordPress is still unable to display your site’s pages. Database corruption can cause this issue. If that’s the case, you will receive the error establishing a database connection message along the lines of ‘One or more database tables are unavailable.’ The database may need to be repaired.

Repairing the database is not that difficult, though. We will use the same wp-config.php file to do this.

So, open the wp-config file and add a single line of code mentioned below before the ‘That’s all, stop editing!’ message comes up:

define(‘WP_ALLOW_REPAIR’, true);

WP database repair code

Save the file and close it. To start the database repair, go visit http://www.yourdomain.com/wp-admin/maint/repair.php and press the Repair and Optimize button. This will take some time, but WordPress will repair any database errors that exist.

Repair WordPress Database

Once the site becomes functional, remove the repair code from the wp-config file.

4. Remove Corrupted Files

By now, your WordPress site should be up and running. But if you still get the same database errors, then it’s time to look at the core WordPress files.

Corrupted files often lead to a website breakdown. Sometimes, a particular theme or plugin can break the site. So, isolating those files from your WordPress installation will let you know if that’s the issue.

Let’s try logging in to the WordPress dashboard. If you succeed, then go to Appearance > Themes and click on the Activate button next to a different theme.

Activate WordPress Theme

Then, go back and try accessing your site from the browser. If the site is loading properly, then it was the old theme that was causing the issue. Make sure to remove and install it again.

Do the same with WordPress plugins. Go to the Plugins menu and select all the plugins to Deactivate them altogether. Use the bulk selection tool to do that easily and quickly.

Deactivating all plugins using bulk selection in WordPress

Remember, you only need to deactivate the plugins to see if corrupted files enter into any of your plugin folders. Don’t delete any plugins, otherwise you’ll have to install the plugins once again from scratch. Moreover, you’ll lose all of the data!

However, if the WordPress dashboard is broken, too, you need to deactivate the theme using your hosting provider’s File Manager or FTP client.

To do that, open the Hosting dashboard and click on Files. Navigate to public_html > wp_contents and find the Themes folder.

WordPress Theme Folder

Rename the Theme file to something else. This will disable all the current themes that are installed.

Once you determine if a bad theme is causing the error, fix it and rename the Themes folder to its original name. Do the same with plugins if you’re still getting error establishing a database connection.

Getting confused about how to fix corrupted themes and plugin folders? Don’t worry, Contact Us to get help for free!

Anyway, if the problem is not with the theme or plugins then you need to fix the WordPress core files.

You need to replace all the core WordPress files with working ones so that it’ll remove all corrupted files from the core files.

To do that, download WordPress from the official site and extract it to a folder. Delete the wp-content folder along with the wp-config-sample.php file. Next, upload all the remaining WordPress files to the public_html folder present on your hosting server, overwriting everything in the process.

Delete wp-config Folder

Note: Get a backup before starting this procedure. Because when you remove your wp themes & plugins and reinstall wp core software, it will clear all of your data.

3 More Ways to Fix a Database Connection Error

You should only attempt these fixes once the previous fixes haven’t worked out. That rules your hosting server to be the only thing causing the problem.

Those using dedicated hosting should troubleshoot this easily, as they have the entire server to themselves. But for shared hosting, the provider will likely provide you with more information on why this is happening in the first place.

1. Update Website URL to Fix Database Connection Error

If you have switched to a new hosting provider and changed the domain name, then you should update the site’s URL in the database. Otherwise, it will always redirect to the old URL from your other hosting plan.

To do this, open pHpMyAdmin from the Hosting dashboard. Select the primary database and click on the SQL tab. Then enter the following lines of code and press Go:

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

This will set the updated site’s current URL.

2. Reboot Hosting Server

Just like restarting your computer can fix memory or OS errors, the same is true for web servers. For those self-managing their servers (dedicated hosting or VPS), do a simple reboot.

If there were any errors due to the server OS, this should fix it automatically.

3. Contact the Hosting Provider to Fix the Error

When nothing works, contact the hosting provider and inform them of your issues at hand. This will confirm whether the issue is on your end or the other. Some providers even provide experts who can fix your WordPress site for you. You can get useful insights from them to help you fix your site.

However, it’s really important to choose a hosting provider wisely. Communicating and getting solutions from the hosting provider vastly depends on which company you bought the service from.

Final Words

A database connection error is pretty serious for any WordPress website. It can literally break the website, resulting in no one being able to visit it anymore.

However, you can fix it easily by checking the database passwords, repairing corrupted databases, and replacing the corrupted WordPress files.

We hope you fix the database issue of your WordPress site successfully. Share this guide with others and help others facing the same problem. Until some other WordPress guides, it’s time to say goodbye.

Black Friday!

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Days
Hours
Minutes
Seconds

Recent Posts

No more posts to show