Error Establishing a Database Connection

How to Fix the Error Establishing a Database Connection in WordPress

Are you experiencing the ‘Error establishing a database connection’ message in WordPress?
This is a fatal mistake, rendering your website inaccessible to users. It occurs when WordPress cannot establish a connection to the database. Because this link can be affected by a variety of reasons, beginners may struggle to resolve the mistake.
In this post, we’ll walk you through how to solve the ‘Error establishing a database connection’ on your WordPress site step by step.

What Causes the Error Establishing a Database Connection in WordPress?

A database is software that facilitates the storage, organization, and retrieval of data from other applications.
WordPress, being a content management system, employs a database to store all of your website’s content and data. It then connects to the database whenever someone visits your website.
WordPress requires the following information when connecting to the database:
  • Your database name
  • Database username and password
  • Database server
This information is kept in your WordPress configuration file, wp-config.php.
If any of this information is wrong, WordPress will fail to connect to your database server, displaying the ‘error establishing a database connection’ message.
What Causes the Error Establishing a Database Connection in WordPress
This is one of the most frequent WordPress mistakes. Aside from improper credentials, this error might occur when the database server is offline or the database files are faulty.
Having said that, let’s look at how to solve the ‘Error establishing a database connection’ problem in WordPress using step-by-step debugging.

1. Check Your WordPress Database Credentials

The most common cause of the Error establishing a database connection issue is incorrect credentials. If you’ve just transferred your WordPress site to a new host, this is most likely the issue.

Your WordPress database credentials are saved in the wp-config.php file. The WordPress configuration file provides critical options, such as database information.
You will look for the following lines in the wp-config.php file.
				
					// ** 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 must ensure that the database name, username, password, and database host are valid. This information may be confirmed using your WordPress hosting account dashboard. For this article, we will use Bluehost, but the procedure will be identical for other hosting panels as well.

After login in, click on the ‘Website’ tab in the left column, then the ‘Advanced’ tab. Then, scroll down to the ‘Database’ area and select the ‘Manage’ option next to it.

This will bring up the MySQL Databases page in cPanel in a new window.

You can locate your database name and username under the ‘Current Database’ section. Remember to copy and paste these data into a notepad so you can add them to the wp-config file.
Check Your WordPress Database Credentials 2
Next, scroll down to the ‘Current Users’ column and click on the ‘Change Password’ link next to your database name.
This will lead you to a new screen where you may update the database password as you choose.
Check Your WordPress Database Credentials 3
Once you’ve validated all of your database information, you may edit it in your wp-config.php file as required. After that, check your website again to verify whether the Error establishing a database connection problem has been resolved.
If you can still see the error, it indicates that something else is missing.

2. Check Your Database Host Information

If you are positive that your database name, username, and password are right, you should ensure that you are using the correct database host information.
Most WordPress hosting companies utilize localhost as the database host. However, some managed WordPress hosting firms maintain databases on different servers. In such instance, the database host will not be localhost.

This implies you’ll have to contact your WordPress hosting provider to validate these facts. This will also help you to resolve the Error establishing a database connection. Rather than this If you want to update your wordpress security read this article.

3. Repair WordPress Database

You may accomplish this by adding the following line to your wp-config.php file. Make careful to put it just before the ‘That’s all, quit editing!’ Happy blogging!
				
					define('WP_ALLOW_REPAIR', true);
				
			
Once completed, you can view the settings at http://www.yoursite.com/wp-admin/maint/repair.php. Simply replace “yoursite.com” with your own domain name.
To start the procedure, click the ‘Repair Database’ option.
Repair WordPress Database 1
The user does not need to be logged in to view the database repair page. Once you’ve finished repairing and optimizing your database, be sure to delete this piece of code from your wp-config.php. If you don’t want to add any code to your site, you may restore your database using your hosting account’s cPanel.
Go ahead and access your hosting account, then select the ‘Websites’ tab from the left column. Then, navigate to the ‘Advanced’ page and select the ‘Manage’ option under the Database section.
This will open the cPanel in a new tab. Scroll down to the ‘Modify Databases’ section, then choose your database name from the ‘Repair Database’ selection option.
Once completed, simply click the ‘Repair Database’ button to begin the procedure.
The hosting control panel will now automatically restore your database. When the procedure is complete, you will get a success message.
Repair WordPress Database 4
You may now go to your WordPress site and examine if the Error establishing a database connection has been repaired.

4. Check If Your Database Server Is Down

If everything appears to be in order, but WordPress still has an error establishing a database connection, your database server (MySQL server) might be unavailable. This might happen if a server has a high volume of traffic. Your host server cannot manage the demand (particularly if you are using shared hosting).
As a result, your site may become slower, and some users may see an error message. In this instance, contact your hosting provider via phone or live chat and inquire if your MySQL server is responding.
Furthermore, if you have other websites operating on the same server, you may check them to ensure that your SQL server is down. If you do not have any other sites on the same hosting account, just navigate to your hosting dashboard and select the ‘Advanced’ option.
Following that, select the ‘Manage’ option next to the phpMyAdmin area.
Check If Your Database Server Is Down 1
This will open phpMyAdmin in a new window, and you must select the ‘Database’ option at the top. After that, choose your database’s name to view its options. If you can accomplish that, you should check to see if your database user has the necessary rights.
Check If Your Database Server Is Down 2
To accomplish this, create a new file named testconnection.php and put the content below into it:
				
					<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

				
			
When pasting the code, replace the username and password with your own. You may now upload this file to your website and view it using a web browser. If the script connected successfully, it implies that your user has the necessary rights and that the issue is caused by something else.
Now, go back to your wp-config file and examine it to confirm that all of the information is correct and free of mistakes. This will resolve the error establishing a database connection.
Scroll to Top