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

“Error Establishing a Database Connection” occurs when your WordPress website can’t connect and retrieve information from the database. Its common causes include misconfigurations in the wp-config file, invalid credentials, or corrupted data.

If left alone, this database problem can harm your website’s uptime and usability. But don’t worry – we’re here to help you identify the root issue and resolve this error quickly.

This WordPress tutorial will explain all about “Error Establishing a Database Connection,” its possible causes, and tried-and-true solutions. We’ll also explore the best practices to prevent this error from happening again.

Error codeError establishing a database connection
Error typeDatabase error
Error causesMismatched information between wp-config and the WordPress database
Incorrect database credentials
Database corruption
Corrupted core files


“Error Establishing a Database Connection” is an issue that occurs when your WordPress site can’t connect and retrieve information from the database. It can cause a malfunction or downtime on your site.

The common causes for this error include mismatched configuration, invalid credentials, failed databases, and corrupted core files.

When someone visits your WordPress site, specific PHP commands fetch information from the database. This data will show up on the visitor’s browser, displaying the requested web page.

However, misconfigurations or data corruption can cause the database connection to fail. Since it can’t retrieve data, the WordPress “Error establishing a database connection” message will appear instead.

The error establishing database connection message

Note that this message might not show up if the web page cache is available, often making the error difficult to notice at first. However, an inaccessible admin dashboard can indicate this WordPress database connection error.

What Causes “Error Establishing a Database Connection” in WordPress

Several common causes for “Error Establishing a Database Connection” in WordPress include:

  • Incorrect database login credentials in the wp-config file. The WordPress database uses login information from your hosting provider. If you modify the MySQL database password and username, the old credentials in your configuration files will be invalid.
  • Corrupted database. Faulty plugins or incompatible themes can corrupt database tables and their content, resulting in errors.
  • Database server error. Issues on your web host’s infrastructure might cause a database error. For instance, traffic surges or permission issues can make the back end unresponsive.
  • Core file corruption. WordPress core files define how your site works and functions. Faulty code in plugins and themes can corrupt them, causing database failure.

The pop-up message can help you identify the culprit. For example, if you see the following error window on your WordPress site’s admin dashboard, the reason is likely incorrect database login credentials.

The error establishing database connection message with potential causes

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

After understanding the common reasons for the “Error Establishing a Database Connection” message, you will learn eight solutions to resolve it. Before proceeding, back up your WordPress site to avoid further damage due to misconfigurations.

1. Check Your Database Server

A database has a maximum number of connected clients. The server will drop connections if it exceeds the limit, causing downtime and preventing your website from fetching the requested data.

For this reason, a traffic surge commonly triggers “Error Establishing a Database Connection”. To resolve it, minimize your server back-end requests using caching plugins like W3 Total Cache or WP Rocket.

Network issues might also cause the error message. We recommend contacting your web host to check if all WordPress sites on the same server are experiencing the same issue. If so, the error is on the hosting provider’s end.

Another common cause for an unresponsive server is insufficient user permissions. If your WordPress database user doesn’t have the proper access permissions, it can’t connect and retrieve content from the back end.

To check if the database user has the correct privileges, access the MySQL server using PHP. Here’s how to do so with a Hostinger plan:

  1. Log in to hPanel. Select Website on the top menu and click Manage on the website you want to configure.
  2. On the website management page, select File manager.
The File Manager button in hPanel's website management menu
  1. Navigate to the public_html folder and click the Create a New File icon on the left sidebar.
  2. Create a new PHP file with any name. We will use test.php for this tutorial.
  3. Paste the following code into the file. Remember to replace the MySQL database password and username with the actual value.
<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
  1. Click the disk icon on the top right corner to save the file.
  2. Open a new tab in your web browser and access the file by entering the file name and path after the domain like so:
domain.com/public_html/test.php

If the WordPress debug script doesn’t return an error, your database user has sufficient permissions, and the connection should work normally.

php-test-script-indicates-successful-database-connection

Otherwise, adjust the user permissions to ensure a successful database connection. On hPanel, access it by navigating to DatabasesList of Current MySQL Databases And Users.

Click the three-dot icon next to the account and select Change Permissions. Check all the boxes and select Update. Rerun the debug script to check if the issue persists.

2. Check the Database Credentials

Incorrect login credentials are another common reason for the “Error Establishing a Database Connection” message. This usually happens after migrating a WordPress site to a new hosting provider.

