Few things are more terrifying for a website owner than typing your URL into a browser only to be greeted by a blank white page displaying a blunt, single-line error: “Error establishing a database connection”. When this error occurs, your entire site is completely offline. Neither visitors can view your content, nor can administrators access the /wp-admin/ dashboard.
Because WordPress is a dynamic Content Management System driven by PHP and MySQL/MariaDB, every single post, page, user account, and theme setting lives inside your database. When WordPress cannot communicate with that database, everything grinds to an immediate halt. In this step-by-step developer tutorial, we will explain the root causes of this error and guide you through fixing it quickly and safely in 2026.
What Causes “Error Establishing a Database Connection”?
In 99% of cases, this error is triggered by one of four specific technical failures:
- Incorrect Database Credentials: Your database name, username, password, or host hostname in
wp-config.phpdo not match MySQL server settings. - Corrupted WordPress Database Tables: A sudden server crash, abrupt plugin update, or unoptimized table overhead corrupted vital database tables.
- Exhausted Server MySQL Resources: Your web hosting server ran out of memory, or the MySQL daemon crashed due to high traffic or unoptimized database queries.
- Host IP / Socket Mismatch: Your web host moved your account or changed the MySQL database host from
localhostto an internal IP address or Unix socket.
Step 1: Check If the Error Also Appears on /wp-admin/
Before touching any code, navigate to https://yourdomain.com/wp-admin/. Observe what message appears on your screen:
- If you see the identical “Error establishing a database connection”: Proceed immediately to Step 2 (your database credentials or MySQL server are down).
- If you see “One or more database tables are unavailable. The database may need to be repaired”: Your database is corrupted. Skip straight to Step 3 for the built-in repair tool.
Step 2: Verify wp-config.php Database Credentials
The most common culprit—especially after migrating your site or changing hosting passwords—is mismatched credentials. Connect to your server using FTP, SSH, or cPanel File Manager, and open the wp-config.php file located in your root directory.
Locate the following four lines of code:
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** Database username */
define( 'DB_USER', 'username_here' );
/** Database password */
define( 'DB_PASSWORD', 'password_here' );
/** Database hostname */
define( 'DB_HOST', 'localhost' );
Now, log in to your hosting cPanel or control panel and navigate to MySQL Databases:
- Verify that the DB_NAME matches the exact database listed in your cPanel. Notice that cPanel prefixes databases with your account username (e.g.,
user_wpdb). - Verify that the DB_USER is assigned to that specific database with ALL PRIVILEGES.
- If you are unsure of the password, click “Change Password” for that MySQL user in cPanel, enter a new strong password, and paste the exact new password into the
DB_PASSWORDline inwp-config.php. - Check DB_HOST: While 90% of hosts use
localhost, some providers (like SiteGround, DreamHost, or custom AWS RDS instances) require an IP address or dedicated database hostname.
Step 3: Run the WordPress Automatic Database Repair Tool
If your credentials are correct but tables are corrupted, WordPress includes a built-in emergency repair utility. To activate it, open wp-config.php and add the following line right before the line that says /* That’s all, stop editing! Happy publishing. */:
define( 'WP_ALLOW_REPAIR', true );
Save the file and navigate to this URL in your web browser: https://yourdomain.com/wp-admin/maint/repair.php.
Click on “Repair and Optimize Database”. WordPress will systematically inspect all core tables (wp_posts, wp_options, wp_comments, etc.), fix corrupted indexes, and re-establish table integrity. Once finished, immediately remove the WP_ALLOW_REPAIR line from your wp-config.php to prevent unauthorized visitors from triggering repair scripts.
Step 4: Test Database Connection with a Standalone PHP Script
To determine whether the failure lies within WordPress or your hosting server’s MySQL service, create a temporary file named testdb.php in your root directory with this lightweight script:
<?php
$link = mysqli_connect('localhost', 'your_db_user', 'your_db_password');
if (!$link) {
die('Could not connect to MySQL server: ' . mysqli_connect_error());
}
echo 'Database Connection Successful!';
mysqli_close($link);
?>
Replace the placeholders with your credentials and visit https://yourdomain.com/testdb.php in your browser:
- If it says “Database Connection Successful”: Your MySQL server is running fine! The issue is within WordPress (such as table prefix mismatch in
wp-config.phplike$table_prefix = 'wp_';). - If it outputs a connection error: Your MySQL server is offline or your user lacks permission. Contact your web hosting technical support immediately and inform them that the MySQL service has crashed.
Always delete testdb.php from your server once testing is complete for security purposes.
Long-Term Prevention: Database Maintenance & Regular Backups
Database crashes frequently occur when WooCommerce sessions, transients, and post revisions swell your database size beyond server memory capacity. To prevent future downtime:
- Clean overhead and transient rows using our WordPress Database Optimization Guide.
- Always maintain automated offsite backups by following our Automated WordPress Backup & Recovery Guide.
- Ensure your hosting plan provides adequate RAM and database execution limits.

