I try to connect MySql database with Java using connector 8.0.11. Everything seems to be ok 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) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226) at com.mysql.cj.jdbc.MysqlDataSource.getConnection(MysqlDataSource.java:438) at com.mysql.cj.jdbc.MysqlDataSource.getConnection(MysqlDataSource.java:146) at com.mysql.cj.jdbc.MysqlDataSource.getConnection(MysqlDataSource.java:119) at ConnectionManager.getConnection(ConnectionManager.java:28) at Main.main(Main.java:8)
Connector Manager:
public class ConnectionManager {
public static final String serverTimeZone = "UTC";
public static final String serverName = "localhost";
public static final String databaseName ="biblioteka";
public static final int portNumber = 3306;
public static final String user = "anyroot";
public static final String password = "anyroot";
public static Connection getConnection() throws SQLException {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUseSSL( false );
dataSource.setServerTimezone( serverTimeZone );
dataSource.setServerName( serverName );
dataSource.setDatabaseName( databaseName );
dataSource.setPortNumber( portNumber );
dataSource.setUser( user );
dataSource.setPassword( password );
return dataSource.getConnection();
}
}
Give connection URL as jdbc:mysql://localhost:3306/hb_student_tracker?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
I solve this issue using below configuration on spring boot framework
Update the useSSL=true in spring boot application connection with mysql;
Alternatively to the suggested answers you could try and use mysql_native_password authentication plugin instead of caching_sha2_password authentication plugin.
I found this issue frustrating because I was able to interact with the database yesterday, but after coming back this morning, I started getting this error.
I tried adding the
allowPublicKeyRetrieval=true
flag, but I kept getting the error.What fixed it for me was doing
Project->Clean
in Eclipse andClean
on my Tomcat server. One (or both) of those fixed it.I don't understand why, because I build my project using Maven, and have been restarting my server after each code change. Very irritating...
If you are getting the following error while connecting the mysql (either local or mysql container running the mysql):
java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
Solution: Add the following line in your database service: