How to Fix “Error Establishing A Database Connection” in WordPress

How to Fix “Error Establishing A Database Connection” in WordPress

A database is software that organizes, stores, and retrieves data. The WordPress database stores all of the site content, from WordPress posts and pages to login credentials and plugin settings, for use by the CMS (Content Management System).

However, sometimes a WordPress site fails to connect to its database. Hence, the “Error Establishing a Database Connection” message can appear on your website. If you’ve no clue what this means, don’t panic just yet – the issue is actually fairly common.

Simply put, this database connection error message indicates that your site has failed to retrieve the information for rendering the page. If left unattended, a site will become inaccessible and lose traffic, causing damage to its reputation in the process.

This article will help you solve this WordPress error and explain what the possible causes for it are. Before we can jump into the first step of fixing this problem, however, we need to know exactly what error means.

What Exactly Is “Error Establishing a Database Connection”?

WordPress is written in PHP and uses MYSQL as its database system. When someone visits your site, specific PHP commands will fetch your site information from the database which will then be displayed on the visitor’s browser.

The “Error Establishing a Database Connection” message appears when PHP fails to access the WordPress database. Since there is no data to retrieve and display on the browser, the message pops up on a blank page.

Error establishing a database connection message

Fortunately, this error message doesn’t appear on the front-end as long as the cache of your website is available. However, this database error blocks your access to the WordPress dashboard. With that in mind, it’s essential to determine what causes this error and fix that accordingly.

Keep reading to the next section to find out various possible reasons triggering this database error.

Reasons Behind “Error Establishing a Database Connection”

A few possible reasons that can cause the “Error Establishing a Database Connection” in WordPress include:

  • Incorrect database login credentials. Keep in mind that the WordPress database uses different login information from your hosting panel. If there is any change to your database username and password, the old login information stored in your WordPress files will be invalid.
  • Corrupted database. Issues like plugin failures and incompatible themes can cause corrupted database tables or missing information. When any database information is deleted or corrupted, it will result in an error.
  • Database server error. Sometimes the problem comes from your web hosting end. For example, a sudden surge of traffic or permission issues can make your database unresponsive. This usually happens in a shared hosting environment where multiple websites use the same resources.
  • Core files corruption. WordPress core files define how your site works and functions. Many reasons can lie behind corrupted files, from faulty plugins and themes to issues when transferring via an FTP client. When modified incorrectly, the core files can also get corrupted and cause failure in the database.

If you see the following window on the back-end of your WordPress site, the problem likely comes from invalid login credentials.

A window showing Error establishing a database connection and its possible causes

However, if a different error message appears, then the cause might be a corrupted database or a traffic spike. Luckily, fixing this database error only takes a few steps – the next section will explain different approaches to fix this issue.

How to Fix the WordPress “Error Establishing a Database Connection”

Below are the six most common solutions for fixing the WordPress “Error Establishing a Database Connection” error. Before you start dealing with the error, make sure to create a backup of your site. If things go wrong, you can use it to restore your site.

Quick Methods to Fix the Error

  1. Make sure your domain is pointing to your host provider
  2. Check the database login credentials
  3. Repair the WordPress database
  4. Fix the corrupted files
  5. Check your database server
  6. Create a new database

1. Make Sure Your Domain Is Pointing to Your Host Provider

Switching to a new hosting provider will automatically move your files to the new host. However, if your domain is not pointing to the correct host where the database is hosted, it will cause database failure.

To do so, start by checking the nameservers through your hosting control panel and WHOIS lookup tool. Hostinger users can access the hPanel dashboard and go to Accounts -> Details to accomplish this. If both nameservers match each other – your domain is pointing correctly.

If the results show otherwise, change the nameservers at your domain registrar. Simply access your hosting dashboard to get a list of the nameservers and enter them into your domain registrar’s control panel.

Another option is to point your domain name via the A record. It’s a type of DNS record that assigns your domain to its correct IP address. Keep in mind that the A record only points to a static IP address.

To perform this alternative, find your current IP address or the A record value and replace them with the ones from your new web host. Do so by using a local computer console or an online DNS checker. Hostinger users can find the IP address through the hPanel on Hosting -> Manage.

A window showing website's DNS records and A record value. You can see whether your domain name has been pointed correctly

In the example above, the website’s A record value is 123.4.567.89. If the value matches your new hosting IP address, your domain name has already been pointed correctly. Otherwise, you need to update it from your domain registrar’s control panel.

2. Check the Database Login Credentials

