The error “Error establishing database connection” typically means that your application, likely a WordPress site or similar CMS, is unable to connect to the database. Here are the common reasons for this issue and steps to resolve it:
1.Database Configuration Issues
- Check Database Credentials : Ensure the database name, username, password, and host specified in your application’s configuration file (e.g., wp-config.php for WordPress) are correct.
- Access wp-config.php:
- Look for these lines:
phpdefine('DB_NAME', 'database_name');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost'); - Confirm the values match your database server’s setup.
- Look for these lines:
2. Database Server Issues
- Is the Database Server Running?
- If you’re using a local server (like XAMPP or WAMP), ensure MySQL is running.For a remote host, check with your hosting provider for server issues.
- Check Database Host:
- Common values for DB_HOST are localhost or 127.0.0.1.
- Some hosts use non-default configurations, such as db.yourdomain.com.
3. Corrupted Database
- Repair the Database:
- Add this to your wp-config.php temporarily:
php
define('WP_ALLOW_REPAIR', true); - Visit: http://yourdomain.com/wp-admin/maint/repair.php to repair or optimize the database.
- Remove the line from wp-config.php after repairing.
4. Resource Limits
- Check for Resource Overload:
- Shared hosting plans can hit resource limits (RAM, CPU, or concurrent connections).
- Upgrade your hosting plan or contact support for assistance.
5. File or Hosting Configuration
- Permissions Issues:
- Ensure the wp-config.php file and other relevant files have the correct permissions (typically 644 for files and 755 for folders).
- Hosting Changes:
- If your host recently changed server configurations, you might need to adjust your settings.
6. Debugging the Issue
- Enable WordPress Debug Mode:
- Add this to your wp-config.php:
php
define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', false); - Check the wp-content/debug.log file for detailed error messages.
7. Hosting Provider Assistance
- If none of the above works, contact your hosting provider. They can check server logs, confirm database availability, and assist with troubleshooting.
