Home > Database > Mysql Tutorial > body text

Why Am I Getting a \'Public Key Retrieval is not allowed\' Error When Connecting to My MySQL Database?

Linda Hamilton
Release: 2024-10-31 05:52:01
Original
505 people have browsed it

Why Am I Getting a

Public Key Retrieval Issue in MySQL-Java Connection

In an attempt to connect to a MySQL database with Java, you may encounter an exception: _Public Key Retrieval is not allowed_. This issue arises due to a security measure implemented when using the MySQL connector.

To address this issue, you need to enable public key retrieval by adding the following option to your MySQL connection string:

allowPublicKeyRetrieval=true
Copy after login

This option allows the client to automatically request the public key from the server, resolving the "Public Key Retrieval is not allowed" exception. However, it's important to note that enabling public key retrieval could potentially allow a malicious proxy to retrieve your password. Hence, it is recommended to use this option only for testing or development purposes.

Example Connection String

Including the allowPublicKeyRetrieval option, your connection string would look something like this:

jdbc:mysql://localhost:3306/db?allowPublicKeyRetrieval=true&useSSL=false
Copy after login

Additionally, for testing/development purposes, you could also set useSSL=false to disable SSL encryption.

Disable SSL for Testing

<code class="java">import com.mysql.cj.jdbc.MysqlDataSource;
...

MysqlDataSource dataSource = new MysqlDataSource();

dataSource.setUseSSL(false);
...</code>
Copy after login

By implementing these adjustments to your connection string and disabling SSL for testing, you can overcome the "Public Key Retrieval is not allowed" exception and successfully connect to your MySQL database.

The above is the detailed content of Why Am I Getting a \'Public Key Retrieval is not allowed\' Error When Connecting to My MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!