The most common reason for the “Error Establishing a Database Connection” message is invalid login credentials. Usually, this happens after a site migration to a different hosting provider.

To fix this problem, check if the database login settings match with the ones stored in your wp-config.php file. You can access this file via an FTP client or the WordPress File Manager in your hosting control panel.

hPanel

The following steps will show you how to access the wp-config.php file via Hostinger’s File Manager. To start, access your hPanel dashboard, and go to Files -> File Manager.

A window explaining where to find hPanel File Manager

Clicking on it will take you to a page containing a list of your site files. From there, navigate to your public_html directory and locate the wp-config.php file. Next, right-click on the file and select Edit.

Site files window showing where is your wp-config.php file and where to press Edit

This will open a set of database information and configurations. Find and take note of the following data:

  • DB_NAME – database name
  • DB_USER – username
  • DB_PASSWORD – password
  • DB_HOST – database host
Data from Edit file showing database's information, including its name, username, password and host

After that, navigate back to your hPanel and go to Database -> MySQL Databases. There, check your current database information under the List of Current MySQL Databases And Users section.

A window showing a list of current MySQL databases and users

If the login data in your wp-config.php file is different from the MySQL database settings, match them by modifying the wp-config file.

Then, edit these lines:

define('DB_NAME', 'fill_this_with_your_current_database_name');
define('DB_USER', 'fill_this_with_your_current_username');
define('DB_HOST', 'fill_this_with_your_current_localhost');

Once you’ve edited the wp-config.php file, refresh your WordPress site to see whether the “Error Establishing a Database Connection” is now resolved. If the error still persists, the database password could still be false. In this case, you need to reset your MySQL password.

Simply head over to the List of Current MySQL Databases And Users section, click on your current database, and select Change Password.

A window showing how to change password for your current database

After entering the new password, hit the Update button.

Change MySQL user password window showing the update button

If your database username and password are correct, but you still receive the error message, change your database host information in the wp-config.php file.

With WordPress, web hosting companies tend to use localhost as the database host. However, we recommend contacting your web hosting company to confirm your database host info.

Edit file window showing the your current database's host

If you have recently switched hosts, double-check the DB_HOST details. In most cases, localhost won’t change, but some web hosts use an IP Address or different URL.

cPanel

For cPanel users, the steps are more or less the same. To open your wp.config.php file, go to Files -> File Manager. Then, click on the public_html directory and locate the file.

Once you have the information, check whether the value of DB_NAME matches your database name through Databases -> phpMyAdmin.

Window showing databases where you can choose phpMyAdmind

On the left side of your window, you will see your database name along with information_schema. If the name matches the DB_NAME value on your wp-config.php file, then your database name is not causing the problem.

phpMyAdmin page directing to click on database name

If the name doesn’t match, edit the value in wp-config.php directly using cPanel’s file manager.

However, if your database name is correct, but the error persists, the problem might be your database username and password. To fix these credentials, create a new PHP file under your root directory.

Name the file as you please, for example, new-database-file.php.

New File window showing where to write the new file name for your database

Then, copy the following code:

