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
- 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
When I apply the above changes I get the below errors
ReplyDeletePublishing 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.
I am getting error "Access denied for user 'root'@'localhost' (using password: YES)"
ReplyDelete