Web Technical & Issues

Fix the Error Establishing a Database Connection in WordPress In Just 5 Ways

How to Fix error establishing a database in WordPress

When WordPress website displays the

‘Error establishing a database connection’

message, this is a fatal error that prevents users from accessing your WordPress website. When WordPress cannot connect to the database, you will receive this error.

Many factors can disrupt your WordPress database connection, making troubleshooting difficult for beginners. We are going to guide you to easily fix the error establishing a database connection in WordPress.

How to Fix Error Establishing a Database Connection in WordPress

What are the causes for “Error Establishing a Database Connection in WordPress”?

Inaccurate database information in your WordPress settings, a corrupt database, or an unresponsive database server can all cause the ‘Error establishing a database connection’ error.

Error establishing connection at site

A database is a piece of software that allows you to easily store, organize, and retrieve data into other software.

WordPress, as a content management system, stores all of your website’s content and data in a database. Each time someone visits your website, it connects to the database.

WordPress requires the following information in order to connect to the database:

  • Database server
  • Database name
  • Database username
  • Database password

The above information is stored in WordPress configuration file which is wp-config.php.

In the event that any of these settings are incorrect, WordPress will be unable to connect to the database server that you have specified, and you will receive the error message “Error establishing a database connection.”

It is among the most typical WordPress mistakes. Incorrect credentials aren’t the only thing that could cause this error; the database server could also be down, or the database files could be corrupted.

So how do you fix the “error establishing database connection” issue in WordPress?

Let’s follow our step by step guide to troubleshoot this error.

 

1. Make Sure Your Credentials Are Correct in the WordPress Database

Incorrect database credentials are the most common cause of a database connection error in WordPress. If you have recently moved your WordPress site to a new host, then this is the most likely explanation for the problem.

Your WordPress database credentials are stored in the wp-config.php file. Important WordPress settings, such as database information, are stored in this file, which is known as the configuration file for WordPress.

Log in to your hosting server via the cpanel or hpanel, depending on your host. Then head over to the file manager, and then over to the public_html folder. Look for the wp-config.php files and edit the file.

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

Check to see that the information for the database name, username, password, and database host is correct.

This information can be confirmed by checking your hosting account dashboard, databases and on the MySQL databases.

phpmyadmin

This will take you to the page in your hosting dashboard that manages your databases when you click it. You will be able to learn the name of your database as well as the username from this location.

MySQL databases

Following that, you will see a list of database users and links that allow you to change each user’s password.

Current User

Having confirmed your database name, username, and password, change that information in your wp-config.php file whenever necessary.

Visit your site once more and verify that the database connection error is now fixed.

If the error still occurs, this could mean that something else is causing the error.

Then head on to the next troubleshooting steps.

 

2. Make Sure Your Database Host Information Is Correct

If you are certain that the details regarding your database name, username, and password are accurate, then you might want to check that the information regarding your database host is accurate.

The majority of WordPress hosting providers utilize localhost as the database host for their customers. However, some managed WordPress hosting providers host databases on separate servers from the rest of their sites. In that case, the information regarding the host of your database will not be localhost.

You will need to get in touch with the WordPress hosting company in order to verify the information about your database host.

 

3. You May Need To Repair Your WordPress Database

If you are experiencing a different error when logging into the wp-admin, for example, something along the lines of “One or more database tables are unavailable,” then continue reading this article. If you received an error message that read “The database might need to be repaired,” then you need to fix your database.

You can do this by adding the following line in your wp-config.php file. Be sure to add it right before the sentence that says “That’s it, stop editing!” Happy blogging’ line wp-config.php.

define('WP_ALLOW_REPAIR', true);

After you have completed those steps, you will be able to view the configuration options by going to the following page: http://www.yoursite.com/wp-admin/maint/repair.php.

Repair Database

Please take note that it is not necessary for the user to be logged in in order to access the database repair page. As soon as you have repaired and optimized your database, remove this code from your wp-config.php.

 

4. Make Sure To Check if Your Database Server is Down

If everything checks out fine, but WordPress is still unable to connect to the database, then the problem may lie with your database MySQL server.

The high volume of users accessing a server might cause this to occur. Simply put, the load that is being placed on your host server is too great (especially when you are on shared hosting).

Your website is going to become unbearably slow, and for some users, it could even display an error. The most important step for you to take at this point is to contact your hosting provider via phone or live chat and inquire as to whether or not your MySQL server is responsive.

You can verify that your SQL server is down by visiting any other websites that are hosted on the same server as your database management system (DBMS).

If you do not have any other websites using the same hosting account, all you need to do is go to the dashboard for your hosting service and attempt to access phpMyAdmin and connect the database.

If you are able to connect, you will need to check to see if the user of your database has the appropriate permissions. Make a new file and save it with the name testconnection.php.  Inside that file, paste the following code:

Test Database

Make sure that the user name and password have been updated. This file can now be uploaded to your website, and you can access it through your web browser.

If the script was able to establish a connection without error, then it indicates that the permissions granted to your user are adequate; however, there may be another issue.

Return to the wp-config file that was created on your WordPress installation and check to see that everything there is accurate (re-scan for typos).

 

5. Other Solutions That You May Consider Doing

If the troubleshooting tips that were mentioned above were unable to fix the database connection error that was occurring on your website, then you can try the additional steps that are listed here.

1. You May Need To Do An Update To Your WordPress Site URL

Using phpMyAdmin, see if you can update the URL for the WordPress site. You just need to navigate to phpMyAdmin through the dashboard of your hosting account, and then select your WordPress database.

Update WordPress Site URL

After that, select SQL from the menu along the top, and then enter the MySQL query that is provided below.

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Don’t forget to provide the URL of your own site, and if you’ve changed the prefix of the WordPress table names, change wp_options to the name of your own table.

2. You May Need To Reboot The Web Server

Users who are logged into dedicated servers, local servers, or virtual private servers (VPS) can attempt to reboot their respective servers.

Your web and database servers will be restarted as a result of this action, which may resolve any temporary issues that have been causing the error.

3. You May Need To Ask For Support From Your Hosting Company

If none of the other troubleshooting steps work, you will need to get in touch with the company that hosts your website. Every reputable WordPress hosting provider will assist you in troubleshooting the issue, point you in the right direction, or even fix it for you if necessary.

You can also hire us help you fix this issue for reasonable rates. 

Hope that this article has been of great help to you.

Leave a Reply

Your email address will not be published. Required fields are marked *