Skip to content
Diamond Sierra

Error establishing a database connection

WordPress shows this when it cannot reach its database. Your content is still there. Check the login details in wp-config.php, then the database server, then the tables.

Updated · By Diamond Sierra

On this page

This message means WordPress could not log in to its MySQL or MariaDB database, so it has nothing to build the page from. Your posts, orders and settings are not lost; they are still in the database. The cause is almost always one of four things: wrong login details, a database server that is down or overloaded, damaged tables, or a host limit on connections.

Error establishing a database connection

Is my content gone?

No. The message only says WordPress could not connect. Nothing is deleted when the connection fails, and the site comes back as soon as the connection works again. Don't restore a backup or reinstall WordPress to fix this message; that can replace newer content with older content.

How do I check the database login in wp-config.php?

Open wp-config.php in your hosting file manager and find these four lines:

define( 'DB_NAME', 'example_wp' );
define( 'DB_USER', 'example_user' );
define( 'DB_PASSWORD', 'the-password' );
define( 'DB_HOST', 'localhost' );

Compare each value with the database and user listed in your hosting control panel. This is the usual cause right after a site is moved to a new host, a database password is changed, or a database user is deleted. DB_HOST is often localhost, but some hosts use a separate server name or a port, such as db.example.com:3306; your host's documentation gives the right value. Also check that the user is still assigned to the database with all privileges.

How do I know if the database server is down?

Try to open the database in phpMyAdmin from your hosting control panel. If phpMyAdmin can't connect either, or every site on the same account shows the same message, the database server itself is down or out of resources. That is the host's problem to fix: contact them and say the database server is refusing connections.

Why does the error come and go?

An error that appears for a few minutes and then clears usually means the database ran out of connections or memory under load. Each visitor's request holds a connection while it runs, so slow queries, a burst of bot traffic or a heavy scheduled job can use them all up. Shared hosting often limits connections per account (max_user_connections). The fix is to find what keeps the database busy: slow queries, pages that run hundreds of queries, and scheduled jobs that pile up.

How do I repair damaged WordPress database tables?

If the dashboard says "One or more database tables are unavailable. The database may need to be repaired", the tables are damaged. Take a backup of the database first, then add this line to wp-config.php:

define( 'WP_ALLOW_REPAIR', true );

Open /wp-admin/maint/repair.php on your site and choose Repair Database. Remove the line again when you are done, because the repair page works without logging in while it is there. You can also repair tables from phpMyAdmin, or ask your host.

What should I check after it works again?

Find out why it happened, so it doesn't happen again: a password change nobody wrote down, a host outage, or a site that runs out of connections at busy times. For the last one, look at slow queries and the busiest pages before the next traffic spike.

How Paul Bunyan helps

No WordPress plugin can run while the database is unreachable, Paul Bunyan included, so it can't diagnose a wrong password or a server that is down. It helps with the kind that comes and goes. Paul Bunyan flags slow database queries (PB-2501), slow queries that read a whole table (PB-2504), requests that run 100 or more queries (PB-2503) and slow requests (PB-2101), and names the plugin behind each one. That shows you what keeps the database busy before it runs out of connections.