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.