Fix the WordPress Error Establishing a Database Connection
WordPress error establishing a database connection? Check wp-config.php, your database server and damaged tables in a safe order that protects data.
Murad Raza
Freelance Full-Stack Developer

The WordPress error establishing a database connection usually means your site cannot reach the database that stores its pages, posts, settings and orders. The most common cause is wrong login details in wp-config.php. After that, look at a database server that is down or over its limits, then at damaged tables. Work through them in that order, because the first checks change nothing on your site.
I'm Murad Raza, a freelance web developer who has built websites, including WordPress sites, since 2022. The steps below come from the official WordPress and MySQL documentation and the WordPress source code, not from a test I ran for this article.
Key Takeaways
- The error usually describes a failed connection. On its own, it does not mean your content is gone.
- Save copies, then check the four database values in
wp-config.php.- If those are right, ask your host whether the database server is running or over a limit.
- Use the WordPress repair tool only when tables are damaged, then switch it off.
- Restoring a backup can wipe recent orders, so treat it as the last step.
What the error actually means
WordPress builds each page by asking a MySQL or MariaDB database for its content. When that connection fails, visitors get a plain error page instead.
The WordPress common errors guide says the error is "usually caused by an error in your wp-config.php file". It also lists a database that has hit its hosting quota, a server that is down and a compromised site.
Use this table to choose where to start:
| What you notice | Likely cause | First move |
|---|---|---|
| Started after a migration or password change | Wrong details in wp-config.php | Compare the four values |
| Comes and goes, worse at busy times | Busy or overloaded database server | Ask your host for logs |
| Every site on the account is down | Database server is down | Contact your host |
/wp-admin says tables are unavailable | Damaged or missing tables | Export, then Step 5 |
Step 1: Protect what you have
Before editing anything, download a copy of wp-config.php using your host's file manager. If you make a typo, you can put the original straight back.
Next, try exporting the database from phpMyAdmin (your host's database tool) or your hosting panel. This often still works when the fault is in wp-config.php, because the panel logs in separately. If the export fails, check when your host last backed it up. The WordPress backup guide points out that a full restore needs both the database and the files.
Step 2: Read the message in wp-admin
Open yoursite.com/wp-admin. WordPress shows public visitors a shorter error and the admin area a fuller one. The wording tells you where to go next:
- "This either means that the username and password information..." points to your credentials or the database server. Go to Step 3.
- "Cannot select database" means the server answered but the database name or that user's access is wrong. Go to Step 3.
- "One or more database tables are unavailable" means WordPress connected but found a table problem. Go to Step 5.
- Any other wording means start at Step 3.
Step 3: Check the details in wp-config.php
Open wp-config.php in the main WordPress folder and 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' );
Check these against the database section of your hosting panel, in this order:
- A missing prefix. Many hosts add your account name, so
shop_dbmay really beaccount_shop_db. - The wrong host.
localhostis common, but some hosts give you a separate database address. - Missing access. Confirm the user is assigned to this database with all privileges.
- An old password. Reset it only if everything above is right and the message still points to credentials. Avoid single quotes and backslashes and paste it into
DB_PASSWORD. If you later restore your saved copy, enter the new password again.
Save the file and reload your site.
Step 4: Ask whether the database server is healthy
If the details are correct, the problem is probably on the server. MySQL's documentation says its "can't connect" error, number 2002, normally means no MySQL server is running or the connection settings point to the wrong place.
Traffic can cause it too. A logged "Too many connections" error means all available connections are in use, up to a limit set by max_connections. On shared hosting, only your host can change connection limits.
Send your host a specific message:
My WordPress site shows "Error establishing a database connection". I have confirmed the details in wp-config.php. Is the database server running, has my database hit a quota, and do the logs show too many connections?
Step 5: Repair damaged tables
Use this step only if WordPress reports unavailable tables or your host confirms damage. Export the database first. If the export fails, ask your host for a fresh copy before you run a repair.
The WordPress repair tool uses MySQL's repair command, which works for MyISAM, ARCHIVE and CSV tables, not InnoDB, a common WordPress setup. phpMyAdmin's Type column shows each table's engine. If any table fails to repair, including a missing table or an unsupported engine, stop and ask your host to recover it.
To run the repair tool, add this line to wp-config.php, above the "stop editing" comment:
define( 'WP_ALLOW_REPAIR', true );
Then visit yoursite.com/wp-admin/maint/repair.php and choose Repair Database.
Remove that line as soon as it finishes. While it is active, anyone can open the repair page without logging in.
Step 6: If it still fails
Check whether wp-config.php contains anything you do not recognise, or changed when nobody on your team touched it. Either can be a sign of a compromised site, and my WordPress security checklist covers what to lock down next.
Restoring a backup is the last resort. It replaces your current database with the older copy, so a WooCommerce store would lose any orders placed since that backup. Ask your host whether those can be recovered first.
How to stop it happening again
Keep automatic off-site backups and test a restore occasionally. Set up uptime alerts so you hear about an outage before a customer does. If connection limits keep causing trouble, find out why your WordPress site is slow and review heavy plugins, or move to a plan with more capacity.
If you would rather not handle this yourself, a website maintenance plan can cover backups, updates and monitoring.
Frequently asked questions
01Have I lost my website content?
02Why did the error appear after moving hosts?
03Why does the error come and go?
04Is it safe to leave the repair tool switched on?
Need this fixed on your site?
I Build Websites That Work as Hard as You Do


