I’m experiencing an issue with my local WordPress installation on Ubuntu 22.04. I have WordPress, MySQL, Apache2, and PHP installed and configured. I recently created a new MySQL user (youssef) and granted it privileges for my WordPress database. However, when I update the WordPress configuration file (wp-config.php) to use this new user and set the database host to localhost, I encounter a “Failed to establish a database connection” error.
The connection works fine when I change the database host to 127.0.0.1. Here’s a summary of the issue:
- Current Setup:
- MySQL Root User: Works with localhost and 127.0.0.1.
- MySQL Custom User (youssef): Set up with host localhost.
- WordPress Configuration (wp-config.php):
define('DB_NAME', 'your_database_name');
define('DB_USER', 'youssef');
define('DB_PASSWORD', 'youssef');
define('DB_HOST', '127.0.0.1');
- Issue:
- WordPress fails to connect to the database with the youssef user when DB_HOST is set to localhost.
- The connection is successful when DB_HOST is set to 127.0.0.1.
- Testing:
- I can connect to MySQL using both localhost and 127.0.0.1 from the command line with the youssef user.
- Other PHP and Laravel projects on the same server can connect to MySQL using both localhost and 127.0.0.1 with the youssef user.
- Configuration Checks:
- The MySQL user youssef has been granted appropriate privileges.
- The MySQL server configuration (my.cnf) specifies the socket and bind-address settings.
- Apache and PHP configurations are standard.
- Question:1-Why does WordPress fail to connect to the database when using localhost but succeed with 127.0.0.1 and with the root user using localhost? 2-What steps can I take to resolve this issue?
Any insights or solutions would be greatly appreciated!