[Resolved] MySQL JDBC Issue: Public Key Retrieval is not allowed

[Resolved] MySQL JDBC Issue: Public Key Retrieval is not allowed
E
EdToks2:14 min read

In this article, I will explain a couple of solutions to addressing the given error in Java development with MySQL and JDBC:

 

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

This error occurs when the Java client attempts to establish a connection to the MySQL server using a secure connection (HTTPS), but the default settings disallow public key retrieval.

The issue stems from a compatibility mismatch between the version of the MySQL JDBC driver (mysql-connector-java) and the MySQL server version. For example, using mysql-connector-java version 5.x with a MySQL server version 8.x will trigger this error. To resolve it, you can employ either of the following approaches:

  1. Update the JDBC URL:
    To remedy the error, add the parameter allowPublicKeyRetrieval=true to the JDBC URL:
    jdbc:mysql://localhost:3306/bookstoredb?allowPublicKeyRetrieval=true

However, this modification may lead to a subsequent error:
javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify

To resolve this, append the parameter useSSL=false to the JDBC URL:
jdbc:mysql://localhost:3306/bookstoredb?allowPublicKeyRetrieval=true&useSSL=false

It’s essential to note that using this solution in a production environment can pose security risks to the MySQL server. Consider this approach only for local development and testing purposes.

  1. Update the MySQL JDBC Driver Version:
    A more advisable way to resolve the “Public Key Retrieval is not allowed” error involves updating the version of mysql-connector-java to a version compatible with the MySQL server. For instance, if the MySQL server version is 8.0.15, select the corresponding MySQL JDBC driver version.

For projects utilizing Maven, update the dependency as follows:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.15</version>
    <scope>runtime</scope>
</dependency>

You may also need to include useSSL=false in the JDBC URL.

These solutions effectively address the “Public Key Retrieval is not allowed” error in Java programming with MySQL databases and the MySQL JDBC driver. I hope you find this information beneficial for resolving your issue.

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.