The "Error establishing a database connection" message appears when your website cannot connect to its MySQL database. This is one of the most common website errors and can be caused by several things — from a brief server hiccup to incorrect configuration.
Symptoms
- Your website displays: "Error establishing a database connection"
- The WordPress admin panel (
/wp-admin) shows the same error or a "database repair" message - All pages on the site are affected, not just specific ones
- The error may appear suddenly on a site that was previously working fine
Causes
Temporary MySQL Server Interruption (Most Common)
If the error appears suddenly across all websites on your hosting account, the MySQL server is likely experiencing a brief interruption. This can happen during:
- Routine server maintenance
- High load on the database server
- Automatic MySQL service restarts
Incorrect Database Credentials
Your website's configuration file contains the wrong database name, username, password, or host. This often happens after:
- Changing the database user's password in cPanel without updating the config file
- Migrating the site from another server
- Restoring a backup from a different hosting account
Database User Not Assigned
The database user exists but is not assigned to the database (or privileges were removed).
Database Deleted or Renamed
The database referenced in the config file no longer exists in cPanel.
Disk Quota Exceeded
If your hosting disk space is full, MySQL cannot write temporary files and may refuse connections. See Fix: Disk Quota Exceeded.
Corrupted Database Tables
Database tables may become corrupted due to an interrupted write operation, server crash, or disk error.
Solution
Step 1: Wait and Refresh
If the error just appeared out of nowhere:
- Wait 2–5 minutes
- Refresh the page
- If the site loads, it was a temporary MySQL interruption — no further action needed
Tip: Check if other websites on your hosting account are also affected. If all sites show the error simultaneously, it's almost certainly a temporary server-side interruption.
Step 2: Verify Database Credentials
If the error persists, check that your configuration file has the correct database details.
For WordPress
- Log in to cPanel → File Manager → navigate to
/publichtml/ - Open
wp-config.php(right-click → Edit) - Find these four lines:
define('DBNAME', 'cpaneluserdbname');
define('DBUSER', 'cpaneluserdbuser');
define('DBPASSWORD', 'yourpassword');
define('DBHOST', 'localhost');
- Verify each value against cPanel:
localhost on Cynet hosting
- If you changed the password, update
wp-config.phpwith the new password and save
For Other Applications (Laravel, Joomla, Custom PHP)
Check the database connection settings in your application's config file:
- Laravel:
.envfile (DBDATABASE,DBUSERNAME,DBPASSWORD,DBHOST) - Joomla:
configuration.php($db,$user,$password,$host) - Custom PHP: Wherever your database connection is defined
Step 3: Check Database User Privileges
- Go to cPanel → MySQL Databases
- Scroll to Current Databases
- Confirm your database user is listed under Privileged Users for that database
- If not, scroll to Add User To Database → select the user and database → click Add → check All Privileges → click Make Changes
Step 4: Repair the Database
If credentials are correct but the error persists, the database may be corrupted.
WordPress Database Repair
- Add this line to
wp-config.php(before "That's all, stop editing!"):
define('WPALLOWREPAIR', true);
- Visit
https://yourdomain.com/wp-admin/maint/repair.php - Click Repair Database or Repair and Optimize Database
- Once complete, remove the line from
wp-config.php(leaving it is a security risk)
phpMyAdmin Repair
- Go to cPanel → phpMyAdmin
- Select your database from the left sidebar
- Click Check All to select all tables
- From the dropdown at the bottom, select Repair table
- Click Go
Step 5: Check Disk Space
- Go to cPanel → Disk Usage
- If usage is at or near 100%, you need to free up space
- See Fix: Disk Quota Exceeded for detailed instructions
Step 6: Contact Support
If none of the above steps resolve the issue:
- The MySQL server may require a restart on the server side
- There may be a broader server issue
- Your domain name
- When the error started
- Whether all sites or just one site is affected
- Steps you've already tried
Prevention
- Don't edit database credentials unless necessary — if you change a database password in cPanel, immediately update your config file
- Keep regular backups — Use Softaculous, WP Toolkit, or cPanel Backup to maintain recent backups
- Monitor disk space — Keep usage below 90% to avoid write failures
- Use a caching plugin — Reduces database load and makes your site more resilient to brief MySQL interruptions
- Optimise your database — Periodically clean up post revisions, spam comments, and transient data using a plugin like WP-Optimize