Connection Java - MySQL : Public Key Retrieval is not allowed

Cover Image for Connection Java - MySQL : Public Key Retrieval is not allowed
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 Blog Post: Connection Java - MySQL : Public Key Retrieval is not allowed

Connecting Java to a MySQL database should be a breeze, but sometimes you run into those pesky exceptions that can make your head spin. One common issue that might arise is the "Public Key Retrieval is not allowed" error. Don't worry, though! I'm here to break it down for you and provide easy solutions so you can get your Java and MySQL connection up and running smoothly!

🕵️‍♂️ Understanding the Issue

So, you're trying to connect to your MySQL database using the Connector 8.0.11, and everything seems fine until you hit an exception. The exception message looks something like this:

Exception in thread "main" java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:108) at ...

This error occurs when the MySQL server doesn't allow fetching the public key, which is required for secure SSL/TLS connections. By default, Connector/J 8.0+ enforces stricter security measures, leading to this exception.

💡 Easy Solutions

No need to panic! Here are a couple of easy solutions to fix the "Public Key Retrieval is not allowed" error:

1. Disabling SSL/TLS

One straightforward solution is to disable SSL/TLS encryption for the connection. To do this, you can modify your ConnectionManager class as follows:

public static Connection getConnection() throws SQLException {

    MysqlDataSource dataSource = new MysqlDataSource();

    dataSource.setUseSSL(false);
    // ...
    return dataSource.getConnection();
}

By setting setUseSSL(false), you disable SSL/TLS for the MySQL connection, and the exception should disappear. However, make sure to only use this solution if your connection doesn't contain sensitive data, as it could potentially compromise security.

2. Allowing Public Key Retrieval

If maintaining SSL/TLS encryption is crucial for your connection, you can modify the JDBC URL to allow public key retrieval. Here's an example of how it could look:

public static final String jdbcURL = "jdbc:mysql://localhost/database?allowPublicKeyRetrieval=true";

By appending allowPublicKeyRetrieval=true to the JDBC URL, you instruct Connector/J to allow fetching the public key, resolving the issue.

🚀 Time to Engage!

I hope these solutions helped you overcome the "Public Key Retrieval is not allowed" error when connecting Java to a MySQL database. Now it's your turn to take action!

👉 Try implementing one of the solutions provided in your code and see if it resolves the issue.

👉 Have any other exceptional connection problems? Comment below, and let's solve them together!

Remember, troubleshooting database connections can be daunting, but armed with the right knowledge, you'll conquer any challenge that comes your way! Happy coding! 😄


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello