MySql 8.x java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed


Error in remote connection to MySQL 8.0 using mybatis plus to reverse generate entity class codegenerator

Exception in thread "main" java.lang.RuntimeException: java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
	at com.baomidou.mybatisplus.generator.config.DataSourceConfig.getConn(
	at com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder.<init>(
	at com.baomidou.mybatisplus.generator.AutoGenerator.execute(
Caused by: java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(
	at com.mysql.cj.jdbc.ConnectionImpl.<init>(
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(
	at java.sql/java.sql.DriverManager.getConnection(
	at java.sql/java.sql.DriverManager.getConnection(
	at com.baomidou.mybatisplus.generator.config.DataSourceConfig.getConn(
	... 3 more


Official website:

         If the user account uses sha256_ Password authentication, transmission must protect the password; TLS is the preferred mechanism, but if it is not available, RSA public key encryption will be used. To specify the RSA public key of the server, use the serverrsapublickeyfile connection string setting, or set allowpublickeyretrieval = true to allow clients to automatically request the public key from the server. Note that allowpublickeyretrieval = true may allow malicious agents to perform mitm attacks to obtain plaintext passwords, so it is false by default and must be explicitly enabled.

         Because allowpublickeyretrieval and allowpublickey retrieval are false by default, it is necessary to add “allowpublickeyretrieval = true” on the JDBC connection


Add “allowpublickeyretrieval = true” to JDBC connection


Read More: