How to Fix the Error Establishing a Database Connection in WordPress in 2023

Have you seen “Error Establishing a Database Connection” error on your WordPress Website? It is a common issue that occurs on WordPress. This error comes when the website refuses to connect to the WordPress database.

If you not resolve it then website it will make you inaccessible to the users and lose traffic, causing damage to its reputation in the process.

This article will help you understand the number of things which can affect your WordPress database connections to get errors. We also show you how to easily fix the “Error Establishing a Database Connection” error and how to debug it in WordPress, and explain what the possible causes for it are.

Before we can go into the first step of fixing this problem, we must know exactly what this WordPress database error means.

Fix the Error Establishing a Database Connection
Fix the Error Establishing a Database Connection

What Causes Error Establishing a Database Connection in WordPress?

The “Error Establishing a Database Connection” error message appears when WordPress cannot connect to the database. This is due to incorrect database information in website WordPress settings, corrupt database, or an irresponsive database server.

Originally, WordPress is written in PHP and uses MySQL as its database system. Website uses this database to store, organize and retrieve data store in it. When user visits your site, specific PHP commands fetches your site information from the database, which will then be displayed on the visitor’s browser.

For this WordPress needs the following information for connecting to the database:

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

If any one from this corrupt or incorrect, WordPress would fail to connect to your database server. Because of this there will be no data to retrieve and display on the browser, the WordPress “Error establishing a database connection” message will appear on a blank page. This message will also come when database server will go down or database files becomes corrupt.

Error establishing a database connection message in WordPress

If there will be cache of your website, this error message will not appear on the users screen. But WordPress database connection error will blocks your access to the WordPress dashboard. So considering this it is essential to determine what causes this error and fix that accordingly.

Lets take a look and Keep reading to the next section to find out how to fix error establishing database connection issue in WordPress

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

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.

1. Make Sure Your Domain Is Pointing to Your Host Provider | Check Your Database Host Information

When you switch to a new hosting provider make sure all your files are moved to the new host. However if it moves successful then make sure that your domain is pointing to your host where the database is hosted, else it will cause to database error.

To do so, check the nameservers through your hosting control pannel 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 Your Database Server | Check if Your Database Server is Down

If everything seems to be correct, still you get “Error Establishing a Database Connection” message then your WordPress site when your MySQL server may be down. This will cause due to heavy traffic on a server.

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.

public_html folder, with the New file option highlighted

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 website caching with plugins like W3 Total Cache or WP Rocket is a good way to reduce the load on your server.

3. Check the Database Login Credentials

Invalid Database Login Credentials is the most common reason for the WordPress “Error Establishing a Database Connection” message. This will happen if you have moved your WordPress Website to a new hosting provider.

Your Website database login settings are stored in the  wp-config.php file. It is the most important configuration file of WordPress which contain important settings of WordPress including database information.

To fix “Error Establishing a Database Connection” problem, check if the database login settings are correct and match with the ones stored in your wp-config.php file. You need to make sure information such as database name, username, password and database host is correct.

For this You can access this file via an FTP client or the WordPress File Manager in your hosting control panel.

hPanel

If your have web hosting from the Hostinger, following troubleshooting steps will show you how to access the wp-config.php file via Hostinger’s File Manager. To start, access your hPanel dashboard, and on the sidebar, go to Files  File Manager.

File Manager in hPanel

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.

public_html directory with the wp-config.php file selected

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
The database name, username, and password in the wp-config.php file.

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

The list of currently created MySQL databases on hPanel

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 file, refresh WordPress to see whether the “Error Establishing a Database Connection” on the website 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 establishing a database connection” 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 information.

wp-config.php file, with the database host information highlighted

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 the database 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 database 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 database credentials and update the values on wp-config.php accordingly.

4. Fix the Corrupted Files

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

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 fix the “Error Establishing a Database Connection” problem, it 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.

Renaming the plugins folder located in the the wp_content folder

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 troubleshooting 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.

Important! 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. Repair the Database in WordPress

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.

The database repair function on the wp-config.php 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 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

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.

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.

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

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.

Bonus Methods to Fix WordPress “Error Establishing a Database Connection”

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 have 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 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.

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 website backup available.

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 – we’ve summarized below.

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.

This database error occurs as a connection to the server cannot be established by the WordPress website. 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.

Leave a Comment