Website Help
Stats and Errors
How to Fix “Error Establishing a Database Connection” in WordPress

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

“Error Establishing a Database Connection” is an error telling you that your WordPress website can’t connect to its database. It may seem hard to fix, but fortunately, there are only a handful of issues causing it, and with a bit of investigation and troubleshooting, you can easily resolve the problem.

This guide will explain what “Error Establishing a Database Connection” means, and outline the most common causes and how to fix them.

The error “Error Establishing a Database Connection” in WordPress indicates that the connection between WordPress and its database has failed due to various potential reasons.

Before looking into the error, we need to understand how WordPress works. Like most CMS (Content Management System) applications, it consists of two integral parts – files and a database.

The files are the website’s frame and contain the core functionalities of WordPress, the media, themes and plugins.

On the other hand, the database contains the website’s custom settings and unique content. All the pages, posts, text and design are recorded as entries in the tables.

The files communicate with the database through various PHP and MySQL functions and extract data visualized on the web pages.

Therefore, when your website fails to connect to the database, it can’t retrieve the data required to display the pages. WordPress warns you of the problem by displaying the message “Error Establishing a Database Connection”, as seen in the image below.

What does WordPress’ “Error Establishing a Database Connection” mean

What are the most common causes of “Error Establishing a Database Connection”?

There are a few different scenarios in which the error appears. The most common ones include:

  • Wrong database settings in the configuration file – Incorrect database credentials are arguably the most common reason for a disrupted database connection. If the database name, username, password or hostname are wrong, the website won’t connect to the database and will return “Error establishing database connection”.
  • Insufficient database user privileges – Another reason why the error occurs is if the database user doesn’t have sufficient privileges, even if the credentials are correct. The user must have permission to execute all types of MySQL operations onto the database.
  • Corrupted database tables – WordPress is a highly-dynamic system, and each time you make changes to your website, the database’s tables are modified. Adding or deleting plugins and themes creates new tables or deletes existing ones, new pages add new entries, etc.
    With so many MySQL operations being conducted, occasionally, the tables may become corrupted, which could lead to another variation of the “Error establishing database connection” – “One or more database tables are unavailable”.
  • Incorrect website URLs – On rare occasions, the “Error establishing a database connection” could be caused by incorrect website URLs.
  • Corrupted WordPress files – Another reason why you see the “Error establishing a Database Connection” message is that your website’s WordPress core files are corrupted. This could stem from a malware infection, failed update, or incomplete file transfer while moving your site manually.
  • The database server is down or overloaded.

How to Fix “Error Establishing a Database Connection”

At first glance, the error sounds quite intimidating and hard to resolve. However, it usually boils down to a few key settings, which can be easily checked and fixed.

How to Fix “Error Establishing a Database Connection” Video Tutorial

Inspect the wp-config.php File of your WordPress website

The settings for the database are stored in the configuration file of every WordPress installation. It is called wp-config.php and is located in the root directory of your website.

To check the file, log into the website folders via FTP or your hosting’s panel. If you are a SiteGround user, navigate to Site Tools > Site > File Manager.

The root folder of every website is the directory yourdomain.com/public_html. In our example, the website name is sg-testing.com, so the folder is sg-testing.com/public_html.

Before making any changes, it’s best to keep a backup of the file just in case. To do that, right-click on the file, select Copy, and rename the copied version to wp-config-backup.php.

How to copy wp-config.php

After creating the backup copy, select the file wp-config.php, and choose Edit.

How to edit the wp-config.php file

The database name, user, password and host are defined in the following lines of code:

// ** Database settings - You can get this info from your web host ** //

/** The name of the database for WordPress */

define( 'DB_NAME', 'database_name_here' );

/** Database username */

define( 'DB_USER', 'username_here' );

/** Database password */

define( 'DB_PASSWORD', 'password_here' );

/** Database hostname */

define( 'DB_HOST', 'localhost' );
Check the database credentials

The values of each setting must match the existing database, user and password in the MySQL server of the website. After applying the changes, press the Save (floppy disk) icon.

Set the correct database hostname

The MySQL server is defined by the line of code:

define( 'DB_HOST', 'localhost' );

Typically, the database host is on the same server as the website’s files. Therefore, the value is “localhost” or “” and the entire line will read:

define( 'DB_HOST', 'localhost' );


define( 'DB_HOST', ‘’ );
How to see the database host

If you’ve set a remote database host, you must replace these values with the correct hostname of the remote server.

Set the correct database name

After the host has been established, check the database details in the MySQL server. SiteGround users can easily access the MySQL database settings from Site Tools > Site > MySQL.

To compare the settings from the wp-config.php file with the MySQL database details more easily, open the MySQL section in a new browser tab by right-clicking on it and selecting Open in a New Tab.

Open the MySQL section in a new tab

The existing databases on the MySQL server are listed in the Databases tab under Manage Databases.

Databases in the MySQL section

The database listed there must match the one defined in the wp-config.php file.

Copy the database name

Copy the database’s name from MySQL > Databases and paste it in the line of code in wp-config.php:

define( 'DB_NAME', 'database_name_here' );

Set the correct database name

Set the correct database username

