PromptHub
Database Beginner 15 views

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