Connection Java-MySql : Public Key Retrieval is not Allowed

In this post, I will provide a solution for frequently occurring exception while working with MySQL database -  "Connection Java-MySql: Public Key Retrieval is not allowed".

Problem

I have encountered this issue and below issue description.
I was using a MySQL database with Java using the mysql-connector 8.0.13 version. Everything seems to be correct but I have this exception:

Exception in thread "main" java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
Stack Trace:
Exception in thread "main" java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed at 
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:108) at 
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) at 
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at 
 com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) at com.mysql.cj.jdbc.ConnectionImpl.
(ConnectionImpl.java:444) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230) 

Solution

We need to set AllowPublicKeyRetrieval=true to allow the client to automatically request the public key from the server.
allowPublicKeyRetrieval=true
You can also disable SSL and also suppress the SSL errors:
useSSL=false

Complete Code Snippet (Use this code)

private static String jdbcURL = "jdbc:mysql://localhost:3306/mysql_database?useSSL=false&allowPublicKeyRetrieval=true";
private static String jdbcUsername = "root";
private static String jdbcPassword = "root";

Reference



Comments