“Error establishing a database connection” means WordPress can’t reach its MySQL or MariaDB database. Your posts, pages and settings are still in the database. WordPress just can’t log in to read them. In most cases the cause is one of four things: wrong database details in wp-config.php, a database server that is down or overloaded, damaged database tables, or settings left over after a move to a new host.
Work through the fixes below in order. Before you change anything, download a copy of your wp-config.php file so you can undo a mistake.
Which version of the error do you see?
The exact message tells you where to start:
| What you see | Where | Start with |
|---|---|---|
| Error establishing a database connection | Whole site and wp-admin | Fix 1 |
| A longer message that mentions “the username and password information in your wp-config.php file” and the database server | Whole site (shown when debugging is on) | Fix 1 and Fix 2 |
| One or more database tables are unavailable. The database may need to be repaired. | wp-admin | Fix 4 |
| The error comes and goes, often during busy hours | Whole site | Fix 5 |
You need your hosting control panel (cPanel, Plesk, hPanel or similar) with File Manager and phpMyAdmin, or FTP/SFTP access.
Fix 1: Check the database details in wp-config.php
WordPress reads four values from wp-config.php to connect to the database. If any one is wrong, you get this error.
- Log in to your hosting control panel and open File Manager.
- Open your site’s folder. On most hosts this is public_html.
- Find wp-config.php, right-click it and choose Download to keep a backup.
- Right-click it again and choose Edit. You should see lines like these near the top:
define( 'DB_NAME', 'acct_wp123' );
define( 'DB_USER', 'acct_wpuser' );
define( 'DB_PASSWORD', 'your-password' );
define( 'DB_HOST', 'localhost' );- In another tab, open MySQL Databases in cPanel (other panels call it Databases or MySQL Management).
- Under Current Databases, check that the database name matches DB_NAME exactly. cPanel adds your account name as a prefix, such as acct_, and the prefix must be included.
- Under Current Users, check that the user matches DB_USER.
- You can’t see the existing password, so set a new one. Next to the user, choose Change Password, create a password and copy it.
- Paste the new password into DB_PASSWORD in wp-config.php, keeping the single quotes around it.
- Back in MySQL Databases, find Add User To Database. Choose your user and database, select Add, tick ALL PRIVILEGES and select Make Changes. If the user was already added, this does no harm.
- Save wp-config.php and reload your site in a private browser window.
If the site loads, you are done. If not, keep the new password (it is now correct) and move to Fix 2.
[OPTIONAL SCREENSHOT: cPanel MySQL Databases page showing Current Databases, Current Users and the Add User To Database section, alt text: error establishing a database connection fix in cPanel MySQL Databases]
Fix 2: Check DB_HOST
DB_HOST tells WordPress where the database server is. Common correct values:
- localhost: most shared hosting with cPanel.
- A separate hostname: some hosts run the database on another server. Your panel shows it on the database page, often labelled MySQL hostname or Database host.
- localhost:3307 or another port: only if your host says the database uses a non-standard port.
- 127.0.0.1: on some servers “localhost” uses a socket file that PHP can’t find. Changing DB_HOST to 127.0.0.1 makes PHP connect over the network instead.
- Docker: use the database service name from your docker-compose file, for example db, not localhost.
On a local site (XAMPP)
- Open the XAMPP Control Panel and check that MySQL shows as running. If not, select Start.
- A fresh XAMPP install uses the user root with an empty password, so DB_PASSWORD should be ” unless you set one.
- Open http://localhost/phpmyadmin and check that the database named in DB_NAME exists.
If MySQL won’t start in XAMPP, another program may be using port 3306, or your drive may be almost full. Our guide to free up C drive space in Windows 11 helps with the second case.
Fix 3: Test the connection with a small PHP file
This test tells you exactly why the connection fails, instead of guessing.
- In File Manager, inside your site folder, create a new file called dbtest.php.
- Paste this code and replace the four values with the ones from wp-config.php:
<?php
mysqli_report(MYSQLI_REPORT_OFF);
$link = mysqli_connect('localhost', 'acct_wpuser', 'your-password', 'acct_wp123');
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
echo 'Connected successfully';- Save the file and open https://yourdomain.com/dbtest.php in your browser.
- Read the message and match it in the table below.
- Delete dbtest.php as soon as you are done. It contains your database password.
| Message | What it means | What to do |
|---|---|---|
| Access denied for user ‘…’@’localhost’ (using password: YES) | Wrong user or password, or the user isn’t added to the database | Repeat Fix 1, steps 8 to 10 |
| Unknown database ‘…’ | DB_NAME is wrong or the database was deleted | Check the name and prefix, or restore the database from a backup |
| Connection refused, or No such file or directory | Database server is stopped, or DB_HOST is wrong | Try 127.0.0.1 (Fix 2), then contact your host |
| Too many connections | Server is overloaded | Go to Fix 5 |
| Connected successfully | Details are correct | Check wp-config.php for typos, then go to Fix 4 |
Fix 4: Repair the WordPress database
Tables can get damaged after a server crash or a full disk. WordPress has a built-in repair tool.
- Open wp-config.php and add this line just above the line that says “That’s all, stop editing”:
define( 'WP_ALLOW_REPAIR', true );- Save the file and visit https://yourdomain.com/wp-admin/maint/repair.php.
- Select Repair Database. (Repair and Optimize Database also works but takes longer on big sites.)
- Wait for the list of tables with “OK” or “repaired” next to each one.
- Remove the WP_ALLOW_REPAIR line from wp-config.php. While it is there, anyone can open the repair page without logging in.
You can also repair from phpMyAdmin: open your database, select Check all below the table list, then choose Repair table from the With selected menu.
[OPTIONAL SCREENSHOT: WordPress database repair page at wp-admin/maint/repair.php with the Repair Database button, alt text: WordPress repair database page to fix database connection error]
Fix 5: Check if the database server is down or overloaded
Signs that the problem is on the server side:
- phpMyAdmin won’t open or shows its own connection error.
- Other websites on the same hosting account show the same error.
- The error appears during traffic spikes and disappears later.
What to do:
- Check your host’s status page or support announcements for an outage.
- On a VPS, check free disk space with df -h. MySQL stops working when the disk is full.
- On a VPS, restart the database service with sudo systemctl restart mysql (use mariadb instead of mysql if your server runs MariaDB).
- On shared hosting, contact support. You can’t restart MySQL yourself there.
A message like this gets a faster answer from support:
“My WordPress site example.com shows ‘Error establishing a database connection’. I checked the database name, user and password in wp-config.php against cPanel, and a PHP test returns: [paste the exact message]. Can you check whether the MySQL server for my account is running and not hitting a connection limit?”
Fix 6: After moving to a new host
If the error started right after a migration:
- wp-config.php may still have the old host’s database name, user and password. New hosts usually use a different account prefix.
- The database may not have been imported. Open phpMyAdmin and check that the tables (wp_posts, wp_options and others) are there.
- Your domain may still point to the old server, where the database was removed. Check your DNS records with your domain registrar.
A real-world example
A site moves to a new host. The database is imported as newacct_wp and the user is newacct_admin, but wp-config.php still says oldacct_wp and oldacct_admin. The dbtest.php file returns “Access denied for user ‘oldacct_admin’@’localhost'”. Updating DB_NAME, DB_USER and DB_PASSWORD to the new values brings the site back in two minutes.
Troubleshooting when nothing works
- You edited the wrong file. Some sites have WordPress in a subfolder, and wp-config.php can also sit one folder above public_html. Check where the file you edited actually lives.
- Curly quotes. Copying values from a word processor can turn ‘ into a curly quote, which breaks PHP. Type the quotes again in File Manager’s editor.
- A single quote in the password. A ‘ inside the password ends the value early. Choose a new password without single quotes.
- Need more detail. Add define( ‘WP_DEBUG’, true ); to wp-config.php to see the longer error message, then set it back to false when you are done, so visitors don’t see error details.
Common mistakes
- Reinstalling WordPress. The problem is the database connection, not the WordPress files.
- Changing the password in cPanel and forgetting to update wp-config.php.
- Leaving dbtest.php or WP_ALLOW_REPAIR on the server after fixing the site.
- Leaving out the cPanel prefix from the database name or user.
- Restoring an old backup before finding the cause, which can lose recent posts and orders.
How to stop it happening again
- Keep daily backups that include the database, not only files.
- Use a caching plugin so fewer page views hit the database.
- Set up a free uptime monitor so you get an email the moment the site goes down.
For the official reference on every wp-config.php setting, see the WordPress wp-config.php documentation. If you test sites locally on a Windows laptop and XAMPP feels sluggish, our guide on how to fix a slow laptop on Windows 11 can help.
Database connection error FAQ
What causes “Error establishing a database connection” in WordPress?
The most common causes are a wrong database name, user, password or host in wp-config.php, a database server that is down or overloaded, damaged database tables, and leftover settings after moving to a new host.
Will I lose my posts because of this error?
Usually not. The error means WordPress can’t connect to the database. Your content is still stored there unless the database itself was deleted.
How do I find my WordPress database name?
Open wp-config.php and look at the DB_NAME line. You can also see your databases in cPanel under MySQL Databases or in the left sidebar of phpMyAdmin.
Can a plugin cause this error?
A plugin rarely causes it directly. A heavy plugin can overload a small server and lead to “Too many connections”, which shows the same error.
Why does the error come and go?
An error that appears and disappears usually means the database server is overloaded or restarting. Check with your host and look for traffic spikes around those times.
Once your site is back, delete any test files, remove the repair line from wp-config.php and take a fresh backup.


Pingback: SSL Certificate Lifetime Drops to 64 Days (Let's Encrypt 2027) - Searching New
Pingback: WordPress 7.1 Update Guide: What’s New and How to Update Safely