Fix error message "Error establishing a database connection" in WordPress itself

Table of contents
WordPress slow?
We'll sort it out for you!
Cover picture Error when establishing a database connection
Table of contents

Are you getting a white screen of death with the error message "Error establishing a database connection" when you visit your WordPress website? This is one of the most common errors on websites and can (usually) be fixed quickly.

Error message Error when establishing a database connection

So don't worry: This step-by-step guide will show you how to fix the "Error establishing a database connection" error.

To test the various proposed solutions for your website, you need:

  • Access to the WordPress hosting your website
  • Depending on the hoster: FTP access
  • Depending on the hoster: Access to phpMyAdmin

In the following, we will go into the solutions to the various causes of the error message. In the vast majority of cases, it is due to incorrect database user data in wp-config.php. Let's start with that right now!

Note 💡: Before you start with the various troubleshooting options, we recommend that you carry out a Backup your WordPress website to create.

Incorrect database user data stored in wp-config.php

As WordPress hoster we regularly support our customers in resolving the error "Error when establishing a database connection". In 98 % of the support cases, the website operators receive the error message due to incorrect database user data in wp-config.php.

The "wp-config.php" file is a standard file that is present in every root directory of a WordPress instance.

In "wp-config.php you will find four database details that must match the database used. If one of the four entries does not match, the white screen of death will be displayed on your website.

1. database name

Database name wp-config.php

2. database user name

Database user name wp-config.php

3. database password

Database password wp-config.php

4. host name of your database

Database hostname wp-config.php

In summary, the details of the database that the wp-config.php wants to access look something like this:

Database wp-config.php

Instructions: Check and adjust the user data

To check the user data for correctness, you need access to:

  • Your wp-config.php, which is located in the root directory of your website
  • The user data of the database of your WordPress website

Note 💡: We'll show you how to customize the database user data in your hosting interface at WPspace. You don't need FTP access to do this.

1. check the database details in wp-config.php

The first step is to copy the database data from wp-config.php so that you can compare it with the "real data" afterwards.

Log in to the WordPress hosting of your website and navigate to "Files" via the left menu. You are now in the base directory of your website.

By default, you will now select the "httpdocs" folder. There you will already find the necessary file "wp-config.php".

Website file directory

Click on wp-config.php to open it. There you will find the "Database settings". Make sure you save the complete section on the database settings in a Word document or similar.

Database details

2. check names and users of the database

After you have successfully copied the data from wp-config.php, the first step is to compare the name and user of the database.

To do this, navigate to the menu item "Databases". If you have several databases here, scroll to the database with the name (DB_NAME) specified in wp-config.php.

Now compare the DB_NAME and the DB_User with the information in the database. This is the cause in most cases.

Compare database data

✅ If these match, you do not need to adjust the DB_NAME and DB_USER in wp-config.php

❌ If these do not match, this is the cause of the error on your website
Then copy the name and user of the database that belongs to your website in your hosting interface into your Word document.

3. check the database password

Next, we check the password and the host of the database. To do this, we navigate to the user administration in the "Databases" menu item.

Database hosting

There you will find an overview of all database users available in your hosting plan. Find the appropriate user again based on the information in wp-config.php. Click on the user.

Check database data

The database server is specified in wp-config.php under 'DB_HOST'.

Copy the password from the copied data and enter it here in the database user mask. Confirm with the "Ok" button.

✅ If the DB_HOST in wp-config.php matches, no changes need to be made.

❌ If the DB_HOST does not match, this is the cause of the error on your website
Copy the DB_HOST into your Word document so that you can adjust it immediately in wp-config.php.

4. adjust data in the wp-config.php

Note 💡: This step is only necessary if the data in the database user and the wp-config.php do not match.

Navigate back to your httpdocs folder via "Files". Open the wp-config here.

Now replace the incorrect data with the correct data from the database and confirm with "Save".

5. test website

After you have made the adjustment in wp-config.php, you should be able to access your website normally again. If the error persists, I recommend that you test the next recommended methods!

Deleted database

Have you just noticed during testing that your database no longer exists? Then this is one reason for the error when calling up your website.

Without the database, WordPress cannot access any data. And therefore cannot display any data.

To fix the error, I recommend that you restore a backup of your database. Have you not created your own backups? Then check whether your host regularly creates backups for you.

WPspace creates daily backups of your website, which are available to you for 14 days. Simply select the most recent backup and restore it. If there is still no database, select the next older backup from the overview and continue in this way.

Repairing a damaged database

It is very unlikely that your database or the table it contains is damaged. However, this can happen in rare exceptional cases.

A connection to your database is established when your website is called up. An error occurs when trying to read and export the tables/data from the database. As a result, your website cannot be displayed.

If you have experienced this rare case, you have the following three options for repairing your database.

Repair database via the hosting interface

As a WPspace customer, the "Check and repair database" function is available natively in the hosting interface. This tool saves you complicated detours and you can also check in the first step whether there is a damaged database at all. It's that easy to check and repair your database:

  • Log in to your Hosting interface at WPspace a
  • Navigate to the menu item "Databases"
  • In the overview, click on "Check and repair" in the submenu of the database that belongs to your website
