Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Java cannot log in to MySQL 8: authentication plugin caching_sha2_password not supported

ErrorUnable to load authentication plugin 'caching_sha2_password'.

Short answer

MySQL 8 accounts use the caching_sha2_password plugin by default, and Connector/J releases before 8.0.9 cannot use it. Move to the mysql-connector-j artifact (8.0.9 or newer). Over an unencrypted connection the plugin also needs an RSA key exchange, so use TLS or allow public key retrieval.

  • Applies to MySQL 8.0, MySQL 8.4, Connector/J 8.x, Spring Boot 3, Maven 3.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find which MySQL driver the application really loads, and what plugin the account uses.

    mvn dependency:tree | grep -i mysql
    mysql -u root -p -e "SELECT user, host, plugin FROM mysql.user;"
  2. 2Match what you see, then jump to its fix

    1. 1The dependency tree shows mysql-connector-java 5.1.x or an early 8.0 buildThe JDBC driver is older than Connector/J 8.0.9Fix
    2. 2The error says authentication requires a secure connectionA new driver, but the connection is not encrypted and has no RSA keyFix
    3. 3A legacy tool in the same stack cannot use the plugin at allA client that cannot be upgraded yetFix

What does the error look like?

  • Unable to load authentication plugin 'caching_sha2_password'.
  • ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.

Cause 1 of 3

The JDBC driver is older than Connector/J 8.0.9

1Check

Connector/J 8.0.9 added support for caching_sha2_password. The release notes say earlier 8.0 versions cannot connect to accounts that use it, and recommend upgrading.

mvn dependency:tree | grep -i "mysql-connector"

2Fix

Use a current Connector/J. In a Spring Boot project the version is usually managed for you, so remove any old pinned version.

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

3Verify

Start the application; the login succeeds.

mvn dependency:tree | grep -i mysql-connector

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 3

A new driver, but the connection is not encrypted and has no RSA key

1Check

caching_sha2_password needs either a secure connection or an unencrypted connection that supports password exchange using an RSA key pair. Connector/J enables the RSA route through allowPublicKeyRetrieval and serverRSAPublicKeyFile.

grep -rn jdbc:mysql src/main/resources/

2Fix

Prefer a TLS connection. In a private development network you can let the driver request the server's public key.

jdbc:mysql://localhost:3306/app?allowPublicKeyRetrieval=true

3Verify

Connect again with the new URL.

mysql --ssl-mode=DISABLED -u app -p --get-server-public-key

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

A client that cannot be upgraded yet

1Check

The MySQL manual gives one explicit alternative: create the account with the older plugin. It also marks mysql_native_password as deprecated, so treat it as a bridge until the client can be upgraded.

SELECT user, host, plugin FROM mysql.user WHERE user = 'legacy';

2Fix

Create a separate account for the legacy client.

CREATE USER 'legacy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'change-me';

3Verify

The legacy client logs in with that account.

SELECT user, plugin FROM mysql.user WHERE user = 'legacy';

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Pin MySQL driver versions in one place, and update them together with the server major version.
  • Use TLS between the app and the database so the RSA workaround is not needed.
  • Do not set defaultAuthenticationPlugin unless you know why; Connector/J otherwise follows the server's default plugin.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Databases

JDBC says Public Key Retrieval is not allowed when connecting to MySQL

ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.

The account uses caching_sha2_password and your connection is not encrypted, so the client needs the server's RSA public key to send the password.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.