-
Home / WordPress / How to Fix error establishing a database connection: Method-By-Method Guide
Error establishing a database connection

How to Fix error establishing a database connection: Method-By-Method Guide

A terrible scenario for a webmaster or WordPress site developer is when their site goes down due to an error like “establishing a database connection”. Not only do you lose inbound traffic, but your site’s reputation and rating suffer as well.

The database connection issue is one of the problems that might make your website momentarily unavailable. This error notice, “error establishing a database connection,” may have appeared when you tried to access your website’s URL and nothing showed up.

Don’t worry if you’ve run into this problem before or are currently trying to figure it out! We’ll walk you through the simple, proper method of fixing this problem in this step-by-step guide: “Resolving Database Connection Error”. You may quickly restore your site and all of your traffic and subscribers by carefully following the steps.

Sounds good? Now let’s get going!

What Does the Broken Database Connection Error Mean?

Allow me to explain what this problem implies before we dive right into the tutorial. The error message “establishing a database connection error” essentially indicates that the PHP code could not create a connection to your MySQL database to obtain the necessary data to complete the page’s construction. Because it is not connected to your database, there is no information about your site, which is why the error is always displayed on a blank page.

Why Does the Database Connection Error While Establishing Occur?

Now that we know what the problem means, we need to identify the cause so that we may try to access our WordPress website. There could be a multitude of explanations for this, as there are numerous potential underlying causes for database connectivity failures.

1. Inaccurate login information

For example, it may occur from using the incorrect login credentials to access the admin panel. Should the login credentials have changed recently, using the previous ones won’t allow you to access your database. Therefore, you should verify the accuracy of the login credentials you are entering as your priority.

2. Impure Database

A second issue is that there may be corruption in the database. There are several ways to corrupt the WordPress database, including installing a problematic or incompatible plugin. The possibility of a temporary outage of the server hosting your database is another possible explanation.

These are some potential causes of the error that could have occurred. Now that you are aware of the error’s nature and most likely source, let’s attempt to resolve it and restore your website’s functionality.

How to Resolve the Error Creating a Database Connection?

We must identify the precise cause of the mistake before attempting to solve it.

First Approach: Fix the Database

Our database has to be checked first. When attempting to enter the WordPress admin page, you may receive an error or another error stating that “One or more database tables are unavailable,” even though you can see your website regularly. It could be necessary to repair the database. These are obvious indicators that your database is faulty when you attempt to access your website. Fortunately, WordPress has a built-in tool that makes it simple to repair your database.

To fix your database, you must manually activate the feature, which is deactivated by default. To do so, open the wp-admin.php file and enter the following code:

define( ‘WP_ALLOW_REPAIR’ , true );

You may improve and fix your database using this shortcode. Just go to www.yoursite.com/wp-admin/maint/repair.php after inserting this line. There will be two comparable options on the page: Repair, which will fix your database, and Repair and Optimize, which will fix your database and optimize it to prevent issues in the future and maintain stability.

If you’re pressed for time, go with the first option instead of the Repair and Optimize mistake, as the latter takes longer.

It’s also crucial to remember that the repair page you just accessed after adding the line of code to the wp-admin.php file is not secure because anybody may visit it without logging in. Therefore, after you have successfully fixed your website, make sure to remove that small line of code that you added to the wp-admin.php file previously. By taking this action, you can be sure that future users won’t tamper with your database settings.

Second Approach: Modify Your Login Information

Even after fixing the database, are you still having trouble getting your website to open successfully? You may wish to check the wp-config file’s database login settings. If you changed your hosting provider or other important database information that wasn’t explicitly updated in the wp-config file, your database login credentials might no longer function.

Locate the database login information by opening the wp-config file again and following the previous instructions. It should be located close to the top of the file.

wp-config

The database host is identified as “DB_HOST,” the database name is “DB_NAME,” the login username is “DB_USER,” and the login password is “DB_PASSWORD.” Make sure everything is accurate after reviewing all of this data, as WordPress won’t be able to connect to the database if any of these numbers are off.

