In this video, I’d like to share a couple of solutions which you can use to fix the following error in Java developing with MySQL and JDBC:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not allowed

This error means that the client (Java program) tries to make a connection to MySQL server with secure connection (HTTPS), but the public key retrieval is not allowed by default.

This error is caused by the version of MySQL JDBC driver (mysql-connector-java) not compatible with the version of MySQL server, e.g. mysql-connector-java version 5.x whereas server version is 8.x. So you could fix this error by using either of the following ways:

 

1. Update JDBC URL

Add the parameter allowPublicKeyRetrieval=true to the JDBC URL:

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

Then you would see the following error:

javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify

To get rid of this error, append the parameter useSSL=false to the JDBC URL:

jdbc:mysql://localhost:3306/bookstoredb?allowPublicKeyRetrieval=true&useSSL=false

Note that this fix is not recommended on production as it poses a security threat to MySQL server. Use this fix for local development and testing only.


2. Update MySQL JDBC Driver Version

I think the best way to fix the error “Public Key Retrieval is not allowed” is updating the version of my-sql-connector-java to the version compatible with the version of MySQL server. For example, if your MySQL server version is 8.0.15, then you need to use the same version of MySQL JDBC driver.



If your Java project is using Maven, update the dependency like this:

<dependency>

          <groupId>mysql</groupId>

          <artifactId>mysql-connector-java</artifactId>

          <version>8.0.15</version>

          <scope>runtime</scope>

</dependency>

And you may also need to append useSSL=false to the JDBC URL.

So those are two solutions that fix the error “Public Key Retrieval is not allowed” in Java programming with MySQL database and MySQL JDBC driver. I hope you found this post helpful.

 

IMPORTANT NOTE: If those solutions do not work with a Java IDE (e.g. Eclipse), you should shutdown your computer entirely (shutdown, not restart). Then turn on your computer and test again, it will work.

 

Other JDBC Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#1ach2022-09-09 04:20
thank you very much for your time and your kindness
Quote