How to Fix "Error Establishing Database Connection" in WordPress
The "Error Establishing a Database Connection" message is one of the most dreaded errors in WordPress. It means your WordPress installation cannot communicate with the MySQL database that stores all your content, settings, users, and configuration. Your website displays a blank white page with just this error message, and the WordPress admin dashboard is equally inaccessible. Don't panic — in most cases, this is fixable within minutes.
What Causes This Error?
WordPress needs four pieces of information to connect to its database, all stored in the wp-config.php file:
- Database Name (
DB_NAME) - Database Username (
DB_USER) - Database Password (
DB_PASSWORD) - Database Host (
DB_HOST)
If any of these are incorrect, or if the MySQL server itself is having issues, WordPress cannot connect and displays this error.
Fix 1: Verify wp-config.php Credentials
The most common cause is incorrect database credentials in wp-config.php. This often happens after a site migration, server change, or if someone accidentally modified the file.
Open wp-config.php
Log into cPanel via billing.sakurahost.co.tz, open File Manager, navigate to your website's document root (usually public_html), and open wp-config.php for editing. Look for these lines:
Cross-Reference with cPanel
In cPanel, go to Databases > MySQL Databases. This page shows:
- All databases on your account (compare with
DB_NAME) - All database users (compare with
DB_USER) - Which users are assigned to which databases
The database name in cPanel includes your cPanel username prefix (e.g., cpaneluser_wordpress). Ensure the value in wp-config.php matches exactly, including the prefix.
Reset the Database Password if Needed
If you're not sure of the password, go to MySQL Databases in cPanel, find the database user, and click "Change Password". Set a new strong password, then update wp-config.php to match.
DB_HOST value should almost always be localhost on SakuraHost shared hosting. Do NOT change this to an IP address or remote hostname unless specifically instructed by our support team. Remote database connections require special configuration.
Fix 2: Ensure Database User Has Proper Privileges
Even if the credentials are correct, the database user must have the proper privileges assigned to the specific database.
In cPanel > MySQL Databases, scroll to the "Add User to Database" section.
Select the correct user and database from the dropdowns and click "Add".
On the privileges page, check "ALL PRIVILEGES" and click "Make Changes". WordPress requires full privileges including SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, and INDEX.
Fix 3: Check if MySQL Server Is Running
Sometimes the MySQL service on the server may be temporarily down due to high load, maintenance, or a crash. This affects all websites on the same server.
How to test: Try accessing phpMyAdmin from cPanel (Databases > phpMyAdmin). If phpMyAdmin loads and you can see your databases, MySQL is running and the issue is with your wp-config.php credentials. If phpMyAdmin also shows an error, the MySQL server itself may be down.
Fix 4: Repair a Corrupted Database
Database tables can become corrupted due to server crashes, abrupt shutdowns, or disk errors. WordPress has a built-in database repair tool.
Enable the Repair Tool
Add this line to your wp-config.php (before the "That's all, stop editing!" comment):
Run the Repair
Visit https://yourdomain.co.tz/wp-admin/maint/repair.php in your browser. You'll see two options:
- Repair Database — Fixes corrupted tables
- Repair and Optimize Database — Fixes corruption and optimizes tables for better performance (recommended)
Click "Repair and Optimize Database" and wait for the process to complete.
Remove the Repair Constant
After the repair is complete, immediately remove the WP_ALLOW_REPAIR line from wp-config.php. This page is accessible without authentication and should not remain enabled on a production site.
Alternatively, you can repair tables through phpMyAdmin. In cPanel > phpMyAdmin, select your WordPress database, check all tables, and choose "Repair table" from the dropdown at the bottom of the page.
Fix 5: Check for Exceeded Hosting Resource Limits
On shared hosting, your account has limits for concurrent MySQL connections, CPU usage, and memory. If your site receives a sudden traffic spike or runs a poorly optimized query, it may exhaust the allowed MySQL connections.
- Check Resource Usage in cPanel under Metrics
- Look at the MySQL section for connection limits and usage
- If you're consistently hitting limits, optimize your database queries or upgrade your hosting plan at sakurahost.co.tz
Fix 6: Restore from Backup
If none of the above fixes work and your database may have been severely corrupted or accidentally deleted, restoring from a backup is the safest option.
- In cPanel, go to Files > Backup Wizard or JetBackup (if available)
- Select "Restore" and choose "MySQL Databases"
- Select the most recent backup date before the error started
- Restore the database and test your website
Preventing Future Database Connection Errors
- Keep WordPress, themes, and plugins updated — Updates include database optimization and bug fixes
- Use a caching plugin — Reduces database queries and server load
- Optimize your database regularly — Monthly optimization prevents bloat and corruption
- Monitor resource usage — Set up email alerts in cPanel for resource limit warnings
- Never edit wp-config.php without a backup — Download a copy before making changes