How to Fix the Error Establishing a Database Connection

Share:
database connection error

Maintaining a WordPress website needs some regular work, it starts from cleaning database to updating plugins.

Regular maintenance needs to be done to ensure proper WordPress maintenance. However, there are instances when database error connection occurs.

This can be a dangerous issue for a website with hundreds of readers trying to reach the website and seeing the dreadful message, “Error establishing a database connection.”

If the issue is not fixed urgently, you might lose some valuable visitors or potential customers! And, that is why we are here to help you by sharing an insightful tutorial on how to fix the error establishing a database connection.

What does the database error mean?

Before we dive deep into the solution, we first need to understand what the database error means. WordPress is a complex software and is composed of multiple components. The database is one of the main components of a WordPress website, and it is used for accessing stored data.

Behind the scene, PHP and MySQL are used to make WordPress function properly. Just like any other architecture, data is stored in MySQL. PHP is the programming language that is used to fetch the stored information from the database.

So, whenever a visitor requests a page load on your website, a request is sent to the MySQL database using the PHP code and then the page is rendered. There are multiple PHP queries that are used to load the page including the author information, page title, and page content.

Now that we understand how the page loads. If you will see a message, “Error establishing a database connection,” this means that the PHP code failed to connect to the database and hence showed the message.

If you see a “blank page,” it is more a database connection issue as well.

What are the possible reasons behind the error?

There are many reasons why the error occurs on your website. To make sure that you understand the source of the error, we are going to list the main reasons why this happens.

1. Database login credentials are wrong: Database credentials are used by your website to fetch the information stored in the database. It is different from the website admin login credentials. Sometimes, the developer who is working on the website change the credentials. This can lead to database connection error.

2. Database server hosts down: If the server that hosts the database it down, you will see the error. The cause can be because of the web hosting company, or your website is getting too many requests that the server cannot handle.

3. Database corrupted: This is the worst case scenario, where your database is corrupted. The database can become corrupted due to multiple reasons including bad theme configuration, plugin or a server-side issue.

How to fix the database connection error?

With enough knowledge under about the error, let’s get started with the solution.

01. Repairing the Database

There are many scenarios associated with the database error. For example, you might be able to access the website, but not the admin panel. Also, some error message might read a completely different error. When this happens, it means that you need to fix the database as it might be suffering from corruption.

To fix the issue, you need to edit the “wp-config.php” file. The file is located in the core directory of your WordPress website.

To access the file, you need to use file manager through the cPanel. If you have a custom WordPress installation on a server, you need to connect using an FTP client.

When you open the file, you will see a lot of options already defined there. To make the exact error popup, you need to add the line at the end.

define( 'WP_ALLOW_REPAIR', true );

Once done, you need to save the file.

After that, you need to open the following URL.

http://www.abcwebsite.com/wp-admin/maint/repair.php where abcwebsite = your website name.

This will prompt the following message to show up.

Error Establishing a Database Connection

That’s it! You just need to choose one of the options, and you are ready to go. Both of the options will work, but the 2nd option will take more time as it also optimizes the database.

Warning: Once you are done with the repair of your database using the above method, you should remove the line you added into your wp-config.php file. This needs to be done to ensure that your site is secure. The URL that you used to access the repair database page can also be accessed by anyone as it is set to public. So, hackers might try to hack your website or run unnecessary repair jobs on your website, making your website slow and unnecessary.

02. Configure wp-config correctly

As we already mentioned, the above method might not work for everyone. If the above method didn’t work, then you can try configuring your wp-config file correctly.

wp-config.php file is an important file which stores key information such as database name, database username, password, and host. If anyone of the information is altered, the database connection error can pop up.

For example, you might have changed the hosting of your website or changed the database name for some reason.

Just like before, you need to use the cPanel to access the File Manager. The file name that you should look for is “wp-config.php.” Once located, you need to edit the file and check if everything is set properly.

When you open the file, you will see four key information that your WordPress website require to operate successfully.

1. Database name, i.e., DB_NAME

2. Login information, i.e., DB_USER

3. Login password, i.e., DB_PASSWORD

4. Database host, i.e., DB_HOST

Check the code below to understand it clearly.

// ** MySQL settings ** //
/** The name of the database for WordPress */
define( ‘DB_NAME’, ‘database_name_here’ );

/** MySQL database username */
define( ‘DB_USER’, ‘username_here’ );

/** MySQL database password */
define( ‘DB_PASSWORD’, ‘password_here’ );

/** MySQL hostname */
define( ‘DB_HOST’, ‘localhost’ );

If you are confused on what to do or don’t have the necessary information, you need to contact your web hosting.  Your website will not work if any information is set wrong.

Using the PHPMyAdmin

Want to find the information yourself? Well, you can do it by using the PHPMyAdmin. PHPMyAdmin is one of the most used application to access MySQL database. It can be accessed using the cPanel provided by your hosting.

PHPMyAdmin is also used by developers to create, delete or manage new database tables. This means that you should be extremely careful in what you are doing. If you are not sure what you are doing, please contact your web hosting support for further help.

The next thing that you can do is find the information needed to make wp-config.php work as intended. After you login into the PHPMyAdmin, you will see a left-hand column. The column contains all the database associated with your site. Now, you need to click on the database that matches the value after DB_NAME.

Check for wp_options table

Once you selected the database,  you now need to search for the wp_options. The table should contain all the information that you need. Click on “browse” and then you will be able to get the following information.

  1. URL
  2. Website Name
  3. Other general settings.

Cross-check the information that you see here with the information in the wp-config.php. If you see a difference, update the wp-config.php.

With the DB_NAME and website name known, now it is time to get the username and password.

To do so, you need to either create a .php file that tests the connection between your website and database. The credentials should be similar to that of wp-config.phpfile. If you are not comfortable with creating a .php file and checking the connection, you can also create a new user and password with root access, and update the wp-config.php file with the new information.

Testing Your Credentials

Code for testing your website connection with the database is a below.

?php
$test Connection = mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$testConnection) {
die(‘Error: ‘ . mysql_error());
}
echo ‘Database connection working!’;
mysql_close($testConnection);
?>

To make the above code work, you need to change the three values localhost, root and password with your website URL, root value and password value. Most of the time, you should not change the localhost as the value is same for popular host.

You may want to check the Knowledge Base(KB) or contact support to know the real value.

Now, you need to save the file with a .php extension. Let’s say you save the file as connectiontest.php.

Once done, put the URL: www.yoursite.com/connectiontest.php. If it works, then you are all set. If it doesn’t then, it will show you an error message, and you should work on solving it. The solution is just simply to create a new username and password and try the script again.

New password and username creation

If your existing credentials are not working, it is time for you create a new user and password. To do so, you need to use the MySQL Databases tool. It is also available in the cPanel.

In the MySQL Databases tool, you need to click on the MySQL users. Then choose “Add New User.” A new window will popup, and then you need to choose a new username and password. Make sure that you choose a strong password as it will be required for security against hackers.

You can use a random number generator to create a strong password. Once done, you need to click on the “Create User” button.

Once the user is created, now you need to add the user to have access to the website database. Click on the heading, “Add User to Database” and then select the new username that you just created. Once done, click on the “Add” option.

Congratulations, you have now created a new user and added to the database.

Corrupted WordPress Files

One more reason that can bring the database connection error is the corrupted WordPress files. This is not a common problem, however, it can happen. So, if all the above-mentioned tricks failed, then the problem can be in your WordPress files.

Corrupted WordPress files issue can easily be resolved by either restoring the WordPress file or do a fresh install. To make sure that you don’t make everything worse, a backup is recommended.

After the backup is done, you need to download a fresh WordPress core files from the official WordPress website. Once downloaded you need to unzip the file and then delete the wp-config.php and wp-contents folder.

Once done, you need to upload the files to your hosting server with the help of an FTP client. You can also use cPanel and upload it using the File Manager.

If everything is done right, your website should load instantly. Also, don’t forget to clear your browser cache before reloading your website.

Final Step

You tried everything, and nothing worked. Don’t worry. You always have the support from your hosting provider. All you need to do is submit a ticket to the support, and they will look into the matter.

As database connection error is the most common type of errors, it would probably get solved within an hour after submitting the ticket(depends on how your hosting provider customer service is). You can also choose to contact them through live chat for a faster solution.

So, what is your database connection horror story? Comment below and let us know. We want to hear everything that you have to say!

Join Software Buyers & Sellers

Get top software information and best deals right on your inbox.

HubSpot CRM Sidebar
ADVERTISEMENT
Popular on Begindot.
An all-in-one employee management HR...
monday.com is a leading project management and CRM solution,...
Smartsheet is a modern project...
Salesforce is one of the...

Promo Box*

Popular EOR Solutions

EOR (Employer of Record) helps businesses hire global workforce and make human resource-related processes easier.