How To Fix Error Establishing A Database Connection WordPress?

You are currently viewing How To Fix Error Establishing A Database Connection WordPress?

WordPress is a CMS (Content Management System) that stores all of its data inside the database. If you are getting “WordPress Error Establishing A Database Connection”, you are not the one. This error arises when WordPress is failed while communicating with the database.

It may be due to any of the reason(s) that are preventing WordPress being communicating with the database. So, in this article, I am going to list down all the possible reasons that are preventing your WordPress being communicating with the database.

throughout this article, I’ll help you to fix the “Error Establishing A Database Connection”. So, keep reading.

What does it mean when it says Error establishing a database connection?

The answer is simple, right now your WordPress site is not able to communicate with the database. That is the reason you are getting this error on your site.

This either means that the username and password information in your wp-config.php file is incorrect or can’t contact the database server. This could mean your host’s database server is down.

Why WordPress needs to communicate with database in order work properly?

Well, WordPress is a CMS. So what WordPress does? it stores all the details of your site inside the database.

When user request specific pages or content on your site. WordPress serves the data from the database.

This means the content which you see on your site is coming from the WordPress database. Nothing is fixed or static there. Everything is dynamic.

So in this case, if WordPress is not able to communicate with the database. It will show you the “Error Establishing A Database Connection” error.

How do I fix Errors establishing a database connection in WordPress?

First thing first, first of all, you need to find out the actual reason why you are getting this error (Error Establishing A Database Connection)?

To spot out the reason, first of all head over the wp-config.php file of your site.

You can find out the wp-config.php file inside the root directory of your site.

wp-config.php

Open the wp-config.php file in editing mode.

Set the “WP_DEBUG” as “true” to enable the debug mode in WordPress. By default, it is set to false. If you have never edited the wp-config.php file before. You can find the “define( ‘WP_DEBUG’, false );” on line 80.

define( 'WP_DEBUG', true );

Note, don’t forget to set WP_DEBUG as “false” when you fix the error.

What is Debug in WordPress? It’s a debug mode in WordPress, it helps you while debugging the bugs inside your WordPress site. If you enable (TRUE) WP_DEBUG mode. WordPress shows you error(s) while accessing your site’s URL if there any.

what is the wp-config.php file?

wp-config.php is a configuration file. Inside this file, WordPress defines the general settings including database settings.

Next step, After enabling the debug mode, try to access your site URL (https://yoursite.com).

Now you can clearly see the the error and the reason why you are getting Error Establishing A Database Connection.

If you are getting this error due to wrong database credentials. Then WordPress will show you an error that your database credentials are incorrect.

Error Establishing A Database Connection – Due to the Wrong Database information inside the wp-config.php file?

To introduce you with the errors, I have created an error for you. What I have done? inside my wp-config.php file inside the MySQL settings on line 23, I have changed the name of the database.

Error Establishing A Database Connection

My actual database name is “fotuk760_wp892“. I changed it “fotuk760_wp892-xx“. This change generated the error which you can see below.

Error Establishing A Database Connection

As you can see my WordPress with user “fotuk760_wp892” is not able to select the database. And it is also showing me that my username and password are correct but not able to select “fotuk760_wp892-xx” database.

As I mentioned above my actual database name is “fotuk760_wp892” and I have mentioned the database name as “fotuk760_wp892-xx” inside my wp-config.php file.

“fotuk760_wp892-xx” database doesn’t exists and if it exists it’s a wrong database for the user “fotuk760_wp892“.

If the username or password information is incorrect inside your wp-config.php file. You will get errors like this (see them below).

Error Establishing A Database Connection

Check Your Database Host Information

Inside your WordPress site’s wp-config.php file if your host information is incorrect. You will get an error (see below).

Error Establishing A Database Connection

If your site host information is incorrect you will get an Error Establishing A Database Connection error. So, it’s important to point your WordPress database to the right host.

Most of the WordPress Hosting providers use localhost as their host. But if localhost is not working for you. You can seek support from your web hosting provider to know the name of your host.

Repair WordPress Database

If there is some another error inside your WordPress site such as missing database tables or something like that. Then repairing your WordPress database should help you.

You can repair WordPress database by adding a single line of code inside the wp-config.php file of your WordPress site.

define('WP_ALLOW_REPAIR', true);

After adding this line of code inside the wp-config file. Now hit the URL given below to access the WordPress database repair settings.

http://www.yoursitename.com/wp-admin/maint/repair.php

Make sure to change the yoursitename.com with your site name.

How to Repair wordpress database

After repairing the database make sure to remove the code which you have added inside your wp-config.php file. If the code exists anyone can access this page they don’t need to be log in as admin.

If You Still Get The Error

After doing all the stuff if you are still getting this error then you can do the following things.

  1. If you get the error most of the time when you access your site. And the error automatically disappears after some time. Then there may be a problem with your host. Here I would recommend you to change your web hosting provider.
  2. You can ask for support from your web hosting provider to solve the Error Establishing A Database Connection.
  3. If your site is facing heavy traffic and if you are on the shared hosting which is not able to handle that amount of traffic. Due to this, you are getting this error then I would recommend you to switch your web hosting provider.

Leave a Reply