Java cannot log in to MySQL 8: authentication plugin caching_sha2_password not supported
Unable 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.
Fix it fast
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;"2Match what you see, then jump to its fix
- 1
The 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
The error says authentication requires a secure connectionA new driver, but the connection is not encrypted and has no RSA keyFix - 3
A legacy tool in the same stack cannot use the plugin at allA client that cannot be upgraded yetFix
- 1
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-connectorConfirmed by dev.mysql.com
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=true3Verify
Connect again with the new URL.
mysql --ssl-mode=DISABLED -u app -p --get-server-public-keyConfirmed by dev.mysql.com
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
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 fixDatabases
A client says it cannot load the caching_sha2_password plugin
A client or connector built before MySQL 8.0 cannot connect to an account that uses caching_sha2_password
MySQL 8 and 8.4 create accounts with the caching_sha2_password plugin.
3 causes, each with a fixDatabases
Hibernate fails on a CREATE or ALTER TABLE statement at startup
Error executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC Statement
Hibernate generated a CREATE, ALTER or DROP statement and MySQL refused it.
3 causes, each with a fixDatabases
Spring Boot stops with 'Unable to build Hibernate SessionFactory'
Could not open JPA EntityManager for transaction
This message only says that JPA could not start.
3 causes, each with a fixDatabases
@CreatedDate is not filled in when I save an entity
@CreatedDate and @LastModifiedDate fields stay null
Spring Data JPA fills @CreatedDate only when auditing is switched on and the entity is wired to the auditing listener.
4 causes, each with a fixDatabases
H2 in PostgreSQL mode still behaves differently from PostgreSQL
Names or null ordering differ from PostgreSQL in test results
H2 compatibility modes are switched on in the JDBC URL and cover only a small part of the differences between databases.
4 causes, each with a fix