With PHPMyAdmin, a program that is essentially for accessing and manipulating MySQL databases, you may inspect your database without being within WordPress. However, use caution as this tool works directly with the database of your website—there’s a good chance you might make matters worse.

Steps to follow carefully:

  1. Click on your database from a list of servers’ databases after logging in with your PHPMyAdmin account. Be sure to select the one whose name corresponds to the one you just checked in the WordPress configuration file.
  2. You’ll notice many table names in your database after clicking on the database name. Click the Browse button next to the table named wp_options to confirm that you have the correct database name.
  3. This will direct you to a page where you can view your WordPress website’s name, URL, and other general options. Verify that it matches the information found in the previously seen wp-config file.

That brings an end to the database name. Now that we have the username and password, there are a couple of methods to verify that they are both accurate. Making a straightforward.php file that tests whether you can connect to the database using the login credentials from the wp-config file or the current login username and password is one method to verify if the already-existing login username and password are correct. To test, merely make a.php file, give it any name you choose, and include the following code in it:

<?php $test Connection = mysql_connect('localhost', 'root', 'password'); if (!$testConnection) { die('Error: ' . mysql_error()); } echo 'Congratulations! The database connection works!'; mysql_close($testConnection); ?>

Using this code, you may open your browser and navigate to the file’s URL to test the connection.

We need to use a different tool called MySQL Databases, which is available in cPanel, to establish a new database user and password. All you have to do is click on it, then select Add New User under MySQL Users. This will redirect you to a new page where you will be requested for your new login information.

Once a username and password have been selected, enter them in the wp-config file. To proceed, click Create User. Select your WordPress database and your new username on the ensuing screen, then click Add User to Database.

Just update your wp-config file with the new username and password when you’ve finished adding the new user to the database. This guarantees that your account, password, and database name are all 100% accurate.

Third Approach: Repair Damaged Files

The last option is to repair any corrupted WordPress files if fully debugging your database doesn’t even work. Many people deleted all of their data, which resolved the error, but they were unable to find the underlying cause.

Having stated that you must exercise extreme caution because we are discussing the primary system files. The smallest errors have the power to destroy your entire website. It’s advisable to manually backup all of your data or use a plugin before moving forward. The next thing we require is brand-new WordPress files to take the place of the outdated, corrupted ones that are giving us trouble.

Steps to follow carefully:

  1. Get a brand-new WordPress CMS download from WordPress.org.
  2. To ensure that there is no possibility of overwriting your existing wp-config file, unzip it to your drive, then remove the entire wp-contents folder and the wp-config file. Furthermore, you won’t lose any of the effort you’ve put into configuring themes and plugins thus far.
  3. Using the File Manager or an FTP client, copy the remaining files into the WordPress root folder after erasing any corrupted or infected files.

This will replace any files that are corrupted or the source of the issue. You may quickly resolve the issue Establishing a Database Connection issue by following these easy steps. When you’re finished, reload the page and you’re ready to go.

The next sensible step would be to get in touch with the customer support agents of your web hosting provider if you’ve followed the full guide in sequence (and correctly) and the issue still exists. This is because there’s a good chance that the error originates at the server end. Don’t forget to share with the customer care agent the steps you took to fix the issue.

Conclusion:

This Mistake One of the most annoying issues you can run into on your WordPress website is the “Establishing a Database Connection” error. This problem has multiple potential root causes, which makes it challenging to locate and fix.

We went over a few of the most likely causes of this problem and showed you three simple ways to resolve it.

On your WordPress website, were you able to locate and resolve the Error Establishing a Database Connection error? Which approach proved effective for you? Please share your experience with us in the comments box below. We would love to hear about it!

About Jai Nagpal

Avatar for Jai Nagpal
Hello everyone, My name is Jai Nagpal. I am a dynamic and accomplished Digital Marketing Executive, recognized for my strategic vision and impactful contributions to the digital landscape. My passion for digital marketing goes beyond the technical aspects, as I am also known for my creative flair and ability to connect with target audiences.