Back to Error Center

Access denied for user

Error Message

SQLSTATE[28000]: Access denied for user 'root'@'localhost'

Cause

The database credentials in .env don't match the actual database user's permissions, or the user doesn't exist.

Solution

Verify DB_USERNAME and DB_PASSWORD in .env. Grant the user proper permissions in MySQL. Reset the MySQL root password if needed.

Code Example

Fix
# Check MySQL user permissions
mysql -u root -p
SHOW GRANTS FOR 'root'@'localhost';

# Grant permissions
GRANT ALL PRIVILEGES ON laravel_app.* TO 'root'@'localhost';
FLUSH PRIVILEGES;

# .env
DB_USERNAME=root
DB_PASSWORD=your_password

Debugging Steps

  1. Verify DB_USERNAME and DB_PASSWORD in .env
  2. Test the credentials with a MySQL client
  3. Check the user's permissions on the database
  4. Ensure the user has the correct host permissions

Prevention

Use separate database users for different environments. Grant minimum required permissions. Store credentials securely outside version control.