📌  相关文章
📜  com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not allowed - SQL (1)

📅  最后修改于: 2023-12-03 15:30:03.801000             🧑  作者: Mango

Public Key Retrieval is not allowed - SQL Exception

This is a common SQL exception that is thrown when attempting to establish a connection to a MySQL database.

Exception

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not allowed

Explanation

This exception occurs when the connecting client (in this case, the JDBC driver) is not allowed to request the public key from the server to perform secure authentication. This restriction was introduced in MySQL 8.0.11.

Solution

There are multiple ways to resolve this issue:

  • Upgrade to the latest version of the MySQL JDBC driver (version 8.0.11 or later), which supports the new authentication method.
  • Change the authentication method used by the MySQL server to an older one. This can be done by modifying the default_authentication_plugin setting in the MySQL configuration file.
  • Modify the JDBC connection string to explicitly specify the authentication method to use. This can be done by appending &allowPublicKeyRetrieval=true to the connection string. For example:
String url = "jdbc:mysql://localhost:3306/mydatabase?allowPublicKeyRetrieval=true";
Connection conn = DriverManager.getConnection(url, "username", "password");
Conclusion

In summary, the Public Key Retrieval not allowed SQL exception can be resolved by upgrading the JDBC driver, changing the MySQL server's authentication method, or modifying the JDBC connection string.