To see the user for the database, click on the link under the Users column of the database.

How to see the database users

A popup window will appear listing all users with privileges to modify the website’s database. Highlight the username under the User column and copy it.

Copy the database username

Go back to editing the wp-config.php file and paste the name in the line:

define( 'DB_USER', 'username_here' );
Set the correct database username

If there is no user attached to the database, there will be no link under the Users column. Click on the Add New User icon under the Actions column to add an existing user to the website’s database.

Add user to a database

In the popup window, select one of the names from the Users drop-down menu, check the radio button All Privileges, and press Confirm.

Select user and confirm adding it to a database

After the user is attached to the database, you can copy their name and paste it in the wp-config.php file.

If there are no existing users, open the section Site Tools > Site > MySQL > Users. You can create a new user by pressing the button Create User.

Create database user

IMPORTANT! Keep in mind that you can’t choose the database’s username. It will be automatically generated by the system.

The new username will appear under the Manage Users section in the Users tab. Click on the icon Add new database to attach the user to the website’s database.

Attach user to a database

In the pop-up window, select the website’s database, check the radio button All privileges, and hit Confirm.

Confirm adding user to a database

You can then copy the new username and paste it in the respective line in wp-config.php.

Set the correct database user’s password

After adding the user in wp-config.php, you must ensure that the user’s password in the file matches its counterpart in MySQL. Copy the password string from the line in wp-config.php:

define( 'DB_PASSWORD', 'password_here' );
Set the correct database user’s password

After the password is copied, go back to Site > MySQL > Users, press the kebab menu, and select Change Password.

Change database user password

Paste the password in the New password field and hit Confirm.

Paste the user password

The error should disappear when reloading the website if all credentials are set correctly.

Check if the Database User has the Correct Privileges

To check the user’s privileges, go to Site Tools > Site > MySQL > Users, and find the user in the section Manage Users. Click on the link under the Database Access column, which opens a new pop-up window.

Set the correct database user privileges

Press the Manage Access icon to open a new window where the access options are managed.

Manage the access for a database user

To set all necessary permissions, select the radio button All Privileges and click on the button Confirm.

Set all privileges for a user

Repair the Database

If your database is damaged, you need to repair it, and thankfully, WordPress has a built-in repair mode.

To activate it, you need to edit your website’s wp-config.php file via FTP or your hosting control panel.

SiteGround users can edit the file by navigating to Site Tools > Site > File Manager. The file is located in the website’s root directory, which is yourdomain.com/public_html (where you need to replace yourdomain.com with your own domain).

Select wp-config.php and press Edit.

Repair the database. Edit wp-config.php

Just above the line “/* That’s all, stop editing! Happy publishing. */”, add the following code:

define('WP_ALLOW_REPAIR', true);

Confirm the changes by pressing the Save icon.

Add the repair mode code in wp-config.php

To load repair mode, open the page https://yourdomain.com/wp-admin/maint/repair.php in your browser, where yourdomain.com is replaced with your website’s name. For example, our name is sg-testing.com, so the address would be https://sg-testing.com/wp-admin/maint/repair.php.

Repair the database from the WordPress Repair Mode

Choose between the two options Repair Database and Repair and Optimize Database, and once the maintenance is over, reload your website’s homepage. Once you confirm that the error is fixed, remove the code you added in wp-config.php so no one else can run repair mode.

Inspect the WordPress Site URL in the Database

The error will prevent you from logging into the dashboard where the website’s URL is set. However, you can use phpMyAdmin to edit the tables where these settings are defined.

SiteGround users can reach the tool by navigating to Site Tools > Site > MySQL > PHPMYADMIN > ACCESS PHPMYADMIN.

How to access phpMyAdmin

Click on the website’s database in the column on the left and open the wp_options table.

Note that wp_ is the default WordPress table prefix, but in most cases, the prefix for your site will be custom.

Open the table wp_options

The website URLs are defined in the options siteurl and home. Press Edit on each, set the correct website URL in the option_value field, and confirm the change with the button Go.

Fix the website URLs in wp_options

Restore a Backup of the Site

If you encountered the error after a manual transfer, make sure to export a new full website backup from the previous hosting and import it into the new hosting again.

If the website has not been transferred and the error suddenly appears, your best choice is to restore the website from a recent backup. See this tutorial for SiteGround’s Backup tool.

Contact the Web Hosting Provider

The solutions described above work if the issue stems from the website’s configuration. However, WordPress may fail to connect to the database if the MySQL server is overloaded or under maintenance.

If you manage your own server, consider restarting the MySQL service to clear any stuck database queries overloading it.

Users with managed hosting don’t have this type of access. Contact your provider’s support team so they can check the status of the server and resolve the problems from their side.


“Error establishing a database connection” is a widespread WordPress error that you are likely to encounter every once in a while. In a nutshell, it informs you that your website can’t connect with its database.

During this period, your website is not accessible, and you need to repair it before you lose substantial traffic. But don’t worry, there are only a small number of causes, and with a bit of troubleshooting, they can be easily ironed out.

This guide has outlined the most common causes of the error. By following the instructions carefully, you’re sure to get your website back online in no time.

Share This Article