Access Denied for User
The database rejected the connection because the username or password is incorrect.
Explanation
Access Denied for User is a MySQL/MariaDB error indicating that the credentials provided for the database connection are incorrect. The error typically includes the username and the host from which the connection was attempted. This can happen due to wrong password, user doesn't exist, user doesn't have permission from the connecting host, or the user account is locked. In PostgreSQL, this manifests as "password authentication failed" or "role does not exist".
Common Causes
- Wrong username or password
- User not allowed from connecting host
- User account locked or expired
- User lacks required privileges
- Extra whitespace in credentials
Solution
Verify the database username and password in your .env file match what's configured in the database server. Check if the user has the correct permissions: SHOW GRANTS FOR 'username'@'host'. Ensure the user is allowed to connect from the host your application is running on (MySQL uses host-based access control). Reset the password if needed: ALTER USER 'username'@'%' IDENTIFIED BY 'newpassword'. Check for extra whitespace in environment variables. For PostgreSQL, check pg_hba.conf for authentication configuration.
Code Example
// Laravel .env - verify credentials
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myapp
DB_USERNAME=myuser // check this
DB_PASSWORD=mypassword // and this (no extra spaces!)
// MySQL: check user permissions
// mysql -u root -p
SHOW GRANTS FOR 'myuser'@'localhost';
// Create user with proper permissions
CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON myapp.* TO 'myuser'@'%';
FLUSH PRIVILEGES;
// PostgreSQL: check pg_hba.conf
// Trust local connections, password for remote
// host myapp myuser 127.0.0.1/32 md5
// Reset MySQL password
ALTER USER 'myuser'@'%' IDENTIFIED BY 'newpassword';
FLUSH PRIVILEGES;
Error Information
Language
php
Difficulty
Beginner
Views
15
Related Errors
ECONNREFUSED
Database
ECONNREFUSED
The network connection was refused by the target server.
Database
Deadlock Found
Two or more database transactions are blocking each other, creating a circular wait.
500
Laravel
Laravel 500 Internal Server Error
A generic server-side error indicating something went wrong during request processing.
404
Laravel
NotFoundHttpException
The requested URL does not match any defined route.