Tutorials Web Development

How To Fix “Error Establishing A Database Connection” In WordPress

Database Connection
5/5 (2)

Have you ever thought of how your website actually works? It usually doesn’t depend on the CMS, the formula is pretty universal. The system stores the information in the MySQL database and connects to it when it needs to get it back or write down more.

Sometimes you may experience difficulties caused by this exact formula. Your website can go down hard and give you the MySQL database connection error.

Do you deal with the WordPress websites? If you read this article and visit this blog, I assume yes. Then you should’ve stumbled upon this common WordPress error called the ‘Error Establishing A Database Connection’.

Today I’d like to take a deeper look into it and tell you what may be the reasons for this error and how to fix it on your side if possible.

Oh, by the way, even in case my ways of fixing it won’t work, I’ll give you a hint about how to fix it anyway!


What are the common reasons

Prior to trying to solve this problem, I would recommend checking what the basic reasons for this mistake are.

  • Your login and password credentials do not match the mentioned ones in the wp-config file. It’s one of the most common things to happen. Especially, when you transfer your website from the localhost to the hosting
  • Your database may have been screwed up due to the technical problems of the hosting provider
  • You have installed a corrupted plugin or a theme that has crushed your database
  • Your website cannot deal with the traffic load you get

These are the reasons, and you should determine which one you faced this time. In order to do that, I’d recommend you try all the ways of fixing this that I’ll give you further.

However, you should always remember that safety goes first, so prior to our fixing process you should backup your website. It will ensure that your website will not get more screwed in case you do something wrong.

First, go to your FTP server and download all the files you have there. Download, literally, anything connected to your website, it’s really important.

Then go to your database and export all the tables to the .sql file.


wordpress error establishing a database connection


How you can fix it

First of all, I recommend you checking up your wp-config file. You can find it in the main folder of your website. It’s called the ‘wp-config.php’, you can open it with any text editor, Brackets or Sublime in case you are a developer and have those on your computer.

Then simply scroll and look for the following part of the code:

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


Now make sure that these credentials actually match the ones from your hosting provider dashboard. In order to check this out, go to the dashboard and visit the FTP and MySQL sections and check if the ones you were given by the provider match the ones in your php-config file.

Oh, I should also mention that your Database host should always be set correctly. When you just upload your website from the localhost to the external server, you should change the DB_HOST value from the ‘localhost’ to the one you have been given by your hosting provider.

If everything is alright, yet you are still getting this error, let’s see what else we can do.

I’m not sure if you know, but you can ask WordPress to repair your website’s issues for you. In order to do that you should perform a few simple moves.

Go back to your wp-config file and add the following line:

define('WP_ALLOW_REPAIR', true);
Once you’ve saved it and applied the changes, you should go to the /wp-admin/maint/repair.php page, and you will see the following message from WordPress.

wordpress error establishing a database connection

You have two options here: Repair Database and Repair and Optimize Database.

Personally, I recommend you going with the second option because it gives a deeper look at your issue and may fix it not only by repairing it but also by optimizing it according to its own guidelines.


Did it help?

Now let’s see if it helped you. Do you still get this stupid error that makes you wanna crash your laptop?

No worries, my friend. I’ve got to tell you this great feature from TemplateMonster that I’ve recently used. They’ve got their very own Service Center, and they can fix anything you need. Just get in touch with them, and they will help you fix any error and perform any customizations you need.

One of my previous projects has been customized and prepared for the release by these guys. My clients were very happy with the results, and I was not getting a headache by all those errors that could have appeared.


Related Posts

Plugins and Tutorials to Modify Your WordPress Database Safely

Setting up Database for Social Media Websites – Some Basics to Know

Six WordPress Plugins to Install on Every WordPress Site


Leave a Reply

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