Connection Java-MySql : Public Key Retrieval is not Allowed

In this post, I will provide a solution for frequently occurring exceptions 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 1

First, you need to check whether your MySQL server is up and running on your machine or not. Before starting your Java or Spring Boot application, make sure that the MySQL server is up and running on your machine.

Solution 2

If your MySQL server is running and still you are getting the same issue then try this solution.
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

  1. When I apply the above changes I get the below errors
    Publishing META-INF/context.xml configurations...
    Could not load the context configuration for the web-student-tracker context due to a syntax error or other exception.
    The reference to entity "allowPublicKeyRetrieval" must end with the ';' delimiter.

    ReplyDelete
  2. I am getting error "Access denied for user 'root'@'localhost' (using password: YES)"

    ReplyDelete

Post a Comment

Leave Comment