To fix it, check if the database host information in your hosting control panel and wp-config.php file matches. If you can’t access your hosting control panel, use an FTP client like FileZilla to view your site files.

While the general procedure is similar, the steps might differ. For example, you must manually download the file, edit it, and reupload it. At Hostinger, you can directly do so in a web browser via the File Manager on hPanel.

Database information in wp-config file

After opening File Manager, locate the wp-config.php file within the public_html directory. Right-click on it and select Edit. Find the following database information:

  • DB_NAME – database name
  • DB_USER – database username
  • DB_PASSWORD – database password
  • DB_HOST – database host

Save this database host information in a safe location since we will compare them with the one on your hosting account. Then, return to hPanel and go to Database Management.

The database information in hPanel

Check all the database information under the List of Current MySQL Databases And Users section. If the credentials in your wp-config file and MySQL database settings match, but the issue persists, proceed to the next solution.

If the information doesn’t match, enter the correct database credentials from your hosting control panel. To do so, add the following code into wp-config.php:

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

Replace the placeholders with the correct information. Once finished, click the disk icon to save the changes. Refresh WordPress to see whether the “Error Establishing a Database Connection” on the website is resolved.

If the error persists, reset the database password from the List of Current MySQL Databases And Users section. Click the three dots icon → Change Password. Enter the new password and hit Update.

The new database password creation window in hPanel

If you used the correct database username and password, but the “Error Establishing a Database Connection” message still shows up, check the wp-config.php file’s DB_HOST value. It should be localhost, 127.0.0.1, or a URL, depending on your hosting provider.

Ask your hosting provider for the correct value. However, if you haven’t migrated your WordPress site recently, the MySQL hostname should not be the problem.

3. Fix Corrupted Files

Besides a faulty database, corrupted files can also trigger the “Error Establishing a Database Connection” message. It might be due to faulty themes or plugins since their code might conflict with the WordPress codebase.

To resolve the issue, deactivate the latest plugin or theme you downloaded since they are most likely the culprit. If you have several third-party software, we recommend disabling all plugins at once and turning them on individually.

Open your WordPress dashboard and click Plugins on the sidebar. Check the box on the top left corner to select all the plugins. Click the Bulk actions drop-down menu and select Deactivate. Hit Apply.

WordPress plugin bulk deactivation menu

For themes, you can’t disable all of them simultaneously via the WordPress admin panel. Instead, activate each of them individually by going to Appearance Themes on the dashboard.

If the database error prevents you from accessing the WordPress dashboard, temporarily deactivate all WordPress plugins by renaming the plugins folder via the File Manager or an FTP client.

For Hostinger users, go to File Managerpublic_htmlwp-content. Right-click on the plugins folder and select Rename. Enter a new name and confirm the changes.

 The rename folder option in Hostinger File Manager

After renaming the folder, your website’s back end and admin area should be accessible. Return to File Manager and restore the folders to their original names.

Important! You can disable all themes simultaneously by renaming the folder. However, unlike plugins, the default one will automatically reactivate when you revert it to the original name.

All the inactive WordPress plugins should now show up in the admin dashboard. Re-enable them individually to determine the faulty ones. Then, delete the problematic plugins by clicking Delete underneath their names.

If none of the solutions work, your content management system (CMS) core files might be corrupted. This means you must replace them with the default files from a fresh WordPress installation.

Download WordPress, extract the package, and open its content. Delete the wp-content folder and the wp-config-sample.php file. Then, upload the remaining items into your website’s root directory via File Manager or FTP.

Important! While you can install WordPress from scratch to restore the core files, we don’t recommend it since it will wipe your website data.

4. Repair the Database in WordPress

Another issue that might trigger “Error Establishing a Database Connection” in WordPress is a corrupted database. It often occurs from removing plugins or themes frequently, which can add redundant data that disrupts the tables.

Usually, your WordPress dashboard shows the “One or more database tables are unavailable. The database may need to be repaired” message if the database fails.

To run the database repair, insert the following function at the bottom of your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

Navigate to the public_html folder via File Manager. Right-click on the wp-config.php file and click Edit. Paste the function and click the disk icon to save the changes.

Next, open a new tab in your web browser and enter http://www.your-site-domain.com/wp-admin/maint/repair.php. You will be prompted with two database repair modes. Pick the Repair Database option and wait until the process is complete.

The WordPress database repair function screen

Return to your WordPress site to see if the problem is resolved. Once it is fixed, immediately remove the repair function from the wp-config.php since unauthorized parties might exploit it to access your back end.

