CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error

How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: June 8, 2026 · 3 min read · 0 Comments
Share: in X

If you are developing a Java or Spring Boot application with a MySQL database, you might suddenly encounter this frustrating connection error:

Caused by: java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed

This issue usually triggers right after upgrading to MySQL 8.0+, moving to a new server, or setting up a fresh local development environment.

In this step-by-step guide, you will learn exactly why this error happens and the three easiest ways to fix it in under two minutes.

Table of Contents

  • Why Does “Public Key Retrieval is not allowed” Happen?
  • Method 1: Update Your JDBC Connection URL (Fastest Fix)
  • For Plain Java Applications
  • For Spring Boot (application.properties)
  • For Spring Boot (application.yml)
  • Method 2: Fix the Error in Database GUI Clients (DBeaver, DataGrip)
    • Steps for DBeaver:
  • Method 3: Change User Authentication (Best for Production)
  • Summary Checklist

Why Does “Public Key Retrieval is not allowed” Happen?

Starting with MySQL 8.0, the default authentication plugin switched from mysql_native_password to caching_sha2_password. This newer mechanism requires an RSA public key to securely encrypt user passwords during the authentication process.

If your application connects to the database over an unencrypted channel (where SSL/TLS is turned off), the JDBC driver attempts to ask the MySQL server for its public key automatically. However, for security reasons, the server blocks this request by default.

Because the driver cannot retrieve the key to encrypt your password, the connection fails and throws the SQLNonTransientConnectionException.

Method 1: Update Your JDBC Connection URL (Fastest Fix)

The quickest and most common fix—especially for local development—is to explicitly tell your JDBC driver that it is safe to retrieve the public key and that you want to bypass SSL.

You can do this by appending allowPublicKeyRetrieval=true&useSSL=false to your database connection string.

For Plain Java Applications

Update your connection string in your database configuration class:

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

For Spring Boot (application.properties)

Add the parameters directly to your datasource URL line:

spring.datasource.url=jdbc:mysql://localhost:3306/your_database?allowPublicKeyRetrieval=true&useSSL=false

For Spring Boot (application.yml)

If you use YAML configuration, format your configuration like this:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/your_database?allowPublicKeyRetrieval=true&useSSL=false

Method 2: Fix the Error in Database GUI Clients (DBeaver, DataGrip)

If you are seeing this error while trying to connect through a database GUI tool like DBeaver or IntelliJ DataGrip, you do not need to change code. You just need to flip a couple of driver settings.

Steps for DBeaver:

  1. Right-click on your database connection in the sidebar and select Edit Connection.
  2. Click on the Driver properties tab.
  3. Scroll down or use the search bar to locate the property named allowPublicKeyRetrieval and change its value to true.
  4. Locate the property named useSSL and change its value to false.
  5. Click Test Connection to verify, then click OK to save.

Method 3: Change User Authentication (Best for Production)

While Method 1 and 2 work perfectly for local development, disabling SSL (useSSL=false) is not recommended for production environments because it sends data across the network unencrypted.

If you need a secure alternative without altering connection parameters, you can downgrade the specific MySQL user’s authentication method back to the legacy native password plugin.

Open your MySQL terminal or workbench as an admin user and run these commands:

-- Replace 'your_db_user' and 'your_password' with your actual database details
ALTER USER 'your_db_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';

-- Clear the privileges cache to apply changes immediately
FLUSH PRIVILEGES;

Once this SQL script runs, the database user will use the classic handshake, bypassing the need for RSA public key retrieval entirely.

Summary Checklist

EnvironmentRecommended FixParameter / Command to Use
Local DevModify Connection StringallowPublicKeyRetrieval=true&useSSL=false
GUI ClientsEdit Driver PropertiesFlip allowPublicKeyRetrieval to true
ProductionAlter MySQL UserIDENTIFIED WITH mysql_native_password

By applying one of these methods, your Java application will seamlessly connect to your MySQL 8 database without any authentication interruptions.

Related Posts:

  • MySQL Commands for Developers
  • Most Frequently Asked Spring Boot Interview…
  • How to Connect Java Application to a Remote Database…
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • JDBC Interview Questions: Ace Your Technical Screening
  • What is JDBC: Bridging Java and Databases
Tags:javamysqlspring-boot
Was this article helpful?
← Previous ArticleComplete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
Next Article →How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap