How to Fix Error Establishing a Database Connection in WordPress?

0
315
Fix Error Establishing a Database Connection in WordPress

It is frequent for a website owner to go through the tedious task of fixing the database connection error. It is common to receive errors when you are working on your website, but every once in a while, your website’s backend loses the connection with the database. Thus, you and/or users of your website will have no access to the website.

For every single minute your website is not working, you are losing clicks, leads and potential clients for your business. The database connection error is a serious situation and, if not handled immediately, could be severe, leading to disruption of your clientele.

The fatal error, which makes your website non-accessible to your users, could occur due to a number of reasons. There are a lot of things that can affect the connection between the database and WordPress. This error is not something that anyone could solve as it requires some knowledge of what’s happening in the backend of your website.

Cheap SSL

Anyways, we are here to help you fix the database connection error. Also, we will be discussing why and how database connection error occurs when one is establishing a new connection between their website and a database.

What is a Web Database and Database Connection?

As we are constantly spending more time on the internet, we are producing data in an uncountable amount. As you are reading this line and by the time you finish up reading this whole article, 2500 more users will be connected to the internet for the first time.

All users using the internet are creating data in one form or the other, so it only makes sense to have someplace to house all this information and store it in such a way that it becomes possible to use it for gathering relevant information.

A web database is essentially a database that can be accessed from a local network or via the internet. It is a database that is not stored in one’s device but available on the internet and can be accessed only if the individual has an active internet connection.

One of the great examples of a web database is the list of products that are present online. Your search for the keyword and the products will line up the results accordingly. Everything on the internet that we see is stored in a database and is retrieved from it when we want to see it.

Working Of Database Connection

A web database works differently than a static database as it is not limited to just one computer or a LAN network. A web database works on a dynamic mechanism that indexes searchable information.

The creator of a web database has no authority or control over the external pages that are referenced to it due to the fact that the internet as a whole works as a non-static platform. Given below is the step-by-step functioning of a web database:

  • Step 1: A user sends the information about the content she wants to see on the screen. In simple terms, she clicks on the URL of the webpage.
  • Step 2: Here, the browser from which the user is accessing the internet will send in the request to the server where the database is stored.
  • Step 3: The server allows the request and sends in the required data to the user’s web browser.
  • Step 4: Once the website is open, the user can go through the webpage to find out the desired information. Also, here users can provide their report which will be stored in a form created by the website and sent to the server.
  • Step 5: The database retrieves the information which it needs to store to make the logging process easier for the same user for next time and sends in the success dialogue or prompts other information if required.

How to Fix the Error Establishing a Database Connection in WordPress Using 5 Different Methods

Given below are the 5 proven methods that can help you fix up your WordPress database connection error all by yourself. All of these are quick fixes. If the problem persists, you need to get tech support and get it resolved as soon as possible. So, let’s start.

1. Check Your Credentials

The easiest quick fix is to check whether you have entered incorrect database credentials. When migrating your WordPress site to a new host, it’s a common mistake that a lot of website owners make. To find out the WordPress database credentials, you need to access the wp-config.php file.

All the essential information regarding the database and its configuration is stored in this file. In case this is your first time editing the wp-config.php file, you might want to look at the online guides on how to edit it correctly without making any mistakes. When you open the document, you need to look for the given fields:

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' );

Here, check the information regarding the database name, username, password and the host of the database. You can match this information from your WordPress hosting dashboard, too, by clicking on MySQL databases.

If there’s any difference in the following fields, copy them from your MySQL database and put them in the wp-config.php file. Save the file and see if the error is still there on your website. If the error is still there, you need to look for other issues as there might be something else that is wrong here.

2. Check for Database Corruption

The second possibility is the database being corrupted. There are many reasons for a database to be corrupted, including being corrupted by an incompatible plugin that the developer has installed in the latest update. Another reason for a corrupted database is the temporarily down server that is hosting your database.

On the other hand, there are still some other possibilities that may have caused the error in the first place. If the host is working perfectly fine and there has been no update or installation of the plugin, then you need to perform the following steps to find if the database is corrupted or not:

Step 1 – First, you need to see if the database is working fine. If you can see pages working fine on your website but still you can’t access wp-admin and get a message “One or more database tables are unavailable. The database needs to be repaired,” then this is a clear sign that your database has been corrupted.

Step 2 – The great thing about using WordPress is that it already has an inbuilt feature to repair the database automatically. By default, however, this feature is disabled. Thus, a user needs to enable it manually to start the database repairing process. The user needs to add this simple line of code and it will do the needful and repair the code all by itself:

define( 'WP_ALLOW_REPAIR', true );

Step 3 – Next, visit www.yoursite.com/wp-admin/maint/repair.php. Now you will have 2 options; the first one being a repair option, and the second one being repair and optimize. The latter option will fix and optimize your database so it can be future-proof and make it stable.

Step 4 – One thing you need to keep in mind when you put the single line of code mentioned above in the wp-admin.php file is that the page is not secured. This means anyone can access it if they get their hands on the URL without having to log in.

Thus, you need to get that little line of code out from the wp-admin.php file. After the database optimization is done, this step will ensure that no one has access to your database.

3. Have a Fresh Installation of Files

In case troubleshooting your database doesn’t work, then the last thing you need to try before calling tech support is fixing the corrupted WordPress files. Many users, when they are not able to find the problem, completely wipe all of their files and install new ones. Just like that, the errors go away. We are going to help you do the same thing.

As we will be deleting main system files and getting them back, so you need to be careful while doing it. Also, it is best to back up all your files before you start this fix. You can use a plugin to make a backup, or you could do it manually. Once you wipe out the files, the next thing is to bring in the new files and replace the ones which are causing the error. This is how to do it:

Step 1 – Download a fresh copy of WordPress CMS from the official WordPress.org website.

Step 2 – Unzip the file in your local drive and delete the wp-contents folder along with the wp-config file. This needs to be done before you do anything else to make sure that no overwriting takes place for the settings that you already have in your current wp-config file. Also, this will keep all your plugins and themes intact and safe.

Step 3 – After you have deleted the corrupted files, it’s time to paste the rest of the files into your WordPress root folder using the file manager or the FTP client.

This will help in deleting any files or any new update, which is causing the database connection problem. Now you need to reload the page once again, and the problem should probably go away.

If you have followed each of these steps correctly and you are still facing the same problem, then the next thing would be getting in touch with the customer support of your web hosting service.

4. Check Your Theme Files and Plugins

There are a couple of misfortune events that could lead to file corruption. In fact, file corruption could take place even if you have made some changes to your plugin. It happens a lot of times when the plugins are not optimized for the given WordPress theme, and when we try to run them, they just take the whole thing down.

On the other hand, you might have hooked your website to some external services, which is causing this issue. Well, if there’s a problem with the theme and plugin, then you need to follow the given steps in order to get back your database connection:

Step 1 – First, you need to make a connection with your FTP. You can do it manually or take the help of some free software like FileZilla to get it done automatically. To get connected with your FTP, you need to provide the login password to the FTP account.

If you have forgotten it by mistake, then the easiest way to retrieve it is by searching for the credentials in your email that you received from the web hosting platform when you first signed up. In case it’s impossible to find the email with credentials, then you need to contact the live chat support to provide you with the required details.

Once you get the initials, connect to the server and go to the WordPress root folder. You can find it with the name “public_html” or “www.” There might be a chance that you are already inside it as you establish the FTP connection. If you can see subfolders like “wp-admin” and “wp-content”, then it’s the root folder.

Step 2 – Go to “wp-content” and change the name of the “plugins” folder name to something else. You can put it in something like “test-plugins.” Save the changes and see if the problem is still there.

Step 3 – If the problem has been removed, then it was the plugins that Were causing the database connection error. Go back to the FTP, rename the folder back to “plugins.”

Now get inside the folder and change the name of individual folders one by one to pinpoint the folder leading to the database connection error. Once you find it out, delete that plugin folder and check if the website is working fine.

The same thing needs to be done with the theme folder, and you have to repeat it till you find the sub-folder causing the error.

5. Get in Touch With Your Web Hosting Provider

If your website was working just a minute ago and now it’s not working and showing you the database connection error, then it is safe to assume that the web hosting provider is handling some technical issue with their server.

Every well-known web hosting provider now offers fast tech support, usually through live chat or email, to the website owners about any technical issue.

Contact their chat support if you didn’t receive any mails and the problem is still there even after waiting for 15 minutes after sending an email. Tell them you are experiencing “Error establishing a database connection” and ask if the server that holds the database is working fine? And if there’s some unusual spike in the activity on the server right now.

In most cases, when you get in touch with your web hosting provider, they will tell you precisely what happened with your database and if the problem is from there, how they are dealing with it and how much time it’s going to take for your website to come back online once again.

On the other hand, if everything is fine from their end, they will work with you to solve the problem or do it by themselves by, obviously, taking your permission.

How to Avoid Database Connection in WordPress in the Future?

There is no 100% fail-proof way to protect your website from database connection errors but there are 2 main things that you need to take care of, which makes your connection to be 90% safe and reliable:

1. Go for the Best Web Hosting Provider

The database connection occurs due to issues faced by the web hosting server. It could be a power issue or the server storing the database could be under an attack. As a result, a website owner needs to be very careful about deciding which web hosting service they need to opt for.

When you choose a great web hosting provider, then you don’t have to worry about half of the error messages that other website owners have to face.

2. Create Backups Regularly

Whenever you are making any changes on your WP website, you need to make sure that you have a backup file before the change takes place. This way, if something went wrong during an update or installation, you can quickly restore the working files and take your website back online.

Conclusion

We really hope that we have helped you in solving the WordPress database connection problem. It’s terrifying to see the “error establishing a database connection” error.

But we believe you now have understood how this error occurs and what you need to do to resolve it. If you follow the steps given in this article, you can easily and quickly restore the harmony between your WordPress website and its database.

People are also reading:

LEAVE A REPLY

Please enter your comment!
Please enter your name here