[Fixed] MySQL JDBC Error: Public Key Retrieval is not allowed
- Details
- Written by Nam Ha Minh
- Last Updated on 12 November 2021   |   Print Email
In this video, I’d like to share a couple of solutions which you can use to fix the following error in Java developing with MySQL and JDBC:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not allowed
This error means that the client (Java program) tries to make a connection to MySQL server with secure connection (HTTPS), but the public key retrieval is not allowed by default.
This error is caused by the version of MySQL JDBC driver (mysql-connector-java) not compatible with the version of MySQL server, e.g. mysql-connector-java version 5.x whereas server version is 8.x. So you could fix this error by using either of the following ways:
1. Update JDBC URL
Add the parameter allowPublicKeyRetrieval=true to the JDBC URL:
jdbc:mysql://localhost:3306/bookstoredb?allowPublicKeyRetrieval=true
Then you would see the following error:
javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify
To get rid of this error, append the parameter useSSL=false to the JDBC URL:
jdbc:mysql://localhost:3306/bookstoredb?allowPublicKeyRetrieval=true&useSSL=false
Note that this fix is not recommended on production as it poses a security threat to MySQL server. Use this fix for local development and testing only.
2. Update MySQL JDBC Driver Version
I think the best way to fix the error “Public Key Retrieval is not allowed” is updating the version of my-sql-connector-java to the version compatible with the version of MySQL server. For example, if your MySQL server version is 8.0.15, then you need to use the same version of MySQL JDBC driver.
If your Java project is using Maven, update the dependency like this:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
<scope>runtime</scope>
</dependency>
And you may also need to append useSSL=false to the JDBC URL.
So those are two solutions that fix the error “Public Key Retrieval is not allowed” in Java programming with MySQL database and MySQL JDBC driver. I hope you found this post helpful.
IMPORTANT NOTE: If those solutions do not work with a Java IDE (e.g. Eclipse), you should shutdown your computer entirely (shutdown, not restart). Then turn on your computer and test again, it will work.
Other JDBC Tutorials:
- JDBC Driver Downloads
- JDBC Database Connection URLs
- How to connect to a database with JDBC
- JDBC CRUD Tutorial
- JDBC Transaction Tutorial
- How to call stored procedure with JDBC
- How to read database metadata in JDBC
- How to insert binary data into database with JDBC
- How to read binary data from database with JDBC
- How to use Scrollable ResultSet
- How to use Updatable ResultSet
- How to use CachedRowSet
Comments