<?php
$link = mysql_connect('localhost', 'user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Success establishing database connection';
mysql_close($link);
?>

Replace the values of ‘user’ and ‘password’ according to your credentials. Next, test the file by accessing the following page on your browser: http://your-site-domain.com/new-database-file.php.

If your username and password are correct, the following window will appear on your screen:

A message confirming that database connection has been established successfully

Once your database is back to working properly, don’t forget to delete the file. However, if your database login credentials are wrong, this window will appear instead:

A message informing that there has been an MySQL Error and the access is denied

In this case, create a new database user through the Databases -> MySQL Databases menu. Enter your new credentials and update the values on wp-config.php accordingly.

3. Repair the WordPress Database

Another fault that may result in the “Error Establishing a Database Connection” in WordPress is a corrupted database. This can be caused by data failure from plugins or themes that are regularly added or removed.

You might have a corrupted database if your WordPress dashboard shows a message along the lines of “One or more database tables are unavailable. The database may need to be repaired.”

Fortunately, you can easily fix this problem by inserting the following function into the bottom of your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

hPanel

Hostinger users can simply access the File Manager, go to the public_html folder, and edit the wp-config.php file.

Edit file window showing where to insert the function in the wp-config.file

Input the code, and don’t forget to save the changes. Next, go to http://www.your-site-domain.com/wp-admin/maint/repair.php on your browser.

There will be two options to repair your database – choose the one that fits you better.

A window of WordPress allowing to choose repair database or repair and optimize database

After the repair process is over, check your WordPress site and see whether it’s back to normal. If so, go to your wp-config.php file and remove the function immediately. This prevents any website abuse as anyone can access the repair page without logging in.

cPanel

The same steps apply to cPanel users. Go to Files -> File Manager, open the root directory, then add the function above to the wp-config.php file.

Aside from editing the wp-config.php file, cPanel users can benefit from its built-in functionality to repair MySQL databases. Start by navigating to Databases -> MySQL Databases.

A window of databases directing to choose MySQL databases

Under the Modify Databases section, you will see two options to check or repair your database. To fix a corrupted database, choose the Repair Database option.

Modify databases window showing where to choose Repair Database option

Wait for the process to finish and access your WordPress site to see whether the ”Error Establishing a Database Connection” message still appears.

Another alternative is repairing database tables from phpMyAdmin. To do so, simply go to Databases -> phpMyAdmin. Click on your database name and check all tables.

phpMyAdmin window showing database tables

Then, click on the dropdown menu next to the Check all button and select the Repair table option.

phpMyAdmin window showing how to repair table

It will trigger the REPAIR_TABLE statement to start and fix any corrupted table.

4. Fix the Corrupted Files

Besides corrupted databases, corrupted files can also be the reason behind “Error Establishing a Database Connection” in WordPress.

A faulty theme or plugin is one of the factors that cause file corruption. To identify which component is causing the error, check them separately. Your theme might be the problem if the error happens after activating a new one.

To resolve this problem, navigate to Appearance -> Themes on your WordPress dashboard and then switch to another theme.

Appearance section in the WordPress dashboard

If changing the theme doesn’t resolve the problem, the error might come from a corrupted plugin. In this case, try disabling all of your plugins and enabling them one by one to find the culprit.

Alternatively, you can deactivate all WordPress plugins by renaming the corresponding folder from your hosting control panel. Here’s how to do so.

hPanel

Go to the File Manager, open the public_html directory, and click on the wp_content folder. Then, rename the currently used plugins folder.

File Manager window showing how to choose plugins and rename them

cPanel

Navigate to the File Manager menu on your cPanel dashboard. Click on public_html -> wp_content and locate the plugins folder. Right-click on the folder, then choose Rename.

cPanel window showing how to rename plugins folder

Doing so will disable all of your plugins at once. The same steps also apply to disabling all WordPress themes. However, make sure not to disable both the themes and plugins at the same time as this will make it much harder to figure out the root of the problem.

If the error disappears after renaming the plugins folder, it means that one of the plugins is the issue.

Once you’ve regained access to your site’s back-end, restore the plugins folder to its original name. This way, all of the WordPress plugins will still be inactive, but you can activate each one from the admin area.

Don’t forget to delete the WordPress theme or WordPress plugin that causes the error.

That said, if none of the steps above work, try re-uploading the core files onto your WordPress site. To do this, download a fresh version of WordPress.

WordPress window showing where to download WordPress

When the download is complete, unzip the package. Inside the folder, delete the wp-content folder along with the wp-config-sample.php file.

WordPress download folder showing wp-config-sample.php file to delete

After deleting them, open an FTP client and upload the remaining WordPress files onto your root folder. This should now safely overwrite all of the corrupted files on your server.

5. Check Your Database Server

The “Error Establishing a Database Connection” message can also appear on your WordPress site when your MySQL server is down. While many reasons can cause this error, usually, too many connections are the problem.

A database server has a maximum number of clients connected at a certain time.

When it reaches that maximum capacity, it might fail to handle more connections. For this reason, we recommend contacting your hosting provider to check your MySQL server.

If you host more than one site on the same server, you can also see if your MySQL server is down on those sites as well. Meanwhile, if you host a single site, use PHP to connect to MySQL and check if your WordPress database user has sufficient permission.

Navigate to your hosting control panel and access your public_html folder. There, create a new PHP file and name it whatever you want. In this example, we’re going to name the file test.php.

New file window showing the new file's name and how to create it

Paste the following code into the file.

<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

Replace the database password and username with your credentials. Lastly, save this file to your website and access it through the web browser.

Confirmation message saying that the connection has been successful

If it’s connected successfully, it means that your user has enough permissions, and something else is causing the “Error Establishing a Database Connection” message to appear.

Usually, a website on a shared server tends to experience database errors due to splitting resources. Performing web caching with plugins like W3 Total Cache or WP Rocket is a good way to reduce the load on your server.

6. Create a New Database

If all of the methods above still yield no results, your database might be corrupted entirely and cannot be used anymore. In this case, you need to create a new MySQL database and user from your hosting control panel.

Once a new MySQL database is created, restore your website backup to ensure you don’t lose any essential website data.

hPanel

hPanel users can simply head over Databases and click MySQL Databases. On the top page, there is a section to create a new MySQL database and database user.

A window showing how to create a new MySQL database and database user

Enter your database name, username, and password, then click the Create button. Keep in mind that the database password should contain at least eight or more characters with uppercase and lowercase letters.

Once you’re done, the new database will appear on the List of Current MySQL Databases and Users. You can then manage the database and complete multiple tasks, such as creating a backup.

A window showing how to enter phpMyAdmin in the list of current MySQL databases and users

cPanel

On cPanel, go to Databases -> MySQL Databases. You will see the Create New Database section on top of the page. Start filling out your new database name and click Create Database.

A window showing how to create new database on cPanel

The system will then add your new database to the list of Current Databases.

cPanel window showing current databases

You can also create a new MySQL user from the same window and assign it to your new database. Users can perform various tasks, from creating new tables to updating rows, depending on the privileges.

Simply enter your new database username and password on the MySQL Users section, then click Create User.

cPanel window showing how to create a new user for MySQL users

Once a new user is created, scroll down to add the user to your new database.

cPanel window showing how to add created users to the database

Bonus Methods

If none of the listed methods above work, there are some other steps you can take to fix “Error Establishing a Database Connection” in your WordPress website.

Update WordPress website URL

For those who just recently moved to new web hosts, you might need to update your WordPress URLs. To do so, navigate to phpMyAdmin from your hosting dashboard. Then, access your WordPress database and click on the SQL tab.

A window showing WordPress database's SQL tab

Copy and paste the following code on to the provided field:

UPDATE wp_options SET option_value = replace(option_value, 'http://www.old-domain.com', 'http://www.new-domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.old-domain.com','http://www.new-domain.com');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-domain.com', 'http://www.new-domain.com');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.old-domain.com','http://www.new-domain.com');

Be sure to replace the values of ‘http://www.old-domain.com’ and ‘http://www.new-domain.com’ to your new and old website URLs accordingly. When you’re done, click the Go button.

Ask for help

We always recommend consulting with experts. Don’t hesitate to contact your web hosting customer service. They should be able to troubleshoot further the database connection problem you are having.

Here at Hostinger, we provide 24/7 live chat support that will assist you in fixing this problem. To contact our support team, just log in to your account and click on the purple button at the bottom right corner of your screen.

Customer support logo

People who don’t have an account can submit questions to our General Inquiry channel.

Reboot the web server and restore backup

Users on dedicated servers, local servers, and VPS (Virtual Private Servers) can also reboot their servers. This will restart your web and database server, which may fix some errors. You can do this by using a root account and SSH clients, like Bitvise or Putty.

In other cases, you might need to restore a backup of both your database and WordPress files. Check to see if your web host has an automatic backup and download the most recent backup available.

Conclusion

Getting the “Error Establishing a Database Connection” error on your WordPress website can be frustrating. The website won’t load, causing a risk of losing traffic and customers.

The error occurs when your WordPress site fails to retrieve information from its database. It might be caused by different reasons, such as incorrect login credentials in the wp-config.php file, corrupted files, or server overload.

Fortunately, there are several simple ways to solve this error:

  1. Point your domain to the correct host – make sure your A record matches your web host’s IP address.
  2. Verify your login credentials – match the login credentials in your wp-config.php file with the ones in your MySQL Database.
  3. Repair database – by inserting the define(‘WP_ALLOW_REPAIR’, true); function into the wp-config.php file.
  4. Fix the corrupted files – deactivate corrupt themes or plugins via your WordPress dashboard or download the latest version of WordPress and re-upload your core files.
  5. Check your MySQL server – create a new file containing the mysqli_connect function to check if your database user has enough permissions.
  6. Create a new database – start over with a new MySQL database and import a backup.

We hope this article has helped you fix the WordPress “Error Establishing a Database Connection” issue on your website. Identify the possible cause of this error, follow the steps accordingly, and you won’t have to worry about this error anymore.

Author
The author

Nabilla R.

Nabilla is a WordPress expert and a digital content writer at Hostinger. When she's not busy writing, she loves to explore nature.