Troubleshooting ‘Error establishing a database connection’ in WordPress

Solution 1:

The “Error establishing a database connection” essentially means the credentials in your wp-config.php file don’t match your database details. These credentials are different from your server login details.

To fix it:

1. Backup and rename your current wp-config.php
Rename it to something like wp-config.php.old.

2. Create a new wp-config.php with the following template:

<?php
// ** MySQL settings - get these from your web host ** //
define('DB_NAME', 'database_name_here');
define('DB_USER', 'username_here');
define('DB_PASSWORD', 'password_here');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

/**#@+
 * Authentication Unique Keys and Salts.
 * Generate at: https://api.wordpress.org/secret-key/1.1/salt/
 */
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');

/** WordPress Database Table prefix */
$table_prefix = 'wp_';

/** WordPress debugging mode */
define('WP_DEBUG', false);

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

3. Determine the correct database credentials:

  • Access your database via phpMyAdmin or a similar tool.
  • Create a new database user and assign a strong password.
  • Make sure the user has all privileges for the database.
  • Use the database name, username, password, and host (usually localhost) in your new wp-config.php.

Note: On some servers, the host may differ; if localhost doesn’t work, check with your hosting provider.

Once you have the correct credentials in place, WordPress should be able to connect to the database and your site will work again.

Solution 2:

Try disabling your plugins to see if they are causing the issue:

  • Access your site’s files via a file manager provided by your hosting control panel (all web hosts offer this).
  • Navigate to the wp-content/plugins directory.
  • Rename the entire plugins folder (e.g., to plugins_disabled) or just the folder of the plugin you suspect.
  • Check if your site loads correctly.

Renaming the folder effectively deactivates the plugins. If the site works after this, you can reactivate the plugins one by one to identify the problematic one.

Solution 3:

If you cannot access phpMyAdmin, it may be because your server has reached its memory or resource limits.

Ways to resolve it:

  • Increase the PHP memory limit in your server configuration (php.ini or .htaccess).
  • Upgrade your hosting plan to provide more resources.

In most cases, simply increasing server resources or moving to a higher-tier plan resolves the issue.