Fix 'Public Key Retrieval is not allowed' in MySQL JDBC
For Java developers and database tool users whose MySQL connection fails with 'Public Key Retrieval is not allowed'.
On this page
Your application or database tool connects to MySQL without TLS, and Connector/J stops with "Public Key Retrieval is not allowed". Since MySQL 8.0, accounts use the caching_sha2_password plugin by default. Without TLS, that plugin needs the server's RSA public key, and Connector/J won't ask for it unless you allow it. Add one of these parameters to the JDBC URL, safest first:
| Fix | Add to the JDBC URL | Use it when |
|---|---|---|
| Connect over TLS | sslMode=REQUIRED | The server offers TLS |
| Give the driver a copy of the key | serverRSAPublicKeyFile=/path/to/public_key.pem | The server has no TLS, and other people share the network |
| Let the driver ask for the key | allowPublicKeyRetrieval=true | The server runs on your machine or on a network that only you control |
Why Connector/J refuses to ask for the RSA public key
Without TLS, Connector/J encrypts the password of a caching_sha2_password account with the server's RSA public key before sending it[1]. The driver can ask the server for that key during the login, but the allowPublicKeyRetrieval property is false by default[2]. The default protects the password. On an unencrypted connection, someone between the client and the server could answer with a forged key and decrypt the password.
To see the error, create an account with the default plugin, then run FLUSH PRIVILEGES to empty the server's password cache:
CREATE USER 'app_user'@'%' IDENTIFIED BY 'app_pw';
FLUSH PRIVILEGES;
On 12 November 2026, we connected as app_user from Java 25 with Connector/J 26.10.0 to MySQL 9.1.0 in Docker. FLUSH PRIVILEGES ran before each attempt. After each connection that succeeded, we read Ssl_cipher, a status variable that is empty when the session is unencrypted:
| Parameters in the URL | Result | Ssl_cipher |
|---|---|---|
| sslMode=DISABLED | Public Key Retrieval is not allowed | no connection |
| useSSL=false | Public Key Retrieval is not allowed | no connection |
| no parameters | Connects | TLS_AES_256_GCM_SHA384 |
| sslMode=REQUIRED | Connects | TLS_AES_256_GCM_SHA384 |
| sslMode=DISABLED&serverRSAPublicKeyFile=public_key.pem | Connects | empty |
| sslMode=DISABLED&allowPublicKeyRetrieval=true | Connects | empty |
The two failures threw this exception, with SQLState 08001:
java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
Connector/J reads useSSL=false as sslMode=DISABLED when the URL sets no sslMode[2]. Connector/J 26.10 supports MySQL 8.4 and later[4]. Which Connector/J release to use, and how to check the one you load, is in MySQL Connector/J: mysql-connector-j vs mysql-connector-java.
How to check the account plugin and session encryption
To check which plugin the account uses, run this query from an administrator's session:
SELECT user, host, plugin FROM mysql.user WHERE user = 'app_user';
| user | host | plugin |
|---|---|---|
| app_user | % | caching_sha2_password |
To check the session, run this query on a connection that your application opens:
SHOW SESSION STATUS LIKE 'Ssl_cipher';
A cipher name, such as TLS_AES_256_GCM_SHA384, means the session runs over TLS. An empty value means the session is unencrypted, and the account can fail with this error after the next server restart.
How to fix it with TLS (sslMode=REQUIRED)
Over TLS, the password travels inside the encrypted session, so Connector/J needs no RSA key. If the URL holds sslMode=DISABLED or useSSL=false, delete that parameter. With neither parameter, Connector/J uses sslMode=PREFERRED, which encrypts whenever the server offers TLS[2]. In the run above, the URL with no parameters connected over TLS.
To make TLS mandatory, set sslMode=REQUIRED:
jdbc:mysql://dbhost:3306/appdb?sslMode=REQUIRED
sslMode=REQUIRED encrypts the connection but doesn't check the server's certificate, so it doesn't prove which server answered. sslMode=VERIFY_CA and sslMode=VERIFY_IDENTITY check the certificate. The other sslMode values are in MySQL JDBC URL parameters.
On a server without TLS, sslMode=REQUIRED gives a different error. We started a second MySQL 9.1.0 container with TLS switched off (--tls-version=''). On that server, the URL with no parameters failed with "Public Key Retrieval is not allowed", and sslMode=REQUIRED failed with this:
java.sql.SQLNonTransientConnectionException: SSL Connection required, but not provided by server.
For such a server, give the driver a copy of the key.
How to set allowPublicKeyRetrieval=true or a server key file
serverRSAPublicKeyFile with a copy of the key
serverRSAPublicKeyFile names a file that holds a copy of the server's public key. Connector/J encrypts the password with that copy and never asks the server for the key[2]. The server keeps the key as public_key.pem in its data directory, and the status variable Caching_sha2_password_rsa_public_key shows it[1].
-
Copy public_key.pem from the server's data directory over SSH or another connection you trust. A key that crosses an untrusted network could already be forged. If you have no shell access, read the key with the mysql client, on the server itself or on a machine in a network you trust. The --raw option keeps the key's line breaks, and cut keeps only the value column:
mysql -h dbhost -u root -p --raw --skip-column-names -e "SHOW STATUS LIKE 'Caching_sha2_password_rsa_public_key'" | cut -f2 > public_key.pem -
Keep the file on the machine that runs your application, readable by the account that runs it.
-
Add its path to the JDBC URL:
jdbc:mysql://dbhost:3306/appdb?sslMode=DISABLED&serverRSAPublicKeyFile=/etc/myapp/public_key.pem
In the run above, this URL connected right after FLUSH PRIVILEGES.
allowPublicKeyRetrieval=true on localhost or a trusted network
allowPublicKeyRetrieval=true lets Connector/J ask the server for its key during the login. Use it only where nobody else can read the traffic, such as a MySQL Docker container on your own machine:
jdbc:mysql://127.0.0.1:3306/devdb?sslMode=DISABLED&allowPublicKeyRetrieval=true
The login works, but the session stays unencrypted: Ssl_cipher was empty in the run. Every query and every result crosses the network as plain text.
Why the error returns after a MySQL server restart
Without TLS, the first login of an account needs the RSA key, because the server checks the password in full. After that login, the server caches a hash of the account's password. Later logins of the account are checked against the cache and need no RSA key[1]. So a URL with sslMode=DISABLED can work for days and fail after a restart.
We connected with sslMode=DISABLED and no key setting, each time after a different event:
| Before the sslMode=DISABLED login | Result |
|---|---|
| FLUSH PRIVILEGES | Public Key Retrieval is not allowed |
| One login with allowPublicKeyRetrieval=true | Connects |
| One login with sslMode=REQUIRED | Connects |
| docker restart of the MySQL container | Public Key Retrieval is not allowed |
| ALTER USER that set the same password again | Public Key Retrieval is not allowed |
The server empties the cache at shutdown and on FLUSH PRIVILEGES. It drops one account's entry when the account gets a password, even the same one, or is renamed or dropped[1]. A login over TLS fills the cache too. So one tool that connects over TLS can hide the error from an application that connects without TLS, until the next restart.
Why a wrong user name or password gives the same error
Without TLS, a wrong password also gives "Public Key Retrieval is not allowed", even while the cache holds the right password. A wrong password never matches the cache. The server then checks it in full, and without TLS that check needs the RSA key. In the run, a wrong password for app_user failed this way under sslMode=DISABLED, and so did nobody, a user name that doesn't exist.
With TLS, a pinned key, or allowPublicKeyRetrieval=true, the same wrong password failed with the real cause, SQLState 28000:
java.sql.SQLException: Access denied for user 'app_user'@'172.17.0.1' (using password: YES)
If the error stays after you have applied a fix, check the user name and the password next.
Why switching to mysql_native_password no longer works
Older answers move the account to mysql_native_password, which needs no RSA key. That plugin was deprecated in MySQL 8.0.34, is disabled by default in MySQL 8.4, and was removed in MySQL 9.0.0[3]. On MySQL 9.1.0, an account can't use it at all:
CREATE USER 'legacy_user'@'%' IDENTIFIED WITH mysql_native_password BY 'legacy_pw';
ERROR 1524 (HY000) at line 1: Plugin 'mysql_native_password' is not loaded
On MySQL 8.4, the plugin works only when the server starts with --mysql-native-password=ON[3]. An account on it fails again after the upgrade to MySQL 9.
How to apply the fix in DbSchema and test the connection
DbSchema connects to MySQL through Connector/J. The DbSchema connection dialog sets the URL parameter of each fix:
- In the DbSchema connection dialog, open the Network and Driver tab.
- Press the pencil next to SSL & Parameters.
- For TLS, tick the SSL / TLS group and choose Required as the SSL Mode.
- For a key file or key retrieval, open the Authentication group instead. It holds serverRSAPublicKeyFile and allowPublicKeyRetrieval.
- Press OK, then Test Connection.
To check the URL, press the eye button next to SSL & Parameters. It shows the JDBC URL that DbSchema will connect with, including the parameter you set.
Download DbSchema, connect to your MySQL server with the fix in place, and reverse-engineer the database into an interactive diagram. Connecting, reverse-engineering and the SQL Editor are in the free Community Edition.
Sources
Connect to MySQL with the fix applied
DbSchema connects to MySQL through the Connector/J JDBC driver. SSL & Parameters on the Network and Driver tab sets sslMode, allowPublicKeyRetrieval or serverRSAPublicKeyFile, and the eye button shows the JDBC URL it will use. Connecting, reverse-engineering and diagrams are in the free Community Edition.