Home > Database > Mysql Tutorial > How to Resolve the \'Public Key Retrieval is not allowed\' Exception When Connecting Java to MySQL?

How to Resolve the \'Public Key Retrieval is not allowed\' Exception When Connecting Java to MySQL?

Linda Hamilton
Release: 2024-10-31 05:53:30
Original
438 people have browsed it

How to Resolve the

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>
Copy after login

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>
Copy after login

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:

  • Enabling public key retrieval may expose security risks. Consider disabling it after testing or development.
  • Alternatively, set useSSL=false for development purposes only. Do not use this in production environments as it disables SSL encryption.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template