How to Fix the error establishing a database connection in WordPress?

In the early days of my career, I often faced errors establishing a database connection. In this article, we will learn to fix it.

Reasons of Database Connection Errors

Behind this error, there can be multiple reasons. It does not depend on a single file or function. It has server involvement. So if you are facing a database connection error in WordPress then you will have to check a few things in WordPress and the server.

  • In some cases, websites get sudden heavy traffic that causes errors from the server. Because if a server has limited bandwidth then it can’t respond to heavy traffic. 
  • Installing bad coded or too many plugins that can corrupt the database. Always use well-reputed plugins and themes in WordPress.
  • Sometimes database connection errors come from the hosting end. Sometimes they migrate databases from one server to another, it happened to me. So if you tried everything to resolve the error establishing a database connection then you should contact the hosting provider.
  • This error can be the reason for forgetting to change the local server’s credentials with the live server’s credentials.

Configuration File

WordPress takes database configuration from wp-config.php which is located in the root directory. It establishes a connection between the database and WordPress.

When you download the fresh version of WordPress, you will have a file on the root directory with the name wp-config-sample.php file. You need to create a clone with the name wp-config.php before the WordPress Installation process. By default, it doesn’t exist. You need to check if your website has wp-config.php.

In case wp-config.php is missing for any reason then you need to create it by cloning wp-config.sample.php and putting database credentials.

Check Database Credentials in WordPress

Next, you need to make sure that you have added the correct database credentials to the WordPress configuration file (wp-config.php).

/** The name of the database for WordPress */
define( 'DB_NAME', 'your_database_name' );

/** MySQL database username */
define( 'DB_USER', 'your_username' );

/** MySQL database password */
define( 'DB_PASSWORD', 'your_password' );

/** MySQL hostname */
define( 'DB_HOST', 'your_hostname' );

WordPress sets other configurations that are usually compatible with every server but in the above four constants, you need to put the correct credentials of your database in order to fix the error establishing a database connection.

Check Database Credentials in Server

Usually, in the server, we create the database and user. Then we grant all privileges to the user. So make sure that you grant all privileges to the user.

The best practice is to copy each detail into a notepad and then put it into a WordPress configuration file. In this way, there will be fewer chances of putting wrong details.

Check if the Server is Down

If you make sure that everything is correct, as mentioned above then you need to check the database server. Sometimes, the server stops responding due to heavy traffic. So in this case you need to contact the web hosting provider to make sure everything is working on the server.