WordPress fix guide
How to fix "Error establishing a database connection" on WordPress
This error means WordPress is running but cannot reach the database that holds your content and settings. In most cases it is fixed by correcting the login details in wp-config.php, repairing the tables, or asking your host to restart the database server.
The message "Error establishing a database connection" means WordPress started, but it could not talk to the MySQL or MariaDB database that stores your posts, pages, users and settings. Without that database WordPress has nothing to show, so visitors see only this one line. The usual causes are wrong login details, a database server that is down or overloaded, or damaged tables.
First, work out how bad it is
Open yourdomain.com/wp-admin in your browser and see what happens.
- The same error: WordPress cannot connect at all. Suspect the login details or the database server.
- A message such as "One or more database tables are unavailable" or an offer to repair the database: the connection works, but some tables are damaged.
- The error comes and goes: the database server is probably hitting limits on your hosting plan.
Before you change anything
Make sure you can log in to your hosting control panel and open the site files, either through the host's file manager or over SFTP. Copy wp-config.php to your computer before editing it. Also check whether your host keeps its own backups, because you may need one later. If you have no backup at all, read what to do about having no website backups once the site is back.
Step by step
- Wait a few minutes and reload. On shared hosting, a traffic spike or a busy neighbour can make the database refuse connections for a short while. Check your host's status page. If the error keeps returning, the site may be hitting resource limits, which can also show up as a 503 error.
- Open `wp-config.php`. It sits in the WordPress root folder (the one containing
wp-adminandwp-content), and sometimes one folder above it. 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' );
- Compare them with your hosting panel. In cPanel, the MySQL Databases page lists database names and users. Other hosts use different screens, so look for a section called Databases. Names often carry an account prefix. If you are unsure of the password, set a new one for the database user in the panel and paste it into
wp-config.php. Watch for stray spaces and missing quote marks. Changing the password inwp-config.phpalone does not change it on the server: both must match.
- Test the login in phpMyAdmin. Most hosts offer phpMyAdmin from the control panel. If it opens the database with that user and password, the credentials are fine. Also check that the user is assigned to that database with privileges.
- Check `DB_HOST`. On many hosts it is
localhost, but some give a specific hostname, sometimes with a port. Your host's documentation will say. This is a common cause right after moving a site to a new host.
- Check the database has tables. In phpMyAdmin, open the database and look for tables such as
wp_postsandwp_options. The prefix (herewp_) must match the$table_prefixline inwp-config.php. An empty database means you are pointing at the wrong one, or the data is gone and you need a backup.
- Run WordPress's built in repair. Add this line to
wp-config.php, above the line that says to stop editing:
define( 'WP_ALLOW_REPAIR', true );
Then visit yourdomain.com/wp-admin/maint/repair.php and click Repair Database. Remove the line afterwards. While it is present, that page works for anyone, without logging in.
- Contact your host if the database server may be down. Only the host can restart it or tell you about a full disk, an account over its quota or a connection limit. Give them the exact time and the exact message you see.
- Restore a backup if the tables are beyond repair. Restore a database copy from before the problem started. Anything published after that backup will be lost, so note what changed since then.
If it started after a plugin or update
A plugin rarely causes this error directly, but a heavy one can overload a small hosting plan. Using SFTP, rename wp-content/plugins to plugins-off and reload the site. If it loads, rename the folder back and reactivate plugins one at a time in the dashboard under Plugins until the problem returns.
Keeping it from coming back
Keep backups somewhere other than the web server, and know how long your host takes to respond to support requests. Repeated database outages are easy to miss if you only notice them when a customer calls. The uptime monitoring report in week 29 of SiteMonday shows whether your site has been going down. Fixing the cause is still up to you or your developer.
For the version of this guide that is not specific to WordPress, see the general database connection error guide.
Not on WordPress? Read the general guide to error establishing a database connection, or the guide for WooCommerce, cPanel, Bluehost, GoDaddy, SiteGround, Hostinger.
Questions
Will I lose my posts and pages because of this error?
Usually not. Your content lives in the database, and this error normally means WordPress cannot reach it, not that it was deleted. Content is only lost if the tables were dropped or you restore an older backup.
Why does /wp-admin show a different message from the front of my site?
If /wp-admin asks you to repair the database, the connection works but one or more tables are damaged. Use the repair step with `WP_ALLOW_REPAIR`, and remove that line afterwards.
Is it safe to leave WP_ALLOW_REPAIR in wp-config.php?
No. While the line is there, the repair page can be opened by anyone without logging in. Delete the line as soon as the repair has finished.
Can I fix this from the WordPress dashboard?
No, because you cannot log in while WordPress has no database connection. The fix happens in your hosting panel, in phpMyAdmin and in the `wp-config.php` file.
I changed the database password in wp-config.php and now it fails. Why?
The password in `wp-config.php` only tells WordPress what to send. It must match the password set for the database user in your hosting panel, so set the same new password in both places.