5. Create a New Database

If previous methods don’t work, you might need to create a new MySQL database and restore the website data from a backup since the current one might be completely corrupted.

The steps differ depending on your web hosting company and control panel. To do this with a Hostinger plan, start by downloading the backup.

  1. In hPanel, open your website management menu.
  2. From the sidebar, navigate to FilesBackups.
  3. Select Database backups.
Backup selection menu in hPanel
  1. Choose the faulty database from the drop-down menu and click Show Database to display the available backups.
  2. Select a backup date you want to restore and click Download.
  3. Wait until hPanel finishes preparing the file. Once completed, click Download Backup.

We recommend downloading multiple backups in case the latest one has errors.

Then, create a new database and restore the data via phpMyAdmin:

  1. In hPanel’s website management menu, navigate to the sidebar → DatabasesManagement.
  2. Enter the new MySQL user, database username, and password. Click Create Database.
The database creation menu in hPanel
  1. Scroll down and click Enter phpMyAdmin on the newly created database.
  2. In the database management panel, switch to the Import tab.
  3. Click Choose File and select the database backup you have downloaded. Scroll down to the bottom and click Import.
phpMyAdmin database archive import menu

Lastly, connect the new MySQL database to your WordPress site by modifying the wp-config.php file. Open File Manager, navigate to the public_html folder, right-click on the item, and select Edit.

Enter your new MySQL user, database name, and password in the corresponding lines. Save the changes, and your WordPress dashboard should now work normally.

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

Switching to a new hosting provider will move your files to the new server. It might cause an error if your domain is not pointing to the correct database host, preventing the website from retrieving data.

To point your domain to the database host, check whether the nameservers in your hosting control panel and WHOIS match.

To check nameservers on Hostinger, go to hPanel WebsitesManageSee Details under your web hosting plan name. Alternatively, use an online tool like dnschecker.

The hosting plan See Details button in hPanel

If the information matches, your domain is pointing to the correct address and should work properly. If not, enter the correct nameservers from your hosting provider on your domain registrar control panel.

To change Hostinger domain nameservers, navigate to hPanel DomainsManage. Under the Domain Information section, click Change next to the nameservers.

Important! If both your domain and website are hosted at Hostinger, the nameservers should be automatically set to ours.

Alternatively, point your domain name to Hostinger via the A record – a type of DNS setting that assigns your domain to an IP. Note that this method only works if you are using a static address.

A record value of a domain name in a DNS lookup tool

To do this, retrieve your domain name’s A record value using an online DNS lookup tool. Go to your hosting account and check your server’s IP address. At Hostinger, you can find this information under the Plan details page.

Go to your registrar’s control panel and replace the A record value with the IP address from your hosting provider. Hostinger users can find it in the DNS Zone Editor menu.

7. Update WordPress Website URL

If the database connectivity error occurs after you migrate to another host, check whether your WordPress site URL has changed. This change can prevent MySQL from connecting and sending data to the front end, resulting in an error.

To resolve this, update your site URL in the MySQL database via phpMyAdmin. Hostinger users can find the panel access button by going to Databases Management in hPanel.

Select your WordPress database from the sidebar and click the SQL tab. In the provided field, enter the following:

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

Replace http://www.old-domain.com with your old site URL and replace http://www.new-domain.com with the new one. Scroll down and click the Go button to run the script.

The SQL query menu in phpMyAdmin

8. Alternative Methods to Fix WordPress “Error Establishing a Database Connection”

If the “Error Establishing a Database Connection” message persists after attempting the methods above, try the following alternatives.

Contact the Support Team

Ask your web hosting provider’s customer support to help resolve the database issue. They have access to your website’s back end and should be able to identify the root cause.

Depending on your web hosting provider, the customer support availability might differ. Hostinger’s Customer Success team is available 24/7 via live chat to help resolve your issues and answer your questions.

If your web host doesn’t provide round-the-clock support, look for solutions from other sources, such as the official WordPress forum. Remember not to give out sensitive information, such as your database credentials, when asking questions in public forums.

Reboot the Web Server

If you host WordPress and the MySQL server on a dedicated hosting environment, reboot it to refresh the settings. Restarting will terminate background services that might cause the connectivity error.

Hostinger VPS users can restart the web and database server by clicking the Reboot button on their VPS overview page.

Alternatively, connect as a root user via an SSH client like PuTTY and enter the restart command according to your operating system. For Linux, run:

