Home >Database >Mysql Tutorial >How to Resolve the \'Public Key Retrieval is not allowed\' Exception When Connecting Java to MySQL?
Connection Java - MySQL: Resolving "Public Key Retrieval is not allowed" Exception
When attempting to establish a connection to a MySQL database using Java and the 8.0.11 connector, users may encounter the following exception:
<code class="java">Exception in thread "main" java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed</code>
Solution:
This exception indicates that the client is attempting to retrieve the public key from the server, but public key retrieval is not permitted. To resolve this issue, we need to explicitly allow public key retrieval by adding the allowPublicKeyRetrieval=true option to the MySQL connection string.
Modified Connection Manager Class:
<code class="java">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 ); // Allow public key retrieval dataSource.setAllowPublicKeyRetrieval( true ); return dataSource.getConnection(); }</code>
By setting allowPublicKeyRetrieval to true, the client is authorized to request the public key from the server. This resolves the "Public Key Retrieval is not allowed" exception.
Additional Considerations:
The above is the detailed content of How to Resolve the \'Public Key Retrieval is not allowed\' Exception When Connecting Java to MySQL?. For more information, please follow other related articles on the PHP Chinese website!