Last updated: December 2, 2026 · Skill level: Beginner to intermediate
“Error establishing a database connection” means WordPress could not talk to its MySQL or MariaDB database using the details in wp-config.php. The usual causes are wrong credentials, a wrong DB_HOST value, a database server that is down or overloaded, or a damaged options table. Test the credentials first, then work down the list below.
Quick fix checklist (TL;DR)
Work through these in order. Back up wp-config.php before you change anything in it.
- Load
/wp-admin/. If you see “One or more database tables are unavailable”, the connection works and a table needs repair (fix 5). - Compare DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php with your hosting panel (fix 1).
- Test those exact values with a small PHP file, then delete it (fix 2).
- Try
127.0.0.1instead oflocalhost, or add the port or socket your host gives you (fix 3). - Check whether MySQL is down, out of connections or out of disk space (fix 4).
- After a migration, check credentials, the table prefix and siteurl/home (fix 6).
- Rename
wp-content/db.phpand verify core files (fix 7).
What does “error establishing a database connection” mean?
It means WordPress stopped before loading any page because it could not open a working database connection, or it connected but could not read its core settings. Every WordPress page is built from database content, so without that connection WordPress has nothing to show and prints this one-line error instead.
The connection code in the wpdb class has a longer message asking whether the credentials, hostname and server are correct. Live sites show only the short heading. You see the long version, plus the raw MySQL error, only when WP_DEBUG and WP_DEBUG_DISPLAY are both true.
This is a different problem from the “There has been a critical error on this website” screen, which comes from a PHP fatal error. If that is what you see, follow our guide to fixing the WordPress critical error instead.
Why does wp-admin say “One or more database tables are unavailable”?
That message means the connection worked, but WordPress could not read the siteurl value from its options table while other WordPress tables exist. It usually points to a crashed or damaged table, not bad credentials. The front end hides this detail and shows the generic database error.
This comes straight from is_blog_installed() and dead_db() in WordPress core. In the admin, dead_db() prints the detailed error. On the front end it prints only “Error establishing a database connection”. So loading yoursite.com/wp-admin/ is a free diagnostic test:
- Same generic error in wp-admin: the connection itself fails. Check credentials, host and server (fixes 1 to 4).
- “One or more database tables are unavailable”: connected, but a table is broken. Go to fix 5.
- The WordPress install screen appears: WordPress connected but found no tables with its prefix. Check
$table_prefix(fix 6).
Symptom, cause and fix: a decision table
Match what you see to the most likely cause before you change anything. The MySQL error codes in the middle column come from the test file in fix 2 or from your PHP error log. They are listed in the MySQL error reference.
| What you see | Likely cause | Fix |
|---|---|---|
| Error everywhere, test file says 1045 “Access denied” | Wrong DB_USER or DB_PASSWORD, or user not granted on the database | Fix 1: reset the password in the panel and update wp-config.php |
| Test file says 1049 “Unknown database” | Wrong DB_NAME (often a missing account prefix like cpuser_) | Fix 1: copy the exact name from the panel |
| Test file says 2002 or 2005 | Wrong DB_HOST, wrong socket, or MySQL not running | Fix 3, then fix 4 |
| Test file says 1040 “Too many connections” | Connection limit reached (traffic spike, bots, slow queries) | Fix 4 |
| wp-admin says “One or more database tables are unavailable” | Crashed or damaged options table | Fix 5 |
| Install screen appears after a move | Wrong $table_prefix | Fix 6 |
| Test file connects fine, WordPress still fails | Faulty wp-content/db.php drop-in or damaged core files | Fix 7 |
| Error on localhost (XAMPP, MAMP, Local, Docker) | MySQL service stopped or a non-default port | Start MySQL, then fix 3 |
Fix 1: Check the database credentials in wp-config.php
Open wp-config.php in your site root and compare four values with what your hosting panel shows: DB_NAME, DB_USER, DB_PASSWORD and DB_HOST. One wrong character in any of them breaks the connection. This is the most common cause, especially after a password change or a host move.
- Download a copy of wp-config.php to your computer as a backup.
- Open the live file in your host’s file manager or over SFTP and find the database block.
- In your hosting panel (for example cPanel “MySQL Databases” or SiteGround Site Tools > Site > MySQL), confirm the database name, the user name, and that the user is assigned to that database with all privileges.
- If you are not sure of the password, reset it in the panel and paste the new one into wp-config.php. Do not try to read the old one.
- Save, then reload the site in a private window.
The block you are checking in wp-config.php looks like this:
// In wp-config.php (site root)
define( 'DB_NAME', 'cpuser_wp123' );
define( 'DB_USER', 'cpuser_wpuser' );
define( 'DB_PASSWORD', 'your-real-password' );
define( 'DB_HOST', 'localhost' );
Watch for stray spaces inside the quotes, curly quotes pasted from a word processor, and a password containing ', which ends the string early.
Fix 2: Test the credentials with a small PHP file
A ten-line PHP test file connects with the same credentials, outside WordPress, and prints the real MySQL error code. That one number tells you whether the problem is the password, the database name, the host or the server, so you stop guessing.
- Create a file named
dbtest-7f3k.phpin your site root (the folder that holds wp-config.php). Use a random name so nobody can guess it. - Paste the code below and replace the four values with the ones from wp-config.php.
- Visit
https://yoursite.com/dbtest-7f3k.phpand note the result. - Delete the file immediately. It contains your database password in plain text.
<?php
// dbtest-7f3k.php in the site root. DELETE AFTER USE.
mysqli_report( MYSQLI_REPORT_OFF );
$link = @mysqli_connect( 'localhost', 'cpuser_wpuser', 'your-real-password', 'cpuser_wp123' );
if ( ! $link ) {
echo 'Failed: ' . mysqli_connect_errno() . ' ' . htmlspecialchars( mysqli_connect_error() );
exit;
}
echo 'Connected OK. Server version: ' . mysqli_get_server_info( $link );
mysqli_close( $link );
The code uses mysqli_connect(), the same PHP extension WordPress uses. If DB_HOST includes a port such as 127.0.0.1:3307, pass the host and port separately: mysqli_connect( '127.0.0.1', $user, $pass, $db, 3307 ). Read the result against the decision table above. “Connected OK” while WordPress still fails sends you to fix 5 or fix 7.
If you have SSH, WP-CLI gives a faster check that reads wp-config.php directly: wp db check. If it connects, the credentials are fine.
Fix 3: Use the right DB_HOST value for your host
DB_HOST tells WordPress where MySQL lives. On most shared and managed hosts it is localhost, but some hosts use a remote hostname, a non-default port, or a Unix socket path. A wrong value gives MySQL error 2002 or 2005 even when the username and password are perfect.
The formats WordPress accepts are documented in the wp-config.php handbook. WordPress parses the value as host, then optional port, then optional socket.
| Setup | DB_HOST value | Source |
|---|---|---|
| Database on the same server (most hosts) | localhost | WordPress default |
| Same server, force TCP instead of socket | 127.0.0.1 | wp-config.php handbook |
| Non-default port | 127.0.0.1:3307 or mysql.example.com:3307 | wp-config.php handbook |
| Unix socket | localhost:/var/run/mysqld/mysqld.sock (use your host’s path) | wp-config.php handbook |
| Kinsta | localhost | Kinsta docs |
| SiteGround | localhost or 127.0.0.1 | SiteGround KB |
| cPanel hosts (default) | localhost unless the host names a remote server | Kinsta docs |
| Remote or cloud database (for example Amazon RDS) | The endpoint hostname shown in the provider console | AWS RDS docs |
Why does localhost fail when 127.0.0.1 works? PHP treats localhost specially and tries a local socket or pipe when it can, per the mysqli docs on php.net. If PHP’s default socket path does not match where MySQL actually puts its socket, localhost fails with error 2002 while 127.0.0.1 connects over TCP. The same thing can make localhost:3307 ignore the port, as discussed in Trac ticket #33842.
Error establishing a database connection on localhost. On XAMPP, MAMP, Local or Docker, the usual cause is that the MySQL service is not running. Start it in the app’s control panel first. Then check which port the app’s MySQL uses and set DB_HOST to 127.0.0.1:PORT. In Docker, DB_HOST is the database service name from your compose file, not localhost.
Fix 4: Check if the database server is down or overloaded
If the credentials are right and the error appears suddenly, or only at busy times, the MySQL server itself is the likely problem. It may be stopped, out of connections, or out of disk space. On shared hosting you cannot fix these yourself, but you can confirm them quickly.
- Check your host’s status page and email for an outage notice.
- Open phpMyAdmin from your panel. If it also cannot connect, the server is down or refusing connections. That is a host ticket.
- Check disk usage in your panel. When the disk or your quota is full, MySQL can stop writing or fail to start.
- If the test file showed error 1040, the server hit its connection limit. Look at your access log for a traffic spike, a bot crawl or a flood of
xmlrpc.phporwp-login.phprequests. - On a VPS you control, check and restart the service.
# On a VPS, over SSH (service may be named mysql or mariadb)
sudo systemctl status mysql
df -h
sudo systemctl restart mysql
# Inside the mysql client, check the connection limit
SHOW VARIABLES LIKE 'max_connections';
SHOW STATUS LIKE 'Threads_connected';
According to the MySQL 8.4 manual, “Too many connections” means every allowed connection is in use, and MySQL keeps one extra slot for an admin account so you can run SHOW PROCESSLIST. Raising max_connections hides the problem if bots or slow queries are the cause. A page cache or CDN keeps traffic spikes away from MySQL.
Fix 5: Repair corrupted database tables
If wp-admin says “One or more database tables are unavailable”, a table, usually wp_options, is damaged. WordPress has a built-in repair page you can switch on in wp-config.php, and WP-CLI has the same tools from the command line. Back up the database before any repair.
Use your host’s backup tool or phpMyAdmin’s Export tab, and test that the backup restores.
Option A: WordPress repair page (no SSH needed)
- Back up wp-config.php and the database.
- Add
define( 'WP_ALLOW_REPAIR', true );to wp-config.php, above the line that says “That’s all, stop editing!”. - Visit
https://yoursite.com/wp-admin/maint/repair.php. - Click “Repair Database”. Use “Repair and Optimize Database” only if you have time, since it is slower.
- Remove the line from wp-config.php as soon as it finishes.
The last step matters. The wp-config.php handbook says that while WP_ALLOW_REPAIR is on, “a user does not need to be logged in” to use the page. Anyone who finds the URL can run it.
Option B: WP-CLI
# Run from the WordPress root folder over SSH
wp db export before-repair.sql
wp db check
wp db repair
Per the WP-CLI db check docs, these commands run MySQL’s mysqlcheck utility with the credentials from wp-config.php. One honest limit: the MySQL REPAIR TABLE docs say repair works for MyISAM, ARCHIVE and CSV tables. Most modern WordPress tables use InnoDB. If an InnoDB table is corrupted, repair will not fix it. Restore that table from a backup or ask your host to recover it.
Fix 6: Fix the error after a migration
After moving a site to a new host or domain, the error almost always means wp-config.php still has the old server’s credentials or host. The new host created a new database, user and password, often with an account prefix added to the names. Copy all four values from the new panel.
- Confirm the database was actually imported. Open it in phpMyAdmin and check that tables exist.
- Update DB_NAME, DB_USER, DB_PASSWORD and DB_HOST from the new host’s panel (fix 1).
- Check that
$table_prefixin wp-config.php matches the table names. Tables namedwpx7_optionsneed$table_prefix = 'wpx7_';. A mismatch shows the install screen. Do not run the installer. - Once the site connects, check siteurl and home. Wrong values cause redirects to the old domain, not a database error.
# Back up first, then run from the WordPress root
wp db export before-url-change.sql
wp option get siteurl
wp option get home
wp search-replace 'https://old-domain.com' 'https://new-domain.com' --all-tables --dry-run
Remove --dry-run once the preview looks right. Next time, follow our checklist to migrate WordPress without downtime.
Fix 7: Check the db.php drop-in and core files
If the test file connects but WordPress still fails, look at wp-content/db.php. Per require_wp_db(), WordPress loads this optional drop-in before its own connection, so a broken one breaks every request.
- Rename
wp-content/db.phptodb.php.offand reload the site. - If the site loads, update the caching or debugging plugin that created it. If no plugin explains it, treat it as possible malware.
- Verify core files with
wp core verify-checksums. See our guide to verifying WordPress core checksums for reading the output. - If files are modified, replace core with
wp core download --force --skip-content. It does not touch wp-content or wp-config.php.
When should you contact your host, and what should you ask?
Contact your host when the test file fails with correct credentials, when phpMyAdmin also cannot connect, when you see error 1040, or when the error comes and goes. Only the host controls the database server. Copy these questions into your ticket:
- “My site [domain] shows ‘Error establishing a database connection’ since [time and timezone].”
- “A mysqli test with the wp-config.php credentials returns error [code and message].”
- “Is the MySQL/MariaDB server for my account running, and was there an outage or restart at that time?”
- “What is the correct DB_HOST (hostname, port or socket) for database [name]?”
- “Did my account hit a connection, CPU or disk limit? What is my max_connections or max_user_connections value?”
- “Can you check [database] for corrupted tables and restore from your latest backup if needed?”
How do you prevent database connection errors?
You prevent most of them with three habits: monitoring so you hear about downtime first, tested backups so corruption is a ten-minute restore, and caching so traffic spikes do not exhaust MySQL connections. None of these needs a paid tool to start.
| Habit | What to set up | Prevents |
|---|---|---|
| Uptime monitoring | A check every 1 to 5 minutes that looks for the text “Error establishing” in the page, not just a 200 status | Hours of unnoticed downtime |
| Database backups | Daily off-server backups, plus a restore test every quarter | Data loss from InnoDB corruption |
| Page caching or CDN | Full-page cache so anonymous visitors never hit MySQL | “Too many connections” during spikes |
| Bot control | Rate-limit or block abusive hits to wp-login.php and xmlrpc.php | Connection floods |
Frequently asked questions
Will I lose my content because of this error?
Usually not. The error means WordPress cannot reach or read the database, not that the data is gone. Wrong credentials, a wrong host or a stopped server leave your posts untouched. Only real table corruption can lose data, which is why you back up before running any repair.
Why does only wp-admin show “One or more database tables are unavailable”?
WordPress shows the detailed message only in the admin area. On the front end it shows the short generic error. Both come from the same check: the connection worked, but WordPress could not read siteurl from the options table, so a table likely needs repair.
Is it safe to leave WP_ALLOW_REPAIR enabled?
No. While WP_ALLOW_REPAIR is true, anyone can open /wp-admin/maint/repair.php without logging in and run repairs or optimizations on your database. Add the line, run the repair, then remove the line from wp-config.php right away.
Should DB_HOST be localhost or 127.0.0.1?
Use whatever your host documents, which is usually localhost. If localhost fails with error 2002, try 127.0.0.1. PHP may use a socket for localhost but always uses TCP for 127.0.0.1, so a socket path mismatch only breaks localhost.
Why did the error start right after I migrated my site?
wp-config.php still holds the old server’s database name, user, password or host. Copy all four values from the new host’s panel, confirm the database was imported, and check that $table_prefix matches the imported table names.



Leave a Reply