reboot

If you don’t use a dedicated hosting environment, ask your WordPress hosting provider for assistance since the manual restart option might be unavailable.

Restore the Latest Backup

Restore both your database and website files to revert changes that might cause the errors. The easiest way is to use your web hosting control panel’s backup feature.

Hostinger users can restore the latest backup in a few clicks via hPanel. In the website management area, navigate to the sidebar → FilesBackups. Select Restore Website and choose the latest date.

Check the confirmation box to acknowledge that the backup will overwrite your website files. Confirm by clicking Restore.

To restore your database, go to the Database Backups menu and follow the same steps.

How to Prevent “Error Establishing a Database Connection”

The database issue can harm your website’s traffic and search engine optimization. After fixing the error, apply the following best practices to prevent it from returning:

  • Enable automatic backup. Regularly back up WordPress and its database to ensure you have a restore point if the issue returns. It simplifies troubleshooting and prevents data loss.
  • Optimize your WordPress database. Clean up your database using a plugin like WP-Optimize. Having too much data will slow down your website’s back end, potentially causing downtime and connectivity errors.
  • Install trusted themes and plugins. Only download plugins and themes from trusted sources like WordPress’ official library. Software from random sources might contain malware or untested code that might be incompatible with WordPress’ functionality.
  • Use a reliable web host. Host your WordPress site on a reliable hosting provider. Hostinger ensures high uptime for your database and provides dependable support to help resolve any issue.
  • Stay up-to-date. Subscribe to Hostinger’s blog to keep up with the latest WordPress release news. These updates often explain potential issues, allowing you to take preventive measures.
  • Regular monitoring. Routine checking of your WordPress site and database can help quickly catch issues before they cause further damage.

How to Fix Error Establishing a Database Connection – Video Tutorial

Do you prefer following a video tutorial to solve this WordPress issue? Check out our video tutorial on how to fix the “Error Establishing a Database Connection” message:

Subscribe For more educational videos! Hostinger Academy

Conclusion

The “Error Establishing Database Connection” issue occurs when your WordPress site can’t retrieve the requested data from the database. Its common causes include mismatched credentials, corrupted files, or server downtime.

In this article, we have explored some of the most common fixes for this error. First, try running a test PHP file to check if your database is running and the WordPress user has sufficient permissions. You can also ensure the MySQL hostname, username, and password on your hosting control panel and wp-config.php match.

If the error persists, try disabling plugins and themes with conflicting code and replacing corrupted core files with the new ones from the WordPress installation package.

If the above methods don’t work, you might have a faulty database. In this case, run the WordPress repair function or create a new database using backed-up data. If you recently changed your domain, update the URL via phpMyAdmin. Also, ensure it points to the correct nameserver using a DNS checker tool.

To prevent the issue from returning, regularly optimize your database using a plugin like WP-Optimize. Also, make sure to host your website with a reliable provider. With Hostinger, you will get 24/7 support and optimal website uptime.

Error Establishing a Database Connection FAQ

In this section, we will answer several questions about the “Error Establishing a Database Connection” issue.

Why Is My WordPress Database Connection Not Working?

If your WordPress site can’t connect to the database, the server might be experiencing downtime. It’s also possible the credentials in the wp-config.php file are incorrect. Another possible culprit is corrupted databases and core files. This often occurs due to conflicts between WordPress and plugins or themes.

Is It Possible for a Plugin or Theme to Cause a Database Connection Error?

Yes, untested plugins or themes can cause code conflicts that disturb the database connection. Frequently adding or removing them can also corrupt the stored data, causing the error. A potential solution is to uninstall them or create a new database if the table is corrupted.

Can Heavy Website Traffic Lead to Database Connection Errors?

Yes, the database host will be inaccessible if it can’t handle the amount of user requests. In this case, your website can’t retrieve data and content from the back end, and you will experience the error.

Author
The author

Nabilla R.

Nabilla is a website hosting and development enthusiast. She loves to share her knowledge with others in order to help them grow their online presence. When she's not busy writing, Nabilla enjoys exploring nature and going on adventures. She is passionate about helping people achieve their online goals.

Author
The Co-author

Aris Sentika

Aris is a Content Writer specializing in Linux and WordPress development. He has a passion for networking, front-end web development, and server administration. By combining his IT and writing experience, Aris creates content that helps people easily understand complex technical topics to start their online journey. Follow him on LinkedIn.