How to fix the Error Establishing a Database Connection in WordPress

How to fix the Error Establishing a Database Connection in WordPress

Looking for a way to fix the Error Establishing Database Connection in your WordPress website? Here in this article, we will show you the easiest way to fix this error.

WordPress has many common errors that can suffer you a lot, Error Establishing a Database Connection is one of the fatal errors that make your website inaccessible to your users. If your WordPress website is not able to make a connection with your database this error will show up. If you are an expert in WordPress then you are familiar with this type of error and easily fix this. But beginners can not easily troubleshoot to fix this error.  This article is for beginners looking for an easy solution for Error Establishing a Database Connection.

Why does Error Establishing a Database Connection occur?

Earlier we mentioned that this type of problem occurs when your website fails to make a connection with your database. So the wrong database setting, a corrupt database, or a database server error are mainly responsible for this error.

The database helps you to store your website data and information in an organized way and send the data to the users when requested. To connect your database with your WordPress website, WordPress needs to access the database name, username, password, and database server. WordPress stored all this information in the wp-config.php file.

If any of this information is wrong or corrupted then WordPress won’t be able to connect with the database and show the Error Establishing a Database Connection error.

Fix the Error Establishing a Database Connection in WordPress

To solve this problem properly you need to check many things. Here we will try to cover all the elements you need to check to fix this error.

Database Credentials

The first element you need to check is your WordPress website database credentials. Incorrect database credentials are one of the major reasons for this error. Sometimes changing the hosting provider can create a conflict between database credentials and your website that can cause the database error issue.

As we mentioned earlier WordPress stores all the database information and important settings in the wp-config.php file. So you need to check this file and make sure that your database name, username, and host all information are correct. To access the wp-config.php file you need to connect your website with an FTP client. After that, you can find the file in the wp-includes folder. wp-includes folder located in the same directory where wp-content and other folders are located.

After accessing the file look for this line where you will find the database name, username, and hostname. Make sure that all the information is correct.

// ** MySQL settings - You can get this info from your web host ** //

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

You can find all this information in your hosting account. Login to your hosting account and match all the information with your database.

Check database host information

If your database information is correct in WordPress wp-config.php file but you see the database error then you need to check for the database host information. Because most of the time hosting providers use localhost as a database host but some companies are not using localhost as a database host. They have a separate server for your database host. So you need to ensure you are using the correct database host information.

If your hosting provider uses a separate server and you are using the localhost information then your website won’t be able to connect with the database. So contact your hosting service provider to get the right information.

Check for the database server is down

If you are using the right database host information, and the right database information but still face the problem then your hosting server may be down. If there is an error in your hosting server that can also be the reason for the database connection error. If there is too much traffic your hosting server can not handle then it goes down and you will face the database error. So make sure that your hosting server can handle traffic pressure.

Repair WordPress database

If you see the message “Database is unavailable” in your admin area, you need to repair your database. To repair your database add this line at the end of the wp-config.php file.

define('WP_ALLOW_REPAIR', true);

This code will repair your database. After repairing make sure to delete this code and save the file.

Rebooting web server

If all the mentioned processes are not able to fix the problem then you need to reboot your web server. Rebooting the web server can fix the temporary issues in your database.

Wrapping Up

Following the process, you will be able to fix the Error Establishing a Database Connection in your WordPress website. You can see our other articles to learn What to do when you are locked out of WordPress admin

How to turn off comments notifications in WordPress

How to fix Upload Failed to Write File to Disk error in WordPress

We hope this article will help you. If you like this article please like our Facebook page to stay connected.