WordPress

Error Establishing a Database Connection in WordPress: How to Fix It

October 28, 2026 · 1 views

"Error establishing a database connection" means WordPress could not talk to its MySQL or MariaDB database, so it has nothing to show. Your posts, pages and orders are stored in that database and are almost never lost. Something between WordPress and the database is broken, and it is nearly always one of four things. Check them in this order: the fastest and most common first.

1. Is it the whole site or only the admin?

Open the home page and /wp-admin/. If the front end shows the error but the admin says "One or more database tables are unavailable. The database may need to be repaired", the connection works and a table is damaged: jump to step 3. If both show the same error, WordPress cannot log in to the database at all: continue with step 2.

2. Check the database login in wp-config.php

This is the cause after a migration, a host change or a password reset in cPanel. Open wp-config.php in the site's root folder (File Manager or FTP) and find these four lines:

define( 'DB_NAME', 'example_wp' );
define( 'DB_USER', 'example_user' );
define( 'DB_PASSWORD', '...' );
define( 'DB_HOST', 'localhost' );

Compare them with what the hosting panel shows. In cPanel open MySQL Databases:

  • The database name and user usually carry the account prefix, e.g. example_wp, not just wp.
  • The user must be listed under Current Databases → Privileged Users for that database. If not, add it with All Privileges.
  • If you are not sure about the password, set a new one for the user in cPanel and paste the same one into DB_PASSWORD.
  • DB_HOST is localhost on most cPanel hosts. Some hosts use a separate server name, shown in their panel.

Save the file and reload. If the site comes back, you are done.

3. Repair crashed tables

A server crash or a full disk can mark a table as crashed. Add this line to wp-config.php above "That's all, stop editing!":

define( 'WP_ALLOW_REPAIR', true );

Then open https://your-site.com/wp-admin/maint/repair.php and click Repair Database. When it finishes, remove that line again: the repair page needs no login while it is enabled. In cPanel you can do the same from MySQL Databases → Repair Database or from phpMyAdmin (select the tables, then Repair table).

4. Is the database server itself down or full?

If the login is right and nothing is crashed, the database server may be refusing connections:

  • The error comes and goes under traffic: the account hits its connection or process limit. Common on cheap shared hosting, and on sites under a bot attack hitting wp-login.php or xmlrpc.php.
  • The disk is full: MySQL cannot write and stops. Check the disk usage in cPanel and delete old backups or logs.
  • The MySQL service stopped: on a VPS run systemctl status mariadb (or mysql) and start it. On shared hosting, ask the host: you cannot restart it yourself.

A quick test: create a file dbtest.php in the site root with the same four values and try to connect. If it fails too, the problem is on the server, not in WordPress. Delete the file afterwards.

5. Hacked sites can show this error too

If wp-config.php was changed and you didn't do it, or there are unknown PHP files in the root folder, the site may have been compromised and the attacker broke the config. Restore a clean wp-config.php, change the database password and every admin password, and have the site scanned. See signs your website is hacked.

What not to do

  • Don't reinstall WordPress or delete the database: the content is in there and is usually fine.
  • Don't restore an old backup first: you lose every order and post since then, and the real cause stays.
  • Don't leave WP_ALLOW_REPAIR enabled.

Need it fixed now?

If the site is a store losing orders, or the steps above didn't bring it back, we fix database connection errors at a fixed price: free diagnosis, a backup before any change, and you pay only after you have checked that the site works. For a site that is down right now, use the Emergency Fix (work starts within 1 hour, 24/7).

Is your website having a similar issue?

We diagnose and fix it within 24 hours — you only pay after the fix is confirmed.

Get a Free Diagnosis

More from the blog