How to Fix Error Establishing a Database Connection in WordPress

How to Fix Error Establishing a Database Connection in WordPress

Are you getting error establishing a database connection? This article will show you how to fix error establishing a database connection and why you get it. But first let me share my experience with this error. This was really another scare for me as I am wondering what did I do wrong. First, I got Catchable Fatal Error: Object of Class WP_Error Could Not Be Converted to String and I resolve it. In a short space of time, I got this error establishing a database connection message! I was wondering if I should re-install my website but there’s no need to do that as I did my research and my problem was solved. As I said earlier, I will demonstrate step by step how to fix error establishing a database connection and explain why you are experiencing that error.

What Caused This Error?

As the error message states, it’s caused by some form of error in your database. Whatever is in your database is not corresponding with something. While digging deep to find the issue, I noticed that the database name in my database was not corresponding to the database name in one of my files in cPanel. So, that was causing the issue with my website. For yours, It could be that the username, password or localhost is not corresponding.

Effect

The effect of this error is that when you or anyone else is trying to browse your website or try to log into your admin panel in WordPress you will see the message “Error Establishing a Database Connection“. You can see a sample of the screenshot below.

How to Fix Error Establishing a Database Connection.

As I promised to show you how to fix error establishing a database connection but you need to open a couple of things. First, you need to access your wp-config.php file in cPanel. To do this:

  1. log into your cPanel. 
  2. Under Files click File Manager.
  3. Ensure that Web Root (public_html/www) is checked under the File Manager Directory Selection.
  4. Click Go.
  5. Scroll down until you see wp-config.php, select it and click Edit.
  6. When the text editor box pops up click Edit again. Do not close this tab as you will need to compare it with your database. See screenshot of a sample below.

Secondly, you need to access your database to do a comparison with what is in your wp-config.php file as I just stated. To do this:

  1. Go back to your cPanel.
  2. Under Databases,click MySQL Databases and you will see your database name and username.

Compare WP-Config.php With Your Database to Fix Error Establishing a Database Connection.

Lastly, you need to do a comparison between your wp-config.php file and your database. Click your wp-config.php tab in your browser, scroll down and look for “DB_NAME, DB_USER and DB_PASSWORD“. They stands for database name, database user and database password respectively. After each of them, take note of what is enclosed by the apostrophe as highlighted in the example in red. eg. define(‘DB_NAME’, ‘database_name_here‘); which is your database name.

Now, click your MYSQL Databases tab in your browser and compare your database name and username to ensure that everything matches as color coded in the sample below.

If the credentials in MYSQL Databases does not match with whatever in your WP-Config.php file, you need to modify it in WP-Config and click Save. Reload/Refresh your website again to see if it works. The issue I had with my website is that the database name was changed to something else. But luckily I was able to resolve it on my own.

If your username and database name is correct in both WP-Config and MYSQL Databases, then you need to test if it’s the password is the problem. If it’s that your password is the issue, you need to create a new user and generate a new password. When you are finish, modify the WP-Config file accordingly with your new username and password and click save. After that, you can delete the old username if you want.

That’s it guys! I hope my article on how to fix error establishing a database connection has help you resolve your database connection. If you have other ways on how to fix this issue, you can share it with us in the comment section below. Also, if you like my articles you can subscribe to my blog.

LEAVE A REPLY

Please enter your comment!
Please enter your name here