How to resolve “java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed” error in java

If you are connecting to MySQL version 8 from your java application, there are bright chances that you might have come across the following error

java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed

and if you are accessing a MySQL database from a web or application server such as Wildfly or Tomcat, then you would have encountered this error

java.sql.SQLException: Cannot create PoolableConnectionFactory (Public Key Retrieval is not allowed)

Solution
Add allowPublicKeyRetrieval connection option of MySQL with a value of true to the JDBC connection string. Thus, your connection string should be modified as

jdbc:mysql://<database server ip>:3306/databaseName?allowPublicKeyRetrieval=true

Reason
Since you are connecting to a database, you will be using password authentication. Obviously, the password should be protected while transmitting over the network. By default Transport Layer Security(TLS) is used but if it cannot be used, then RSA public key encryption is used.

In order for the client to request for the public key from the database server, allowPublicKeyRetrieval option is used. But remember, using this option could expose the password to malicious attacks. That is why, this option is set to false by default.

sdasdasd

Leave a Reply