php artisan migrate - SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'localhost'
When you run php artisan migrate and encounter the error SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'localhost' (using password: NO), the root cause is almost always a misconfiguration in your database credentials or MySQL user privileges. This specific error indicates that the application cannot authenticate with the database server, and critically, it notes that no password was provided (using password: NO).
Looking at the .env file configuration you provided, the issue is immediately apparent:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=
The DB_PASSWORD field is empty. If you do not enter a password here, the application attempts to connect using the MySQL default behavior for that specific user account. In many local development setups, users are created without passwords, but Laravel's default configuration often expects a password to be set. When the connection fails with "Access denied," it means the credentials in the environment file do not match a valid user defined on the MySQL server.
You mentioned using Ubuntu 16.04 instead of macOS. While this is an operating system difference, the solution remains consistent across platforms: you must ensure the database user exists and has the correct password set. The error message explicitly states using password: NO, which confirms that even if a user named laravel exists in your MySQL instance, it likely has no password associated with it, or the application is failing to pass one.
To resolve this, you need to establish a secure connection by setting a password. You can do this directly via the MySQL command line:
Open your terminal and access the MySQL shell using the root user (or whichever user you have configured as superuser):
mysql -u root -p(Note: If your root user has a password, enter it when prompted. If you previously set up root without a password on Ubuntu 16.04, you might need to secure it first depending on your security settings.)
Once inside the MySQL prompt, execute the following SQL commands to create or update the
laraveluser and grant permissions:CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'your_secure_password'; GRANT ALL PRIVILEGES ON *.* TO 'laravel'@'localhost'; FLUSH PRIVILEGES;Replace
your_secure_passwordwith a strong password you will use for your project. The commandGRANT ALL PRIVILEGESensures the user can perform migrations, insert data, and manage the schema as required by the application.After running these commands, update your
.envfile to include the password:DB_PASSWORD=your_secure_password
It is important to note that if you are connecting from a remote environment (like a shared host), the user might need to be created as 'laravel'@'%' instead of 'laravel'@'localhost', but since your DB_HOST is set to 127.0.0.1, local authentication is the correct path here.
Another potential pitfall involves MySQL version differences or security plugins. On older versions of MySQL (pre-5.7), the default authentication plugin was mysql_native_password. Newer versions might use caching_sha2_password by default for new users. If you upgrade your MySQL instance and previously created the user without specifying a plugin, Laravel might fail to connect if it expects the older hash format. In that case, you can force the correct plugin during creation:
CREATE USER 'laravel'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_secure_password';
Finally, ensure that the database laravel actually exists. The error log shows a query attempting to select from information_schema.tables. If the user has privileges but not access to the specific schema because it doesn't exist yet, you might need to create the database first:
mysql -u root -p -e "CREATE DATABASE laravel CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
By setting a password in the environment file and ensuring the MySQL user has matching credentials and appropriate privileges, the Access denied error will be resolved, allowing your migrations to run successfully.
