Laravel error with php artisan migrate
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Solving the Dreaded Access Denied Error: Migrating Laravel on AWS
As a senior developer, I’ve seen countless developers face frustrating roadblocks when deploying or setting up new projects, especially when bridging local development environments with remote servers like AWS. One of the most common stumbling blocks for newcomers is database connectivity errors. Today, we are diving deep into a very specific and common issue: encountering the [PDOException] SQLSTATE[28000] [1045] Access denied for user 'homestead'@'localhost' error when running php artisan migrate in a Laravel application deployed on an Ubuntu server.
This post will walk you through exactly why this happens and provide a robust, step-by-step solution that bypasses the confusion around local setup versus remote server permissions.
Understanding the Root Cause: Database Permissions
The error message Access denied for user 'homestead'@'localhost' is not actually a Laravel or PHP error; it is a communication failure originating from the underlying MySQL database server itself.
When Laravel attempts to run migrations, it uses the credentials defined in your .env file (e.g., DB_USERNAME=homestead, DB_PASSWORD=secret) to connect to the database. The "Access denied" message means that while the connection was established, the MySQL server rejected the request because the user 'homestead' does not have the necessary privileges to perform the requested action (in this case, creating tables or executing migration commands) on the specified database.
This discrepancy often arises because development setups (like Laravel Homestead) handle these permissions automatically, but when you move to a fresh AWS server running raw Ubuntu and MySQL, you must manually configure these security settings.
The Practical Fix: Configuring MySQL Access for Laravel
To resolve this, we need to manually log into the MySQL server and ensure that the database user specified in your .env file has full control over the intended database. This process involves setting up the database and granting permissions correctly.
Here is the sequence of commands you need to execute on your AWS Ubuntu machine:
Step 1: Accessing the MySQL Server
First, log into your MySQL server as the root user:
mysql -u root -p
Enter the password for the root user when prompted.
Step 2: Creating the Database
If the database specified in your .env file does not exist yet (e.g., homestead), you must create it first:
CREATE DATABASE homestead;
Step 3: Granting Permissions to the User
Next, you need to ensure the user Laravel is trying to connect with (homestead) has full rights over that specific database. You will use the GRANT command for this purpose:
-- Replace 'homestead' with your actual DB_USERNAME and 'homestead@localhost'
-- with the host Laravel is connecting from (often localhost or '%' for flexibility)
GRANT ALL PRIVILEGES ON homestead.* TO 'homestead'@'localhost';
-- Apply the changes
FLUSH PRIVILEGES;
By executing these steps, you are explicitly telling MySQL that the user 'homestead' is authorized to perform all necessary operations within the homestead database. This resolves the "Access denied" error that prevents Laravel from running migrations.
Best Practices for Laravel Deployment
While manually configuring permissions solves the immediate problem, it’s crucial to adopt better practices for managing environment variables and deployments. When working with Laravel, always treat your .env file as sensitive information. Never commit it to public repositories.
For more advanced insights into structuring your application logic, especially around database interactions and Eloquent models, I highly recommend exploring the official documentation at laravelcompany.com. Understanding how frameworks manage these connections is key to writing secure and maintainable code.
Conclusion
The Access denied error during php artisan migrate is a classic permissions issue that often trips up developers transitioning from local setups to remote hosting environments. The solution lies not in the Laravel code itself, but in correctly configuring the underlying database server security. By understanding how MySQL permissions work and executing the necessary GRANT commands, you can successfully connect your Laravel application to its database and proceed with migrations. Happy coding!