Java-MySQL Connection Error: Public Key Retrieval Not Allowed
Overview
If you are encountering the error "Java-MySQL Connection Error: Public Key Retrieval Not Allowed", it means that your Java application is unable to connect to the MySQL database because the server's public key retrieval is not allowed. This error can occur when using Java versions 8u141 and later.
Solution
To resolve this error, you can add the "allowPublicKeyRetrieval=true" property to your JDBC connection string. This property enables public key retrieval and allows your Java application to connect to the MySQL database.
Here is an example of how to add this property to your JDBC connection string:
String url = "jdbc:mysql://localhost:3306/mydatabase?allowPublicKeyRetrieval=true";
Connection conn = DriverManager.getConnection(url, "username", "password");
Alternatively, you can also update your MySQL server configuration to allow public key retrieval by setting the "mysql_native_password" authentication plugin to use the "caching_sha2_password" plugin instead.
To do this, you can run the following SQL command:
ALTER USER 'username'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';
Note that you will need to replace 'username' and 'password' with your own credentials.
Conclusion
The "Java-MySQL Connection Error: Public Key Retrieval Not Allowed" can be caused by a security change in Java versions 8u141 and later. To resolve this error, you can add the "allowPublicKeyRetrieval=true" property to your JDBC connection string or update your MySQL server configuration. By following these steps, you can successfully connect your Java application to the MySQL database.
Leave a Reply
Related posts