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

Do you like to watch more videos like this?
Check out our YouTube Channel.

"Error establishing a database connection" is a fatal WordPress error: your site cannot reach its database, so visitors see a blank page with that single line instead of your content. The most common cause is wrong database credentials in your wp-config.php file, usually after a site move, so that is the first thing to check.

Quick fix: open wp-config.php and confirm that DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST match the values in your hosting account’s MySQL settings. If they match and the error stays, the database tables are likely corrupt or the MySQL server is down. The steps below work through each cause in order of likelihood.

What Causes the Error Message "Error Establishing a Database Connection"?

The error message "Error Establishing a Database Connection" can be caused by either incorrect database credentials in your WordPress settings, a corrupt database, or an irresponsive database server.

Picture: Error message error establishing a database connection
This is what the error message “Error Establishing a Database Connection” looks like

Use this table to match what you see to the most likely cause and jump straight to the fix:

What you see Most likely cause Where to fix it
The same error on the front end and in /wp-admin Wrong database credentials in wp-config.php Check Your WordPress Database Credentials
The error started right after moving to a new host Wrong database host, or new-server credentials Check Your Database Hostname Information
/wp-admin shows "One or more database tables are unavailable" Corrupt database tables Repair the WordPress Database Tables
The site slows down under traffic, then errors MySQL server overloaded or offline Check if the Database Server is Offline
The error appeared after changing the site address Stale siteurl value in the database Update the WordPress Site URL

What is a Database?

To understand the problem, we briefly explain what we are dealing with:

A database is a software that makes it easier to store, organize, and retrieve data from one software to another.

WordPress acts as a content management system. It uses a database to store all of your content and other website data. A connection to the database is established each time the website is visited.

What Data does WordPress need to Establish a Connection to the Database?

WordPress needs the following information:

  • Name of the database
  • Database username
  • Database password
  • Database server

This information is saved in your WordPress configuration file with the name "wp-config.php."

Once any of these items are incorrect, WordPress can no longer connect to your database server. Then you get the error message "Error Establishing a Database Connection."

It is one of the most common WordPress errors. Aside from incorrect credentials, this error can also occur if the database server has failed or the database files are damaged.

Various reasons can cause this error; we have listed the different scenarios one by one. If you have already tried one of these approaches yourself, select the next steps from the table of contents above.

We’ll start with the most likely reason and then go into less common reasons.

Check Your WordPress Database Credentials

The most common cause of database connection failure in WordPress is incorrect database credentials.

One of the reasons for incorrect WordPress database connection can be a recent move of your WordPress website to a new host or server.

The login information for the WordPress database is saved in the wp-config.php file. This WordPress configuration file contains all the basic WordPress settings, including the database information.

Login to your website by using an FTP client like FileZilla and look for the lines below in the file wp-config.php:

Picture: Database credentials

You now need to ensure that the database name, username, and password are correct.

You can check this information in your WordPress hosting account dashboard. Log into your hosting account and click on "MySQL Databases" in the Database section.

Picture: Database hosting account mysql

You will now be taken to the database administration page in your hosting dashboard. Here you can see your database name and user name.

Picture: Database info

Below are the database users and links to change the user password.

Picture: Database user

After you have matched your database name, username, and password, you can change this information in your wp-config.php file if necessary.

Try revisiting your website to see if the database connection error is resolved.

If you can still see the error, it means that something else is causing the error. Keep reading the article for more troubleshooting steps.

Check Your Database Hostname Information

If you followed method one and verified that you are using the correct WordPress database login information, you should ensure that you are using the appropriate database host information.

The majority of WordPress hosting companies use "localhost" as their database host. However, some WordPress hosting companies use separate servers to host the databases. In this case, your database host information is not localhost.

In this case, you will need to contact your WordPress hosting company to obtain your database host information.

Repair the WordPress Database Tables

There is a quick way to tell corrupt tables apart from bad credentials: if your public site shows the generic error but /wp-admin shows a different message, "One or more database tables are unavailable. The database may need to be repaired," the tables themselves are damaged and this is the section you need.

To repair the database, add the following line to the "wp-config.php" file:

define('WP_ALLOW_REPAIR', true);

Add this text directly before the line "That’s all, stop editing! Happy blogging".

Add the Database Repairing Code WP_ALLOW_REPAIR constant.

Visit http://www.example.com/wp-admin/maint/repair.php to view the settings on this page:

Repair and Optimize Database Option

Click on the "repair database" button and wait for the process to finish.

Any visitor can access this database repair page. When you have finished repairing and optimizing your database, you will need to remove this code from your “wp-config.php” file.

Check if The Database Server is Offline

If you checked all items and WordPress still cannot connect to the database, your database server (MySQL server) may be down.

This happens when there is heavy traffic on a server. The hosting server cannot handle this heavy load. The problem occurs, especially when you are using shared hosting.
As a result, your site will become very slow, and some users may even get the error.

Get in touch with your hosting provider and see if your MySQL server is responding.

Alternatively, if you have other websites running on the same server, you can check those websites to make sure your SQL Server is down.

To check if your database access credentials are correct and your database server is not offline, create a new empty text file with a text editor, name it tod testconnection.php and copy and paste the code below to that empty text file:

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

Picture: Verify the permission of the database user
Verify if your database user has sufficient permission

Adjust the values ​​"username" and "password." to your settings
Now upload this file with FTP or a file manager plugin to your website and access it through a web browser.

If the script can connect successfully, it means your user has sufficient permissions, and your database server is online. Now use the same credentials in the wp-config.php, and WordPress can connect to the database.

In the next step, we will list other solutions that also work but occur less often than the previous ones.

Update the WordPress Site URL

Access phpMyAdmin from your hosting account dashboard and select your WordPress database.

Then, click on the SQL menu at the top and enter the MySQL query below:

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Note: Be sure to enter your own "Site URL" and change "wp_options" to your site’s table name if your site uses a different WordPress table prefix.

Reboot the Web Server

If you are using a dedicated server, a local server, or a virtual private server (VPS), you can try restarting your servers.

Restarting the web and database servers can resolve temporary glitches that may be causing the error.

If the Error Still Persists

If none of the steps above bring your site back, gather more detail before contacting support:

  • Enable the debug log by adding define('WP_DEBUG', true); and define('WP_DEBUG_LOG', true); to wp-config.php, reload the site, then read wp-content/debug.log for the exact database error. Remove both lines once you are done.
  • Ask your hosting provider to confirm that the MySQL service is running and that your account has not hit a connection or storage limit.
  • If the error appeared right after an update, a migration, or a restore, roll back to a recent backup of your site and database. That reverts whatever change broke the connection and gives you a working starting point to try again.

Conclusion

The "Error Establishing a Database Connection" can be daunting, but with the right steps, it’s often a straightforward fix. By understanding the potential causes and applying the solutions outlined above, you can restore your WordPress site quickly. Regular maintenance and proactive measures will help prevent this error in the future.

Related Articles

Rene Hermenau

Author: Rene Hermenau

About the author: René Hermenau is the founder of WP STAGING. He works on WordPress backups, staging, migrations, database handling, and safe deployment workflows.