Skip to content

'java.sql.SQLException: Access denied for user ''root''@''localhost'' (using

A java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) error typically indicates that the MySQL server is unable to authenticate the user with the provided username and password. This can be caused by a number of factors, including:

  1. Incorrect username or password: Make sure that you are using the correct username and password for the MySQL server.
  2. User account is not granted access to the database: If the user account does not have the necessary permissions to access the database, you will need to grant the appropriate privileges to the user.
  3. User account is not active: If the user account has been disabled or expired, you will need to re-enable or reset the account.
  4. MySQL server is not running: Make sure that the MySQL server is running and reachable from your Java application.
  5. Incorrect MySQL server hostname or port: Make sure that you are using the correct hostname and port for the MySQL server in your Java application.

Troubleshooting Steps

1. Verify JDBC Connection String Ensure your JDBC URL matches the correct host, port, and database name:

java
String url = "jdbc:mysql://localhost:3306/your_database_name";
String username = "root";
String password = "your_password";

2. Verify MySQL Server Connectivity Test the connection directly via the MySQL command line to isolate Java-specific issues:

bash
mysql -u root -p -h localhost

If this fails, the issue lies with the MySQL server configuration or network, not your Java code.

3. Reset or Verify Root Password If you suspect an incorrect password, reset it via the MySQL command line:

sql
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_secure_password';

4. Grant Database Privileges Ensure the root user has full access to the target database:

sql
GRANT ALL PRIVILEGES ON your_database_name.* TO 'root'@'localhost';

After applying these changes, restart your Java application and verify the connection.

Dual-run preview — compare with live Symfony routes.