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: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.<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.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.