Why Laravel Still Tries to Login as 'homestead' After Changing Credentials
The error SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' is a classic symptom of configuration drift in the Laravel ecosystem. It typically surfaces when a developer attempts to migrate from Homestead (the official Vagrant box) to a manual local setup, assuming that updating the .env file is sufficient to sever ties with the virtual machine environment.
In your specific case, you have correctly updated config/database.php to reference the root user and have adjusted your .env file to point to localhost, root, and an empty password. However, the application continues to attempt a connection using the credentials associated with the Homestead VM: user 'homestead'@'localhost'.
This discrepancy usually indicates that Laravel's configuration cache is holding onto the old state, or that you are running a command that forces the use of the cached config before your new environment variables are loaded. When Laravel starts, it reads the .env file and merges it with config/database.php. If the application has previously been built (e.g., via php artisan config:cache), those compiled files take precedence over runtime environment variables.
Clearing the Configuration Cache
The most immediate step to resolve this is to clear Laravel's cache. The framework caches configuration, routes, and events to improve performance, but in a development environment where credentials change frequently, this cache becomes an obstacle.
Run the following command in your project directory:
php artisan config:clear
php artisan cache:clear
php artisan route:clear
After executing these commands, restart your PHP server (e.g., php artisan serve) and attempt to access the application again. This ensures that Laravel re-reads the .env file fresh and applies the DB_USERNAME=root setting immediately.
Verifying MySQL User Permissions
If clearing the cache does not resolve the issue, the problem likely lies within the MySQL user setup itself. The error message explicitly mentions homestead@localhost, which suggests that while your config file says root, the connection string being used might still be defaulting to homestead in some edge cases, or the root user simply lacks permission to connect from localhost with an empty password.
First, ensure that the root user exists and is permitted to access the database without a password (or with the correct one). Connect to MySQL directly using your system's root credentials:
sudo mysql -u root -p
Once inside the MySQL prompt, check the privileges for the root user:
SELECT user, host FROM mysql.user WHERE user='root';
If root does not have a password set but is restricted, you may need to flush privileges or explicitly grant access. Run the following command to ensure root can connect from localhost with no password:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Note: The syntax for setting a blank password varies by MySQL version. On MySQL 8.0+, you might need to use SET PASSWORD FOR 'root'@'localhost' = ''; followed by ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ''; FLUSH PRIVILEGES;.
Checking for Hidden Environment Variables
Sometimes, the .env file is correct, but other environment variables interfere with the connection logic. Specifically, ensure that no other files in your project directory are defining DB_USERNAME or DB_PASSWORD. If you have a .env.example file checked into version control by mistake, make sure it is ignored via .gitignore, although this usually doesn't affect runtime unless explicitly sourced elsewhere.
Additionally, verify that you are not running the application through a container (like Docker) that might be mounting your project but preserving old environment variables from the host system. If you are using Docker Compose, ensure your docker-compose.yml matches your .env file exactly.
Conclusion
The persistent error stems from either cached configuration or an actual mismatch between the MySQL user database and your application's expectations. By flushing Laravel's config cache and ensuring the root user has explicit permissions for localhost access with an empty password, you should be able to transition fully away from Homestead credentials without further issues.
