Fixing "Error establishing a database connection" in WordPress
In short
The "Error establishing a database connection" message means WordPress cannot reach its database, most often due to wrong credentials in wp-config.php after a migration, an overloaded or unreachable database server, or corrupted tables. Work through it in order: verify DB_NAME, DB_USER, DB_PASSWORD and DB_HOST, test login via phpMyAdmin, check whether other sites on the server are affected, and use WP_ALLOW_REPAIR for corrupted tables.
Instead of your site, you get one stark sentence: “Error establishing a database connection”. WordPress can’t reach its database – and without the database it has nothing to show. The causes are limited; we’ll work through them in order.
1. Are the Credentials in wp-config.php Correct?
The most common trigger, especially after a server or hosting migration. Open wp-config.php in the root directory and check these four values:
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'user' );
define( 'DB_PASSWORD', 'password' );
define( 'DB_HOST', 'localhost' );
DB_HOST is often localhost, but with some hosts it’s a dedicated server name or an IP with a port (e.g. db1234.host.com:3306). You’ll find the right value in your host’s database overview. A common stumbling block: special characters in the password that got altered when copied.
A temporary test script tells you in a minute whether the credentials are right – including a plain-language explanation of the MySQL error:
Script Generator
Test the database connection
Enter the credentials from your wp-config.php → download the script → upload via FTP to the WordPress root → open in browser. Deletes itself afterwards.
Stays in your browser.
How to use:
- Download the script (button above)
- Upload via FTP/SFTP to your WordPress root – same level as
wp-config.php - Open in browser:
https://your-domain.tld/db-test.php - Read the result – it explains the error code and cause in plain language
- The file deletes itself automatically. If not: delete it manually via FTP
Never leave this file on the server – your database password is stored in it in plain text. Delete it immediately after testing.
Your input never leaves the browser. The script is generated locally via JavaScript.
Also available as a standalone page with all guides: connection test.
2. Is the Database Server Even Running?
Under heavy traffic or with tight resources, the database server can be briefly unreachable. Check whether other sites on the same hosting are affected, and look into phpMyAdmin – if you can log in there with the same credentials, you’ll know whether the problem is the credentials or the server. An exceeded storage or connection limit on your plan can also cut the connection; that usually shows in the hosting panel.
3. Is the Database Corrupted?
If the front end won’t load but the admin shows “One or more database tables are unavailable”, WordPress’s built-in repair often helps. Add to wp-config.php:
define( 'WP_ALLOW_REPAIR', true );
Then visit https://www.domain.tld/wp-admin/maint/repair.php, run the repair – and remove the line again afterwards, otherwise the page is reachable without a login.
4. Reset Credentials with Your Host
If the database password has changed or the user is locked, reset both in the hosting panel and enter them in wp-config.php. If you’re in the database anyway and still have login issues, Regain access to your WordPress admin takes it from there.
Quick Checklist
- Check the credentials in
wp-config.php(name, user, password, host). - Log into phpMyAdmin with the same details.
- Check other sites on the server → contact your host.
- Repair via
WP_ALLOW_REPAIR(remove the line afterwards).
An overview of other error patterns is in the overview of WordPress errors.
If the connection stays down, or you’d rather not touch credentials and the database yourself: drop us a line – we’ll restore the connection. With an ongoing maintenance plan, an outage like this shows up in monitoring before your visitors notice it.
Frequently Asked Questions
What does 'Error establishing a database connection' mean?
This message appears when WordPress can't reach its database – and without the database it can't show any content. The most common causes are wrong credentials in wp-config.php (often after a migration), a temporarily overloaded or unreachable database server, or corrupted tables. Usually the cause can be narrowed down in a few steps.
How do I fix the database connection error in WordPress?
In order: first check the four credentials in wp-config.php (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST). Then test whether you can log into phpMyAdmin with the same details – if you can, it's not the credentials. Check whether other sites on the server are affected too (then it's the database server), and if in doubt repair corrupted tables via WP_ALLOW_REPAIR.
Where do I find the database credentials?
In the wp-config.php file in the root directory of your WordPress install. It contains DB_NAME, DB_USER, DB_PASSWORD and DB_HOST. The correct values – above all the right DB_HOST, which isn't always localhost – are in the database overview of your hosting panel. With some hosts it's a dedicated server name or an IP with a port.
What do I do if the WordPress database is corrupted?
If the admin shows a note that one or more tables are unavailable, the built-in repair helps. Add the line WP_ALLOW_REPAIR with the value true to wp-config.php, visit the repair page at wp-admin/maint/repair.php and run the check. Important: remove the line again afterwards, otherwise the repair page is publicly reachable without a login.
Related Services
Founder & Full-Stack Developer
20+ years of web development experience. Specialised in Laravel, WordPress and custom software for mid-sized businesses.