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
- https://stackoverflow.com/questions/50379839/connection-java-mysql-public-key-retrieval-is-not-allowed
- https://mysqlconnector.net/connection-options/
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course