Fixing Database Connection Errors
Fixing Database Connection Errors
The “Error establishing a database connection” message (common in WordPress) or similar database connectivity errors indicate your website cannot communicate with its MySQL database. Here’s how to diagnose and fix the issue.
Common Causes
- Incorrect database credentials in configuration files
- Corrupted database or tables
- MySQL server is down or overloaded
- Exceeded hosting resource limits
- Wrong database host setting
Step 1: Verify Database Credentials
The most common cause is incorrect credentials. Check your website’s configuration file:
WordPress (wp-config.php):
define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_password');
define('DB_HOST', 'localhost');
Verify in cPanel:
- Go to Databases > MySQL Databases.
- Confirm that:
- The database name exists (remember the cPanel prefix, e.g.,
cpuser_dbname) - The database user exists
- The user is assigned to the database with appropriate privileges
- The database name exists (remember the cPanel prefix, e.g.,
Test the credentials by logging into phpMyAdmin with the same username and password.
Step 2: Check if MySQL Is Running
If all credentials are correct, the MySQL server may be down:
Via SSH:
mysqladmin -u your_user -p status
Via cPanel:
- Try opening phpMyAdmin. If it can’t connect, MySQL may be down.
- Contact 10Corp support to check the MySQL service status.
Step 3: Repair a Corrupted Database
WordPress built-in repair:
- Add this line to
wp-config.php:define('WP_ALLOW_REPAIR', true); - Visit
https://yourdomain.com/wp-admin/maint/repair.php - Click Repair Database (or Repair and Optimize Database).
- Remove the line from
wp-config.phpafter repair (it’s publicly accessible).
Via phpMyAdmin:
- Open phpMyAdmin and select the database.
- Select all tables (Check All).
- From the dropdown, choose Repair table.
Via SSH:
mysqlcheck -u your_user -p --repair your_database_name
Step 4: Check Database Host
Most hosting accounts use localhost as the database host. However, some configurations may require:
- A specific IP address
- A socket path (e.g.,
/var/lib/mysql/mysql.sock) - A port number (e.g.,
localhost:3306)
Check your hosting welcome email or contact 10Corp support for the correct database host.
Step 5: Check Resource Limits
On shared hosting, database connection errors can occur when:
- Too many simultaneous connections are open
- Memory limits are exceeded
- CPU usage is maxed out
Solutions:
- Optimize your database: Remove unused tables, clean post revisions, delete spam comments.
- Reduce database queries: Use caching to minimize database load.
- Close idle connections: Ensure your application properly closes database connections.
- Upgrade your hosting plan if you’ve outgrown shared hosting.
Step 6: Check for WordPress-Specific Issues
- Disable plugins: Rename
wp-content/pluginstoplugins_disabledvia FTP/File Manager. If the error resolves, a plugin is the cause. - Increase max connections in wp-config.php: This doesn’t change the server limit but can be relevant if you’re customizing database behavior.
- Check table prefix: Ensure
$table_prefixinwp-config.phpmatches your database tables.
Prevention
- Regular backups — Back up your database frequently.
- Keep software updated — Update your CMS, plugins, and themes.
- Use caching — Reduce the number of database queries.
- Monitor resources — Keep an eye on hosting resource usage in cPanel.
For persistent database connection issues, contact 10Corp support.