Access denied for user 'homestead'@'localhost' (using password: YES)

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Troubleshooting "Access denied for user 'homestead'@'localhost'" Issues in Laravel Development Environments Body: In this comprehensive blog post, we will explore common issues leading to access denied errors when working with the Homestead environment and Laravel applications. We will cover potential causes, practical solutions, and best practices for maintaining a stable local development environment. 1. Mismatched Configuration Settings: One of the most frequent causes of this error is having contradicting configuration settings between your .env file and the app/config/database.php file. To troubleshoot this issue, ensure that both files contain identical information regarding the database host, username, password, and other relevant credentials. Here's an example of a correctly configured .env file: APP_ENV=local APP_DEBUG=true APP_KEY=***** DB_HOST=127.0.0.1 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret And the corresponding configuration in app/config/database.php: 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'homestead'), 'username' => env('DB_USERNAME', 'homestead'), 'password' => env('DB_PASSWORD', 'secret'), 'unix_socket' => '/tmp/mysql.sock', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ] 2. Localhost vs. 127.0.0.1: Occasionally, running php artisan migrate will return an access denied error if you set localhost as the database host in your configuration files instead of using the correct IP address, which is 127.0.0.1. This issue can lead to a disconnect between the Laravel application and MySQL server, leading to authentication problems. Be sure to replace 'localhost' with '127.0.0.1' in your configuration files if you encounter this issue. 3. Environment Checking: When dealing with Laravel migrations, it's always a good practice to specify the environment you want to use by running php artisan migrate --env=local from the command line. This ensures that the correct configuration settings are used and minimizes the risk of access denied errors stemming from mismatched database credentials. 4. Verify MySQL Status: To confirm if your MySQL server is running correctly, try using the mysqladmin command to check the status. For example: mysqladmin -u homestead -p status Enter password: secret Uptime: 21281 Threads: 3 Questions: 274 Slow queries: 0 Opens: 327 Flush tables: 1 Open tables: 80 Queries per second avg: 0.012 This output confirms that your MySQL server is operational and the service has been up for over 21,000 seconds with a steady number of running threads. If you can't connect to MySQL using this command or get an error, it might be necessary to troubleshoot your database connection further. 5. Verify UNIX Socket Access: In some cases, the access denied error can be caused by permission issues on the UNIX socket file (usually located at /tmp/mysql.sock). To ensure that your Laravel application has proper access to this socket, try changing its permissions or rebuilding the socket with a fresh install of MySQL: - Change UNIX socket permissions: sudo chown www-data mysql.sock sudo chmod 660 mysql.sock - Rebuilding the UNIX socket: 1. Stop MySQL service: sudo systemctl stop mysql.service sudo mysqld --skip-grant-tables --init_file=/tmp/mysql_init & 2. Flush privileges and exit MySQL connection: ALTER USER root@localhost WITH PASSWORD=''; FLUSH PRIVILEGES; exit; Then, restart the MySQL service with your new root password. In conclusion, troubleshooting "Access denied for user 'homestead'@'localhost'" issues can often involve a few simple yet crucial steps like ensuring correct configuration settings, running the MySQL server, and checking UNIX socket access. By following these guidelines and keeping your local development environment stable, you are more likely to avoid such errors and focus on developing your Laravel application effectively.