Website Errors

How to Fix WordPress Error Establishing a Database Connection

Error establishing a database connectionPlain English: WordPress tried to open your site's MySQL database and failed — either the login details in wp-config.php are wrong, or the database server itself is down. Your content is almost certainly safe; WordPress just cannot reach it right now.
Quick answer: Open wp-config.php and verify the four database values — DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST — against your hosting panel's MySQL settings. Wrong credentials cause most cases. If the credentials are correct, the MySQL server may be down: test with phpMyAdmin, and repair corrupted tables by adding define('WP_ALLOW_REPAIR', true); and visiting /wp-admin/maint/repair.php. The full steps are below.

What Error establishing a database connection means

WordPress does not store your posts, pages, and settings in files — they live in a MySQL (or MariaDB) database. Every time someone opens a page, WordPress connects to that database using the login details stored in wp-config.php, asks for the content it needs, and builds the page. The "Error establishing a database connection" message appears when that connection attempt fails, so WordPress has nothing to build the page from and shows this plain white screen instead of your site.

The wording is blunt, but the situation is usually less alarming than it looks: this is a connection failure, not data loss. Your content is still sitting in the database untouched in almost every case. The two families of causes are a bad login (wrong database name, username, password, or host in wp-config.php) and a database server problem (MySQL crashed, is being restarted, or is refusing new connections because it is overloaded). One useful clue: if your homepage shows this error but /wp-admin/ shows a message about database tables being unavailable, the fault is corrupted tables rather than the login.

Common causes

How to fix Error establishing a database connection — step by step

The whole fix takes about thirty minutes, and step 1 tells you which of the remaining steps you actually need.

  1. Check wp-admin to isolate the fault. Open yoursite.com/wp-admin/ in the browser. If the dashboard shows the same database error, the problem is the database login or the database server itself — continue with steps 2 through 4. If wp-admin instead shows "One or more database tables are unavailable. The database may need to be repaired," your tables are corrupted and you can skip straight to step 5. This one check tells you which fix to pursue first, so do not skip it.
  2. Verify the database credentials in wp-config.php. Open wp-config.php in cPanel File Manager or an FTP client and compare the four values with your hosting panel's MySQL Databases page: DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST. Re-copy the password from the panel rather than typing it from memory — passwords changed during a migration or a security reset are the classic culprit. Check DB_HOST carefully too: it is localhost on many hosts, but managed and cloud hosts often use a separate database hostname, and a migration silently breaks this value. Correct anything that differs, save the file, and reload the site.
  3. Confirm the database server is actually running. Log in to your hosting panel and look for the services or server status section — a stopped MySQL or MariaDB service needs a restart from the host's side. A quicker test is phpMyAdmin: open it from the hosting panel, and if it cannot connect either, the database server is down and no WordPress setting will fix it — this is a host problem, not a WordPress problem. If other sites on the same server show the same error, the whole database server is affected. Open a support ticket and ask the host to check and restart the MySQL service.
  4. Wait out a transient overload, then add caching. On shared hosting, the error sometimes appears during traffic spikes or server maintenance and clears on its own within 10 to 15 minutes — the server temporarily refuses new database connections because it hit its max_connections limit. Wait, then retest before changing anything. If the error keeps coming and going on a schedule, every page load is hammering the database: install a caching plugin so most visits never touch MySQL at all, and talk to your host about a plan with a higher connection limit if it keeps recurring.
  5. Repair corrupted database tables. If wp-admin reported that tables are unavailable, open wp-config.php and add this line above the "That's all, stop editing!" comment: define('WP_ALLOW_REPAIR', true);. Then visit yoursite.com/wp-admin/maint/repair.php and click Repair Database (Repair and Optimize also works). Wait for every table to report OK, then reload your site. Important: remove the WP_ALLOW_REPAIR line from wp-config.php immediately afterward — the repair page is reachable by anyone who knows the address while that line is present, and you do not want strangers running database repairs.
  6. Fix the table prefix and database user privileges after a migration. In phpMyAdmin, check whether your tables start with the same prefix that wp-config.php sets in $table_prefix — for example, the config says wp_ but the actual tables are wp_ab12_. If they differ, update $table_prefix in wp-config.php to match the real tables. Then, in the hosting panel under MySQL Databases, confirm the database user is assigned to the database with ALL PRIVILEGES checked. A user without privileges or a mismatched prefix produces exactly this error right after a migration or restore, even when the password is correct.

Did this fix it? Quick checklist

If the checklist is clean and the error persists, the fault is deeper in the server: ask your host to check the MySQL error log for crashes and whether the server is hitting its max_connections limit. As a last resort, restore the site from a recent backup — hosts like Bluehost, SiteGround, and Hostinger keep automatic daily backups, and a full restore (files plus database) replaces anything the repair tool could not save. And once the site is back, install a backup plugin and keep your own copies off the server, so the next database hiccup is a five-minute restore instead of a scramble.

Frequently asked questions

What does "Error establishing a database connection" actually mean?

WordPress stores all your posts, pages, and settings in a MySQL database. On every page load, WordPress opens a connection to that database using the login details in wp-config.php. This error means the connection failed — either the login details are wrong, or the database server is down or refusing connections. The site cannot load any content until the connection works again.

Did I lose my website's content?

In almost all cases, no. Your posts, pages, and media files are still sitting in the database — this is a connection failure, not data loss. The exception is a database that is corrupted beyond repair or was actually deleted, which is rare. Once you fix the connection, the site comes back exactly as it was. This is also why regular backups matter: if the worst happens, a backup restores everything in minutes.

Why does the error appear and then disappear on its own?

On shared hosting, the database server has a cap on simultaneous connections. During traffic spikes — a post going viral, a scheduled backup running, a bot crawl — the server hits that cap and starts refusing new connections, and WordPress shows this error. When the load drops, the error vanishes. If it keeps happening, a caching plugin reduces database hits, and a hosting plan with more headroom solves it permanently.

wp-admin shows a message about repairing the database instead. What do I do?

That message means one or more database tables are corrupted — usually after a failed update or a server crash. Add define('WP_ALLOW_REPAIR', true); to wp-config.php, visit yoursite.com/wp-admin/maint/repair.php, and click Repair Database. Once every table reports OK, delete that line from wp-config.php right away, since the repair page stays publicly reachable while it is there.