How to troubleshoot WordPress database connection errors?
Answer
The "Error establishing a database connection" in WordPress occurs when your site cannot communicate with its MySQL database, rendering it inaccessible. This issue typically stems from incorrect database credentials, server problems, corrupted files, or resource limitations. The most common first step is verifying the wp-config.php file for accurate database name, username, password, and host details, as mismatched credentials account for 60%+ of cases [2][4]. Server-side factors like MySQL downtime, traffic spikes, or insufficient hosting resources also frequently trigger this error, particularly on shared hosting plans [1][7].
Key immediate actions to resolve the error:
- Check wp-config.php for correct database credentials (name, user, password, host) [2][3]
- Verify database server status through your hosting provider’s control panel [1][5]
- Repair corrupted databases using phpMyAdmin or WordPress’s built-in repair tool [4][9]
- Disable plugins/themes via FTP if the error persists after credential verification [5][7]
Systematic Troubleshooting for WordPress Database Connection Errors
Verifying and Correcting Database Credentials
The wp-config.php file contains four critical database connection parameters that must match your hosting provider’s settings: database name (DB_NAME), username (DB_USER), password (DB_PASSWORD), and host (DB_HOST). Incorrect values in any of these fields will trigger the connection error. Begin by accessing this file via FTP/cPanel File Manager or your hosting provider’s file editor [2][10]. The file is located in your WordPress root directory (same level as /wp-admin/ and /wp-content/).
Key steps for credential verification:
- Locate the correct credentials: Log in to your hosting account (e.g., cPanel, Plesk, or custom dashboards like Kinsta/WP Engine) and navigate to the MySQL Databases section. Note the exact database name, username, and assigned host (often
localhost, but some hosts use custom addresses likedb123.hostprovider.com:3306) [1][6]. - Compare with wp-config.php: Open the file and verify these lines match your hosting details:
define('DBNAME', 'yourdatabasename');
define('DB
USER', 'yourdatabaseuser'); define('DBPASSWORD', 'yourpassword'); define('DB_HOST', 'localhost'); // Or custom host like db123.host.com:3306 Note: Passwords are case-sensitive, and extra spaces or quotes can break the connection [3].
- Test connectivity: If credentials appear correct but the error persists, manually test the connection using a tool like phpMyAdmin or the MySQL command line:
mysql -u yourdatabaseuser -p -h yourdbhost
Enter the password when prompted. If this fails, the credentials or host are likely incorrect [8].
- Reset credentials if needed: Some hosts (e.g., Virtualmin) require explicitly setting a password for the database user before WordPress can connect. Use the hosting control panel to reset the password, then update
wp-config.php[6].
Common credential-related pitfalls:
- Localhost vs. custom hosts: Shared hosting often uses
localhost, but managed hosts (e.g., Kinsta, WP Engine) may require a different host address [4]. - Special characters in passwords: Passwords with quotes (
'), backslashes (\), or semicolons (;) can break the PHP syntax inwp-config.php. Enclose such passwords in single quotes and escape special characters [3]. - Case sensitivity: Database names and usernames are case-sensitive on Linux servers (common in hosting environments) [2].
Diagnosing Server and Database Issues
When credentials are confirmed correct but the error persists, the problem typically lies with the database server or WordPress installation files. Start by checking if the MySQL server is operational, as downtime or crashes will sever all connections. Most hosting providers offer a server status page (e.g., cPanel’s "MySQL Server Status" or Kinsta’s "System Status") where you can verify uptime [1][4].
Steps to diagnose server/database problems:
- Check MySQL server status:
- Log in to your hosting control panel and look for MySQL/MariaDB service status. If it’s stopped, restart it (options like "Restart MySQL" are usually available) [5].
- For local installations (e.g., XAMPP/MAMP), ensure the MySQL service is running via the control panel [3].
- Use command line (if you have SSH access):
service mysql status Linux
or check via phpMyAdmin—if you can’t log in, the server is likely down.
- Review error logs: Enable WordPress debugging by adding this to
wp-config.php:
define('WPDEBUG', true);
define('WP
DEBUG_LOG', true); // Logs errors to /wp-content/debug.log Common log entries for this error include:
MySQL server has gone away(server crash or timeout) [8]Access denied for user(permission issues) [6]Too many connections(server overload) [7]- Test database connectivity: Create a simple PHP file (e.g.,
test-db.php) in your WordPress root with:
$link = mysqliconnect('yourdbhost', 'yourdbuser', 'yourdbpassword'); if (!$link) { die('Connection failed: ' . mysqliconnect_error()); } echo 'Connected successfully'; ?> Access this file via browser. If it fails, the issue is server-side [2].
- Check for corrupted tables: Corrupted database tables can prevent connections. Use phpMyAdmin to: 1. Select your WordPress database. 2. Click Check All tables, then choose Repair Table from the dropdown [4][9].
Alternatively, add this to wp-config.php to trigger WordPress’s auto-repair:
define('WPALLOWREPAIR', true);
Then visit yourdomain.com/wp-admin/maint/repair.php.
Server-related solutions:
- Increase PHP memory limit: Add this to
wp-config.phpif logs show memory exhaustion:
define('WPMEMORYLIMIT', '256M');
- Disable plugins/themes: Rename the
/wp-content/plugins/folder to/plugins_old/via FTP to disable all plugins. If the site loads, a plugin is causing the issue. Reactivate them one by one to identify the culprit [5][7]. - Restore from backup: If the database is severely corrupted, restore a clean backup via your hosting provider’s backup tool or phpMyAdmin [4].
- Contact hosting support: If all else fails, provide your hosting provider with:
- The exact error message (from logs or screen).
- Steps you’ve already taken (e.g., "verified credentials, repaired tables").
- Any recent changes (e.g., plugin updates, server migrations) [1][5].
Sources & References
wordpress.org
forum.virtualmin.com
developer.wordpress.org
Discussions
Sign in to join the discussion and share your thoughts
Sign InFAQ-specific discussions coming soon...