Connection Java – MySQL : Public Key Retrieval Is Not Allowed

The "Public Key Retrieval is not allowed" error typically occurs when trying to establish a secure connection between Java and MySQL using SSL/TLS encryption.

To fix this issue, you can try the following steps:

  1. Ensure that you have the MySQL Connector/J JDBC driver for Java installed in your project. You can download it from the official MySQL website or add it as a Maven/Gradle dependency.

  2. Verify that your MySQL server is configured to allow SSL/TLS connections. You can enable SSL/TLS by modifying the MySQL server configuration file (my.cnf) and adding the necessary SSL/TLS settings. For example:


    Replace "/path/to" with the actual path to your CA certificate, server certificate, and server key files.

  3. In your Java code, establish the connection to MySQL as follows:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class Main {
        public static void main(String[] args) {
            String url = "jdbc:mysql://localhost:3306/database_name?useSSL=true&requireSSL=true";
            String user = "username";
            String password = "password";
            try {
                Connection conn = DriverManager.getConnection(url, user, password);
                // Use the connection...
            } catch (SQLException | ClassNotFoundException e) {

    Make sure to replace "localhost:3306" with the actual hostname and port of your MySQL server, and "database_name", "username", and "password" with your own database credentials.

  4. If you still encounter the "Public Key Retrieval is not allowed" error, try adding the following additional connection properties:

    String url = "jdbc:mysql://localhost:3306/database_name?useSSL=true&requireSSL=true&allowPublicKeyRetrieval=true";

    The "allowPublicKeyRetrieval=true" property allows the client to retrieve the public key from the server during the SSL handshake process.

  5. Make sure that your Java version is compatible with the SSL/TLS encryption protocols supported by your MySQL server. Older Java versions may have limitations in terms of SSL/TLS support. Consider upgrading to a newer Java version if necessary.

By following these steps, you should be able to establish a secure connection between Java and MySQL using SSL/TLS encryption without encountering the "Public Key Retrieval is not allowed" error.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!