php artisan migrate - SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'localhost'
This error message is indicating that the PHP script, "artisan," is attempting to run a database migration command, but the database server is denying access to the user "laravel" on the "localhost" location.
The SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'localhost' error occurs when the Artisan command attempts to run a database migration, but the database server denies access to the laravel user on localhost. This is a common error indicating that the database server is rejecting the connection.
This error can be caused by several factors:
- Incorrect login credentials (username or password) for the database.
- The
laraveluser lacks appropriate permissions; verify the user has the correct grant permissions. - The
laraveluser does not exist on the database server; verify the user exists. - Firewall or network restrictions; ensure the database server is not blocking connections (MySQL typically uses port
3306).
To resolve this, verify your database connection settings in the .env file at the root of your Laravel project:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=laravel
DB_PASSWORD=your_secure_password
After updating .env, clear the configuration cache:
php artisan config:clear
php artisan migrateIf credentials are correct but access is still denied, verify the user exists and has proper permissions via the MySQL CLI:
CREATE USER IF NOT EXISTS 'laravel'@'localhost' IDENTIFIED BY 'your_secure_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'laravel'@'localhost';
FLUSH PRIVILEGES;It's recommended to check the database server log files for more information about the error if the issue persists.