Guides

How To Fix "Error Establishing A Database Connection" In WordPress

How to fix Error establishing a database connection in WordPress, in five steps

Every WordPress page is built from content stored in a database. When WordPress can't reach that database, it can't show anything at all, and your visitors see a single line: "Error establishing a database connection." If your site, or a fresh install, is stuck on this message, this article will help you.

We are here to help you find out exactly why the connection fails and fix it, step by step. It is one of the most common problems new site owners face. When we checked the WordPress.org Installing WordPress support forum on November 30, 2026, five of its 92 most recent threads were about a database connection failure.

Related Articles

What "Error Establishing A Database Connection" Means

WordPress keeps your posts, pages, users and settings in a MySQL or MariaDB database. On every visit, it connects using four details from your wp-config.php file: the database name, username, password and host. If any of them is wrong, or the database server isn't answering, WordPress stops and shows this error.

The WordPress documentation lists three main causes: wrong details in wp-config.php, a problem with your web host's database server, and a hacked site.

What We Saw When We Reproduced It

We ran a copy of WordPress 7.1.4 on our computer with PHP 8.4. Then we pointed its wp-config.php at a database server that wasn't running. Visitors saw only one line, with no clue about the cause.

WordPress page showing only the heading Error establishing a database connection
What a visitor saw on our test copy of WordPress 7.1.4 when the database server wasn't running.

Next, we turned on WP_DEBUG. Now the same page showed the real reason, "Connection refused", plus a short checklist from WordPress itself.

WordPress database error page with debug mode on, showing Connection refused and a checklist asking about the username, password, hostname and whether the database server is running
The same error with debug mode on. WordPress showed "Connection refused" and the database host it tried to reach.

Where I Have Seen This Error

I have run many WordPress sites on different hosts, and this error has come up for different reasons. On GoDaddy Managed WordPress hosting, we saw it regularly, and their support team fixed it for us. On SiteGround, I saw it when my plan ran short of resources: the server stopped responding whenever I published a new article, and their support suggested upgrading to a plan with more resources and RAM. On a KnowHost VPS 1 plan, adding more RAM fixed it.

So don't assume the problem is in your own files. Sometimes the database server is simply overloaded, and only your host can see that.

Quick Checklist

  1. Compare the four database details in wp-config.php with your hosting panel.
  2. Test the connection with a small file to see the exact reason.
  3. Ask your host whether the database server is down or overloaded.
  4. Repair the database if WordPress says tables are unavailable.
  5. Check that nobody changed wp-config.php without you.

Step 1: Check The Database Details In wp-config.php

First, open wp-config.php in your host's File Manager. Find these four lines:

define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );

Next, open the databases page in your hosting panel and compare every value letter by letter. The host is often localhost, but some hosts use a separate database server, so copy the exact host name from your panel. If you recently changed the database password in your panel, update it here too.

Step 2: Test The Connection With A Small File

To see the exact reason, create a file called db-test.php in the same folder as wp-config.php, with this code:

<?php
$link = @mysqli_connect( 'localhost', 'your_database_user', 'your_database_password', 'your_database_name' );
echo $link ? 'Connected to the database.' : 'Could not connect: ' . mysqli_connect_error();

Now open yoursite.com/db-test.php in your browser. On our test copy, it said "Could not connect: Connection refused". Here is what the common messages mean, based on the MySQL error reference:

  • Connection refused, or Can't connect to MySQL server: the database server is down, or the host name or port is wrong.
  • Access denied for user: the username or password is wrong, or the user hasn't been given access to the database.
  • Unknown database: the database name is wrong, or the database doesn't exist yet.

Delete db-test.php as soon as you have your answer, because it contains your database password.

Step 3: Check If The Database Server Is Down Or Overloaded

If the details are correct but you still get "Connection refused", the problem is on the server. Check your host's status page, then contact their support and ask whether the database server is running. If the error appears only at busy times, your plan may be running out of memory, as it did on my SiteGround and KnowHost plans.

Step 4: Repair The Database

If your dashboard says "One or more database tables are unavailable", the database may be damaged. Take a backup first. Then add this line to wp-config.php, just above "That's all, stop editing!":

define( 'WP_ALLOW_REPAIR', true );

Now open yoursite.com/wp-admin/maint/repair.php and click Repair Database.

WordPress database repair page with Repair Database and Repair and Optimize Database buttons
The WordPress database repair page on our Rahul Vithala test site after we added WP_ALLOW_REPAIR.

Remove the line straight after. The wp-config documentation warns that anyone can open the repair page while it is switched on, without logging in.

Step 5: Check For Unexpected Changes

If the details in wp-config.php look different from what you set, and nobody on your team changed them, treat it as a possible hack. Compare the file with your latest backup, change your hosting and database passwords, and restore a clean copy of the site if anything else looks wrong.

Getting The Error While Installing WordPress

All five forum threads we counted were in the Installing WordPress forum, so many people hit this error while installing WordPress. In that case, create the database and a database user in your hosting panel first, give the user all privileges on that database, and then run the installer. Our guide to creating a database with phpMyAdmin shows how. On a local setup such as XAMPP, make sure the MySQL service is started, because a stopped server gives the same "Connection refused" we saw.

Common Mistakes To Avoid

  • Copying a password with a space at the end: the extra space makes the password wrong. Paste it carefully between the quotes.
  • Editing wp-config.php in a word processor: it can turn straight quotes into curly ones and break the file.
  • Forgetting the repair line: leaving WP_ALLOW_REPAIR on lets anyone run the repair page.
  • Leaving the test file online: always delete db-test.php when you are done.

How To Prevent It

  • Keep your database details in a password manager, so you never retype them from memory.
  • Update wp-config.php on the same day you change a database password in your hosting panel.
  • Set up an uptime monitor that emails you when your site goes down.
  • Choose a hosting plan with enough memory for your traffic, and upgrade before busy periods.

Error Establishing A Database Connection: FAQs

Have I lost my posts?

Almost certainly not. The error means WordPress can't reach the database, not that the data is gone. Once the connection works, everything returns.

Why does the error come and go?

An error that appears only at busy times usually means the database server is overloaded. Ask your host about your plan's limits.

Could this mean my site was hacked?

It can. If wp-config.php has changed and you didn't change it, restore a clean backup and read our guide on what to do when your WordPress site is hacked.

Final Words

An error establishing a database connection always comes down to the four details in wp-config.php or the database server behind them. Check the details, test the connection, and ask your host if the server is the problem. Let us know in the comments what caused the error on your site.

Sources

We checked these sources for this guide on November 30, 2026:

#Database#Error#Troubleshooting