Connection Java - MySQL: Troubleshooting the "Public Key Retrieval is not allowed" Exception
When attempting to establish a connection to a MySQL database using Java connector 8.0.11, developers may encounter the "Public Key Retrieval is not allowed" exception. This error arises during the connection handshake process and signifies that the client (Java application) is not authorized to obtain the public key from the server for authentication.
Cause of Exception
The exception occurs when the Java application tries to retrieve the server's public key, which is used to encrypt and transmit sensitive information like the user password. By default, MySQL disallows public key retrieval to prevent potential man-in-the-middle (MITM) attacks.
Solution
To resolve this issue, the Java application must be explicitly configured to allow public key retrieval when establishing the database connection. This can be achieved by specifying the allowPublicKeyRetrieval=true parameter as part of the JDBC connection string.
Here's an example of a JDBC connection string that includes the allowPublicKeyRetrieval parameter:
jdbc:mysql://localhost:3306/db?allowPublicKeyRetrieval=true
Note that enabling public key retrieval poses a potential security risk by making the client susceptible to MITM attacks if the connection is not over a secure channel (e.g., HTTPS). For development and testing purposes, it may be safer to disable SSL and explicitly specify useSSL=false in the connection string:
jdbc:mysql://localhost:3306/db?allowPublicKeyRetrieval=true&useSSL=false
Additional Tips
If the issue persists despite specifying allowPublicKeyRetrieval=true, try the following additional measures:
The above is the detailed content of Why Does My Java Application Throw a \'Public Key Retrieval is not allowed\' Exception When Connecting to MySQL?. For more information, please follow other related articles on the PHP Chinese website!