Check and repair function

✅ Do you receive a Success message "Database has no errors" your database is not damaged. You therefore do not need to repair the database. A damaged database is not the cause of the error message "Error establishing a database connection".

❌ Do you receive No success messageyour database is damaged. The native function will then automatically repair your database for you.

Repair database via phpMyAdmin

If the "Repair database" function is not natively available in your hosting, you can also carry out the steps via phpMyAdmin. How to repair your website's database via phpMyAdmin:

Repair database phpMyAdmin
  • Access your database via phpMyAdmin
  • Check the "Select all" box to automatically select all tables
  • Open the "marked" drop-down menu
  • Select "Repair table"
  • The SQL command is now executed automatically.

Repair database via WordPress

Alternatively, you can also repair your database via WordPress. In my opinion, this is the most complex way. With the following instructions, you can also do this without any problems:

1. access your main directory:
Access the main directory of your WordPress website either directly via your hosting interface or via FTP access

2. open the wp-config.php
In the root directory / main directory of your websites you will find the file "wp-config.php". Open this file.

Note 💡: Depending on the host, it may be necessary to download wp-config.php and upload it again after customization. You can then save the TXT file locally on your PC using programs such as Notepad++ (recommendation) or Sublime Text edit.

3. add a line of code

Enter the following line of code in wp-config.php:

define('WP_ALLOW_REPAIR', true);

4. call up the WordPress database repair page
You can call up the repair page for the WordPress database with the following prefix: /wp-admin/maint/repair.php

In summary, the link looks something like this:

https://deinedomain.de/wp-admin/maint/repair.php

Important: Exchange deinedomain.de with your actual own domain.

5. repair database
After you have clicked on the link, the WordPress repair page will appear. Here you click on the "Repair database" button. WordPress will now automatically repair your database for you.

6. remove code line
Navigate back to your wp-config.php. Search here for your added line (the easiest way to do this is to use CTRL+F and insert "define('WP_ALLOW_REPAIR', true);"). Delete the line from the file and save the updated wp-config.php.

7. check website
Call up your website in the Incognito tab and check whether the error "Error establishing a database connection" has been fixed.

Check the accessibility of your database server

There are factors over which you generally have no direct influence. These include the accessibility of your host's database server.

If you are looking for a Reliable WordPress hosters like WPspace this usually happens very rarely or not at all. WPspace had an availability of 99.9 % in 2023! 🚀

However, if you suspect that your hosting's database server is the cause of the error message, you have various options for determining the accessibility of the database server.

Note 💡: The database server may be unavailable and the "main server" that runs your hosting is available.

  • Check the status page of the hoster
    A status page is used to keep customers up to date on current outages, restrictions and maintenance work. A professional hoster always keeps this overview up to date.
    You can find all information about the status at WPspace via this link: WPspace Status (statuspage.io)
    Tip: It is often possible to be automatically informed of updates by e-mail.
  • Contact the hoster's support
    If you do not know the link to the hoster's status page, you can also contact the hoster's support team directly by phone, email or via support chat.
  • Login to database
    Try to access the database directly via your hosting interface or alternatively via phpMyAdmin. Can't access the database? Then it may be that the database server is not accessible.
  • Call up alternative page
    Do you have a second page on the same server? Then try calling it up. If the website also shows you the error "Error establishing a database connection", this confirms the assumption that the database server is not accessible.

Import current backup

I recommend that you only import a backup if the cause is still unknown to you and you have no other solution. The following tips and information on importing the backup:

  • Create a backup before you import your old backup:
    When making such major changes, you should always first carry out a complete Create WordPress backup.
  • Start with the latest backup:
    If you are unsure about how long the error has existed on your website, start with the most recent backup. If the error persists after that, import the next older backup.
  • Data loss:
    When you import the backup, you overwrite all the changes that have been made since then. This can be a big problem, especially for stores.
  • Import database backup:
    Depending on the backup, you may only have the option of importing a backup of your database. Start with this, as this may already solve your problem.

Conclusion on the error "Error establishing a database connection"

The error "Error establishing a database connection" occurs rarely. There are various reasons why the error can occur:

  • Incorrect database information in wp-config.php
  • Deleted database
  • Damaged database
  • Database server not accessible

The most common cause is incorrect database information in wp-config.php. I therefore recommend that you check this information first. If that doesn't help, you can try to repair the database or ask your host for help.

Pro tip for WPspace customers💙: Is your website displaying the error? Then contact our support chat so that we can help you fix the error! Our technical team will be happy to check possible causes for you and provide you with tips.

If you are at a loss, you can import a backup of your website. Please note that this may result in data loss! So create another backup beforehand.

Picture of Isabell Bergmann
Isabell Bergmann
As an online marketing manager at WPspace, I love to share my knowledge around online marketing, web design and hosting.

Links marked with an * are affiliate links. If you buy a product via this link, WPspace receives a small commission. There are no additional costs or disadvantages for you! This has no influence on our opinion of individual products and services - we only recommend what we love ourselves.

Leave a Reply

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