Troubleshooting · Troubleshooting
How to Fix "Error Establishing a Database Connection" in WordPress
Short answer
Fix the WordPress database connection error by checking the four values in wp-config.php, DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST, against your hosting panel. If they match, check that the database user has all privileges, then repair damaged tables. If the error started with no change on your side, ask your host whether the database server is down or over quota.
Key takeaways
- The error means WordPress could not open its MySQL or MariaDB database, so it cannot build any page.
- Wrong values in wp-config.php are the usual cause, so compare DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST with your hosting panel.
- MySQL error 1045 means access was denied, and the words using password YES or NO show whether a password was sent.
- Add WP_ALLOW_REPAIR to wp-config.php to open the repair tool, then remove that line as soon as the repair finishes.
- WordPress.org lists two host-side causes: a database over quota and shut down, or a server that is down.
- WP_DEBUG_LOG writes errors to wp-content/debug.log, which shows the exact database error behind the one-line message.
What does “Error establishing a database connection” mean?
It means WordPress tried to open its MySQL or MariaDB database and failed, so it could not build any page.
WordPress stores posts, pages, settings, users, and plugin options in a database, not in files. On each request, WordPress reads four login details from wp-config.php and opens a connection before it builds anything.
If that connection fails, WordPress stops and shows this one line instead of your site. Your content is usually still in the database, but WordPress cannot reach it to display it.
WordPress.org states the message means there is a problem with the connection to your database, and it lists wrong details in wp-config.php as the usual cause (Common WordPress Errors).
Why does WordPress show this error?
The error has a short list of causes, and wrong login details in wp-config.php come first.
WordPress.org names four values to check in that file: database name, database username, database password, and database host. A wrong value in any one of them blocks the connection.
| Cause | Sign | First check |
|---|---|---|
Wrong credentials in wp-config.php | Error started after a move, a password change, or an edit | Compare the four values with your host panel |
| Database server down | Error started with no change on your side | Ask your host about server status |
| Database over its quota | Host panel shows a full or shut-down database | Ask your host about quota and disk space |
| Damaged tables | Admin shows tables are unavailable | Run the repair tool described below |
| Wrong user permissions | Login works but WordPress cannot read tables | Check the user has all privileges on the database |
A host-side cause is also possible. WordPress.org lists two: your database has met its quota and has been shut down, or the server is down.
If you checked wp-config.php and your host confirms no hosting issue, WordPress.org notes the site may have been hacked, so scan it and get help if that looks likely.
What should you check first before changing anything?
Open /wp-admin/ first, because it often shows a longer message than the front end.
The front end shows only the one-line error, while the admin area can name the exact problem, such as tables being unavailable. That longer message tells you whether to fix login details or repair tables.
Next, make a copy of wp-config.php before you edit it. Save the copy on your computer, not only on the server, so you can put the old values back if an edit makes things worse.
Also check whether the error affects every page or only some pages. An error on every page points to the connection itself, while an error on one page points to a smaller problem, such as a damaged table used by that page.
If you have a recent backup, confirm where it is before you start. A backup lets you restore the database if a repair step fails, and our guide to restoring a backup with UpdraftPlus walks through one restore route.
How do you check the database credentials in wp-config.php?
Open wp-config.php in the root folder of your site with SFTP or your host file manager, then find the four database lines.
The WordPress wp-config reference shows the four lines in this form, with your own values in place of the placeholders (wp-config.php):
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );
Compare each value with the database name, user, password, and host shown in your hosting panel. All four must match exactly, including any prefix your host adds to the database name or user name.
Check these details one by one:
DB_NAMEis the exact database name, including any account prefix.DB_USERis the database user, not your hosting login or WordPress admin user.DB_PASSWORDis that database user's password, with no stray space or extra character.DB_HOSTis the database server address, and it is not alwayslocalhost.
Some hosts use a separate hostname, a port, or a socket for the database. The wp-config reference gives 127.0.0.1:3307 as an example of a host with a non-default port, as of November 2026.
If a value is wrong, fix it in wp-config.php, save the file, and reload your site. If the site loads, the credentials were the problem.
How do you test the database connection yourself?
Use the same four values outside WordPress to see which part of the login fails.
If you have shell access, run the MySQL client with the values from wp-config.php, for example mysql -u DB_USER -p -h DB_HOST DB_NAME, then type the password when asked. A login here means the credentials work, so the fault lies elsewhere.
MySQL gives error 1045 when access is denied, with the code ERROR 1045 (28000) and the words “Access denied for user” (Troubleshooting Problems Connecting to MySQL).
Read the two parts of that message. The words “(using password: YES)” mean a password was sent, while “(using password: NO)” means no password was sent at all.
If you do not have shell access, use WP-CLI if your host provides it. The command wp config get DB_NAME prints the value WordPress actually reads, and the same command works for DB_USER, DB_PASSWORD, and DB_HOST.
DreamHost lists wp db check to check the database and wp db repair to repair it in its WP-CLI overview (WordPress wp-cli overview).
How do you fix wrong database user permissions?
A correct password is not enough if the database user lacks rights on the database.
WordPress needs a user with all privileges on its database. The WordPress guide to creating a database shows the grant as GRANT ALL PRIVILEGES ON databasename.* TO "wordpressusername"@"hostname", followed by FLUSH PRIVILEGES (Creating Database for WordPress).
In a hosting panel, the same fix looks different. Open the databases section, find the user linked to your WordPress database, and tick the box for all privileges, then save.
This cause is common after a move to a new host. The database may copy across fine, but the user on the new server may have no rights, or the rights may point at the old database name.
After you fix the rights, reload the site. If the error stays, move on to the repair step, since permissions were not the cause.
For background on the two database systems WordPress uses, see our MySQL vs MariaDB for WordPress guide.
How do you repair damaged database tables?
Repair tables only after the credentials and permissions check out, because repair cannot fix a failed login.
The sign of damaged tables is an admin message saying one or more database tables are unavailable and the database may need to be repaired. Kinsta's troubleshooting docs give that exact message as the sign of a corrupt database (Fixing Error Establishing a Database Connection).
WordPress ships a built-in repair tool. Add this line to wp-config.php, above the line that says to stop editing:
define( 'WP_ALLOW_REPAIR', true );
Then open /wp-admin/maint/repair.php on your own site in your browser and choose “Repair Database” or “Repair and Optimize Database”. The page works without a login, which is why the next step matters.
Remove the WP_ALLOW_REPAIR line from wp-config.php as soon as the repair finishes. While it is present, anyone who knows the repair URL can run database operations on your site.
Two other routes do the same job:
- In phpMyAdmin, open your WordPress database, tick all tables, and choose “Repair table” from the bulk menu.
- With WP-CLI, run
wp db checkfirst, thenwp db repairif the check reports damage.
If repair fails or keeps finding new damage, restore the database from your latest clean backup instead of running repair again. Our guide to fixing database tables that need repair covers the repair routes in more detail.
What if the database server is down or over its quota?
Contact your host, because only the host can fix a down server or a quota limit.
WordPress.org lists two host-side causes: the database has met its quota and has been shut down, or the server is down. Ask your host which one applies to your account.
Before you call, note when the error started and whether you changed anything. A sudden error with no change on your side points to the server, while an error right after a move points to your settings.
Also ask the host to check disk space and the database size. A full disk can stop the database from writing, and that can damage tables or block new connections.
If the host says the server is fine, go back to the credential and permission checks. Do not keep reloading the site while you wait, since each failed request adds load to a server that may already be busy.
How do you use debug mode to find the exact error?
Turn on WordPress debug logging to record the exact error instead of guessing at it.
Add these three lines to wp-config.php, before the line that says “That's all, stop editing!”:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
WordPress.org says this logs all errors, notices, and warnings to a file called debug.log in the wp-content folder, and hides them from visitors (Debugging in WordPress).
Reload the site once, then open wp-content/debug.log and read the newest lines. Look for database errors near the time you reloaded, and copy the exact message before you change anything else.
Turn debug mode off when you finish by setting WP_DEBUG back to false, and delete debug.log if it holds paths or database names. WordPress.org notes these tools are meant for local testing and staging installs, not live sites left in debug mode.
How do you know the fix worked?
Reload the home page and at least one post, then log in to /wp-admin/ and open a page that reads the database.
A fixed site loads the home page, shows posts and pages with their real content, and lets you log in to the admin area. If the admin area loads but a post shows old or missing content, the connection works and a different problem remains.
Check the admin area for a second sign. Open “Posts” and “Pages” and confirm the counts match what you expect, then open “Settings” and confirm your site title and address are correct.
Run wp db check again if you used WP-CLI, and confirm it reports success. If you used the repair tool, confirm the repair page reported no failed tables.
Finally, check your site on a fresh browser tab without being logged in. A visitor view that loads cleanly is the real test, since an admin login can mask a front-end problem.
When should you contact your host?
Contact your host when the cause sits on their side of the connection.
Call them if the credentials in wp-config.php match the panel exactly and the error stays, or if the MySQL client test fails with the panel values. Those two results point away from your files.
Also contact them if the error started with no change on your side, if the database is over quota, or if the server is down. Ask them to confirm the database server status, the database size, and the user privileges in one message, so you get a full answer.
Give them the exact error text, the time it started, and what you already checked. A host can act faster with those three facts than with a note that says the site is down.
If the host finds a hacked site or damaged core files, ask them to restore the latest clean backup. A related server error may need a different fix, so see our guide to fixing the 500 internal server error if that message appears instead.
What mistakes should you avoid?
Do not edit wp-config.php without a copy, and do not leave repair or debug mode switched on.
The most common mistake is changing more than one value at a time. Change one credential, save, and reload, so you know which change fixed the site.
Other mistakes to avoid:
- Leaving
WP_ALLOW_REPAIRset totrueafter a repair, since the repair page needs no login. - Leaving
WP_DEBUGset totrueon a live site, since error messages can show server paths to visitors. - Restoring a backup over a live database without first checking that the backup is clean and recent.
- Reinstalling WordPress before checking the database, since a fresh install does not fix wrong credentials.
- Changing the table prefix in
wp-config.phpto guess at a fix, since the wrong prefix points WordPress at empty tables.
Check the current WordPress hosting requirements if your host suggests a server change, and confirm the MySQL or MariaDB version your plan runs before you accept it.
Frequently asked questions
Will I lose my posts if I see the database connection error?
No, your posts are usually still in the database, since the error means WordPress cannot reach the database, not that the content is gone. Fix the connection first, then check that posts and pages load. If repair fails or tables are missing, restore the database from your latest clean backup.
Why did the error appear right after I moved my site to a new host?
A move changes the database name, user, password, or host, and wp-config.php often still holds the old values. Compare all four values with the new hosting panel, and check that the database user has all privileges on the new database. The DB_HOST value is the one most likely to differ between hosts.
Is it safe to leave WP_ALLOW_REPAIR turned on?
No, it is not safe. The repair page at wp-admin/maint/repair.php works without a login while that line is present, so anyone who knows the address can run database operations on your site. Remove the line from wp-config.php as soon as the repair finishes, then reload the site to confirm it still works.
What does MySQL error 1045 mean for my WordPress site?
Error 1045 means MySQL refused the login, either because the password did not match or because no account matched that user and host. The message shows whether a password was sent, with YES or NO in brackets. Check the four wp-config.php values first, then check the user privileges in your hosting panel.
How long does it take to fix a database connection error?
A wrong credential often takes only a few minutes to fix once you compare wp-config.php with your hosting panel. A damaged table takes longer, since you need to run the repair tool and check the result. A host-side outage has no fixed time, so ask your host for a status update and a time estimate.
Can a plugin cause the database connection error?
A plugin does not usually break the database connection itself, since the error happens before WordPress loads plugins. A plugin can still fill the database, damage a table, or use up server resources, which then blocks the connection. Fix the connection first, then check recent plugin changes if the error keeps coming back.
Sources
- Common WordPress Errors WordPress.org
- wp-config.php Developer.WordPress.org
- Debugging in WordPress WordPress.org
- Creating Database for WordPress Developer.WordPress.org
- Troubleshooting Problems Connecting to MySQL MySQL Documentation
- Fixing Error Establishing a Database Connection Kinsta Docs
- WordPress wp-cli overview DreamHost Knowledge Base
Is your product missing, or something out of date?
Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.
Thanks — we review requests within 3 business days.
