Open links in new tab
  1. This error occurs when MySQL rejects the connection attempt due to incorrect credentials, missing privileges, or authentication plugin mismatches.

    Example:

    String url = "jdbc:mysql://localhost:3306/mydb";
    String user = "mckb_user";
    String password = "mypassword";

    Connection conn = DriverManager.getConnection(url, user, password);
    // java.sql.SQLException: Access denied for user 'mckb_user'@'localhost' (using password: YES)
    Copied!

    A common cause is that the MySQL user either does not exist, has the wrong password, or lacks privileges for the host localhost.

    To fix this, first verify that you can connect via the MySQL CLI:

    mysql -u mckb_user -p
    Copied!

    If this fails, create or update the user and grant privileges:

    CREATE USER 'mckb_user'@'localhost' IDENTIFIED BY 'mypassword';
    GRANT ALL PRIVILEGES ON mydb.* TO 'mckb_user'@'localhost';
    FLUSH PRIVILEGES;
    Copied!

    This ensures the user exists with the correct password and has access to the intended database.

    If you are using MySQL 8+, ensure the authentication plugin matches your JDBC driver capabilities. For example:

  1. java.sql.SQLException: Access denied for user …

    So I got something like java.sql.SQLException: Access denied for user …

    • Reviews: 6

      Code sample

      static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
          static final String DB_URL = "jdbc:mysql://localhost:3306/YOUR_DB_NAME";
          static final String USER = "root";
          static final String PASS = "YOUR_ROOT_PASSWORD";
        Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);...
    • How to Resolve java.sql.SQLException: Access Denied for User 'root ...

      Learn how to fix the java.sql.SQLException: Access denied for user 'root'@'localhost' error with step-by-step solutions and code snippets.

    • How to Fix Access Denied for User 'root'@'localhost' in MySQL

      Apr 8, 2024 · In MySQL, encountering the "Access Denied for User 'root'@'localhost' " error typically occurs when there is an issue with the user privileges or incorrect credentials. This article explores …

    • 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 …

    • How to Fix Java.SQL.SQLException: Access Denied for …

      Feb 2, 2024 · This tutorial demonstrates how to solve the java.sql.sqlexception: access denied for user root@localhost error in Java.