Last updated on May 31, 2023
In the early days of my career, I often faced errors establishing a database connection. If you are facing the same error, don’t worry about it because the reason can be found by following a few steps. In this article, we will learn to fix it.
There can be multiple reasons that cause this error which can be a PHP file error, function, or server error. 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.
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
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.
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.
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.
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.