Error Establishing a Database Connection in Localhost XAMPP
When you encounter the "Error establishing a database connection" while setting up WordPress on a local XAMPP server, it usually points to one of three specific failure modes: incorrect credentials in wp-config.php, a misconfigured MySQL port, or a missing database service. The error message itself is often misleading; it groups these distinct issues together without differentiating between a wrong password and an unreachable host.
The standard advice found online often suggests adding define('WP_ALLOW_REPAIR', true); to the top of your wp-config.php file. While this constant was historically used for database maintenance tasks in older WordPress versions, it will not resolve connection errors. In fact, attempting to access repair tools before establishing a valid connection will simply result in another error or an HTTP 500 response, wasting time without addressing the root cause.
The first thing to verify is the port configuration within XAMPP. By default, XAMPP installs MySQL (or MariaDB) on port 3306. However, WordPress's default wp-config.php generated by the installer often assumes a connection string of localhost:3306 but might be hardcoded to expect a different port if your environment was modified previously. More commonly, the issue arises because MySQL is not actually running when you try to connect.
To resolve this, open the XAMPP Control Panel and ensure that both Apache and MySQL are started. The status indicator next to MySQL should be green. If it is red or says "not responding," click Start. Once the service is active, try accessing your WordPress site again. If the error persists, proceed to inspect the wp-config.php file manually.
Locate the database credentials defined in this file:
define('DB_NAME', 'wordpress');
define('DB_USER', 'root');
define('DB_PASSWORD', ''); // Often empty by default in XAMPP
define('DB_HOST', 'localhost');
In a standard XAMPP installation, the database user is root, and the password is typically empty. However, if you have manually created a new database or changed the MySQL root password via the phpMyAdmin interface, ensure these values match exactly. Even a single extra space in the password string will cause a connection failure.
Another frequent pitfall involves the DB_HOST variable. While localhost works for many setups, some configurations require 127.0.0.1. If you are using a socket-based connection rather than TCP/IP, MySQL might reject connections initiated via the hostname localhost if the socket file path is misconfigured in your my.ini file.
If you have created the database but not selected it before running WordPress installation scripts, ensure you have run the SQL commands to create the specific database name defined in your config file. If the database does not exist, MySQL will return a generic connection error even if the credentials are correct.
Finally, check for port conflicts. If another service is occupying port 3306, XAMPP's MySQL instance will fail to start silently or throw an error upon startup. You can verify this by attempting to connect to the database using a raw SQL client like HeidiSQL or Navicat before even touching WordPress. If you cannot connect from outside WordPress, the issue is definitely within the MySQL service configuration, not the WordPress application logic.